11package main
22
33import (
4- "context"
54 "fmt"
65 "log"
76 "os"
7+ "path/filepath"
88 "time"
9- _ "time/tzdata"
109
11- "github.com/ory/graceful"
12- flag "github.com/spf13/pflag"
13- "github.com/xos/serverstatus/cmd/dashboard/controller"
14- "github.com/xos/serverstatus/cmd/dashboard/rpc"
10+ "github.com/spf13/pflag"
1511 "github.com/xos/serverstatus/model"
1612 "github.com/xos/serverstatus/proto"
1713 "github.com/xos/serverstatus/service/singleton"
@@ -22,19 +18,29 @@ type DashboardCliParam struct {
2218 ConfigFile string // 配置文件路径
2319 DatebaseLocation string // Sqlite3 数据库文件路径
2420 ResetTraffic bool // 重置所有服务器的累计流量数据
21+ LogDir string // 日志文件目录
2522}
2623
2724var (
2825 dashboardCliParam DashboardCliParam
26+ dir string
27+ hostName string
2928)
3029
3130func init () {
32- flag .CommandLine .ParseErrorsWhitelist .UnknownFlags = true
33- flag .BoolVarP (& dashboardCliParam .Version , "version" , "v" , false , "查看当前版本号" )
34- flag .StringVarP (& dashboardCliParam .ConfigFile , "config" , "c" , "data/config.yaml" , "配置文件路径" )
35- flag .StringVar (& dashboardCliParam .DatebaseLocation , "db" , "data/sqlite.db" , "Sqlite3数据库文件路径" )
36- flag .BoolVar (& dashboardCliParam .ResetTraffic , "reset-traffic" , false , "重置所有服务器的累计流量数据" )
37- flag .Parse ()
31+ var err error
32+ dir , err = os .Getwd ()
33+ if err != nil {
34+ panic (err )
35+ }
36+
37+ // 使用pflag库解析命令行参数
38+ pflag .BoolVarP (& dashboardCliParam .Version , "version" , "v" , false , "查看当前版本号" )
39+ pflag .StringVarP (& dashboardCliParam .ConfigFile , "config" , "c" , "data/config.json" , "配置文件路径" )
40+ pflag .StringVarP (& dashboardCliParam .DatebaseLocation , "db" , "d" , "data/data.db" , "数据库文件路径" )
41+ pflag .BoolVar (& dashboardCliParam .ResetTraffic , "reset-traffic" , false , "重置所有服务器的流量统计" )
42+ pflag .StringVar (& dashboardCliParam .LogDir , "logdir" , "logs" , "日志文件目录" )
43+ pflag .Parse ()
3844}
3945
4046func initSystem () {
@@ -53,46 +59,60 @@ func initSystem() {
5359}
5460
5561func main () {
62+ var err error
63+ hostName , err = os .Hostname ()
64+ if err != nil {
65+ panic (err )
66+ }
67+
68+ // 设置日志目录
69+ if dashboardCliParam .LogDir != "" {
70+ singleton .SetLogDir (dashboardCliParam .LogDir )
71+ }
72+
73+ // 初始化日志系统
74+ if err := singleton .InitLogger (); err != nil {
75+ fmt .Printf ("初始化日志系统失败: %v\n " , err )
76+ os .Exit (1 )
77+ }
78+ defer singleton .CloseLogger ()
79+
5680 if dashboardCliParam .Version {
5781 fmt .Println (singleton .Version )
58- os .Exit (0 )
82+ return
83+ }
84+
85+ // 如果指定了绝对路径,则使用绝对路径,否则使用相对于工作目录的路径
86+ configFilePath := dashboardCliParam .ConfigFile
87+ if ! filepath .IsAbs (configFilePath ) {
88+ configFilePath = filepath .Join (dir , configFilePath )
5989 }
6090
61- // 初始化 dao 包
62- singleton .InitConfigFromPath (dashboardCliParam .ConfigFile )
91+ // 如果指定了绝对路径,则使用绝对路径,否则使用相对于工作目录的路径
92+ dbPath := dashboardCliParam .DatebaseLocation
93+ if ! filepath .IsAbs (dbPath ) {
94+ dbPath = filepath .Join (dir , dbPath )
95+ }
96+
97+ // 初始化配置
98+ singleton .InitConfigFromPath (configFilePath )
99+ // 初始化时区和缓存
63100 singleton .InitTimezoneAndCache ()
64- singleton . InitDBFromPath ( dashboardCliParam . DatebaseLocation )
65- singleton .InitLocalizer ( )
101+ // 初始化数据库
102+ singleton .InitDBFromPath ( dbPath )
66103
67104 // 处理重置流量命令
68105 if dashboardCliParam .ResetTraffic {
106+ log .Println ("NG>> 正在重置所有服务器的流量统计..." )
107+ // 调用重置流量的功能
69108 resetAllServerTraffic ()
109+ log .Println ("NG>> 流量统计重置完成" )
70110 return
71111 }
72112
113+ // 这里是原来的main函数代码
114+ log .Println ("NG>> 服务启动中..." )
73115 initSystem ()
74-
75- // TODO 使用 cmux 在同一端口服务 HTTP 和 gRPC
76- singleton .CleanMonitorHistory ()
77- go rpc .ServeRPC (singleton .Conf .GRPCPort )
78- serviceSentinelDispatchBus := make (chan model.Monitor ) // 用于传递服务监控任务信息的channel
79- go rpc .DispatchTask (serviceSentinelDispatchBus )
80- go rpc .DispatchKeepalive ()
81- go singleton .AlertSentinelStart ()
82- singleton .NewServiceSentinel (serviceSentinelDispatchBus )
83- srv := controller .ServeWeb (singleton .Conf .HTTPPort )
84- go dispatchReportInfoTask ()
85- if err := graceful .Graceful (func () error {
86- return srv .ListenAndServe ()
87- }, func (c context.Context ) error {
88- log .Println ("NG>> Graceful::START" )
89- singleton .RecordTransferHourlyUsage ()
90- log .Println ("NG>> Graceful::END" )
91- srv .Shutdown (c )
92- return nil
93- }); err != nil {
94- log .Printf ("NG>> ERROR: %v" , err )
95- }
96116}
97117
98118func dispatchReportInfoTask () {
0 commit comments