nezha/cmd/dashboard/main.go

83 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-12-05 09:36:58 -05:00
package main
import (
"context"
2023-11-02 09:06:34 -04:00
"fmt"
2022-04-27 11:51:45 -04:00
"log"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/cmd/dashboard/controller"
"github.com/naiba/nezha/cmd/dashboard/rpc"
"github.com/naiba/nezha/model"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2022-04-12 01:16:33 -04:00
"github.com/ory/graceful"
2023-11-02 09:06:34 -04:00
flag "github.com/spf13/pflag"
)
type DashboardCliParam struct {
Version bool // 当前版本号
ConfigFile string // 配置文件路径
DatebaseLocation string // Sqlite3 数据库文件路径
}
var (
dashboardCliParam DashboardCliParam
2019-12-05 09:36:58 -05:00
)
2019-12-08 03:59:58 -05:00
func init() {
2023-11-02 09:06:34 -04:00
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.BoolVarP(&dashboardCliParam.Version, "version", "v", false, "查看当前版本号")
flag.StringVarP(&dashboardCliParam.ConfigFile, "config", "c", "data/config.yaml", "配置文件路径")
flag.StringVar(&dashboardCliParam.DatebaseLocation, "db", "data/sqlite.db", "Sqlite3数据库文件路径")
flag.Parse()
// 初始化 dao 包
2023-11-02 09:06:34 -04:00
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
2022-10-12 11:06:25 -04:00
singleton.InitTimezoneAndCache()
2023-11-02 09:06:34 -04:00
singleton.InitDBFromPath(dashboardCliParam.DatebaseLocation)
2022-04-27 11:51:45 -04:00
singleton.InitLocalizer()
initSystem()
2019-12-08 10:18:29 -05:00
}
func initSystem() {
2022-04-12 01:16:33 -04:00
// 启动 singleton 包下的所有服务
singleton.LoadSingleton()
2022-04-11 10:51:02 -04:00
// 每天的3:30 对 监控记录 和 流量记录 进行清理
2022-04-12 01:16:33 -04:00
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanMonitorHistory); err != nil {
2021-07-18 22:37:12 -04:00
panic(err)
}
2022-04-11 10:51:02 -04:00
// 每小时对流量记录进行打点
2022-04-12 01:16:33 -04:00
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
2021-07-18 22:37:12 -04:00
panic(err)
}
}
2019-12-08 03:59:58 -05:00
func main() {
2023-11-02 09:06:34 -04:00
if dashboardCliParam.Version {
fmt.Println(singleton.Version)
return
}
// TODO 使用 cmux 在同一端口服务 HTTP 和 gRPC
2022-04-12 01:16:33 -04:00
singleton.CleanMonitorHistory()
2022-01-08 22:54:14 -05:00
go rpc.ServeRPC(singleton.Conf.GRPCPort)
2022-04-11 10:51:02 -04:00
serviceSentinelDispatchBus := make(chan model.Monitor) // 用于传递服务监控任务信息的channel
go rpc.DispatchTask(serviceSentinelDispatchBus)
go rpc.DispatchKeepalive()
2022-01-08 22:54:14 -05:00
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
2023-11-02 09:06:34 -04:00
if err := graceful.Graceful(func() error {
return srv.ListenAndServe()
}, func(c context.Context) error {
2021-11-06 06:40:29 -04:00
log.Println("NEZHA>> Graceful::START")
2022-04-12 01:16:33 -04:00
singleton.RecordTransferHourlyUsage()
2021-11-06 06:40:29 -04:00
log.Println("NEZHA>> Graceful::END")
srv.Shutdown(c)
return nil
2023-11-02 09:06:34 -04:00
}); err != nil {
log.Printf("NEZHA>> ERROR: %v", err)
}
2019-12-05 09:36:58 -05:00
}