package alert import ( "context" "fmt" "log" "net/http" "os" "os/signal" "time" "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/config/source/file" "github.com/go-admin-team/go-admin-core/sdk" "github.com/go-admin-team/go-admin-core/sdk/api" "github.com/go-admin-team/go-admin-core/sdk/config" "github.com/go-admin-team/go-admin-core/sdk/pkg" "github.com/spf13/cobra" "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" "go-admin/app/alert/router" "go-admin/common/database" common "go-admin/common/middleware" mhandler "go-admin/common/middleware/handler" extConfig "go-admin/config" "go-admin/handler" ) var ( configYml string apiCheck bool StartCmd = &cobra.Command{ Use: "alert", Short: "Start Alert server", Example: "go-admin alert -c config/settings.yml", SilenceUsage: true, PreRun: func(cmd *cobra.Command, args []string) { setup() }, RunE: func(cmd *cobra.Command, args []string) error { return run() }, } ) var AppRouters = make([]func(), 0) func init() { StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file") AppRouters = append(AppRouters, router.InitRouter) } func setup() { config.ExtendConfig = &extConfig.ExtConfig //1. 读取配置 config.Setup( file.NewSource(file.WithPath(configYml)), database.Setup, // storage.Setup, // olap.Setup, ) usageStr := `starting alert server...` log.Println(usageStr) } func run() error { handler.SetAlertManager() if config.ApplicationConfig.Mode == pkg.ModeProd.String() { // gin.SetMode(gin.ReleaseMode) } initRouter() //暴露metric数据接口 for _, f := range AppRouters { f() } srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", config.ApplicationConfig.Host, config.ApplicationConfig.Port), Handler: sdk.Runtime.GetEngine(), } go func() { // 服务连接 if config.SslConfig.Enable { if err := srv.ListenAndServeTLS(config.SslConfig.Pem, config.SslConfig.KeyStr); err != nil && err != http.ErrServerClosed { log.Fatal("listen: ", err) } } else { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatal("listen: ", err) } } }() fmt.Println(pkg.Green("Server run at:")) fmt.Printf("- Local: http://localhost:%d/ \r\n", config.ApplicationConfig.Port) fmt.Printf("- Network: http://%s:%d/ \r\n", pkg.GetLocaHonst(), config.ApplicationConfig.Port) //TODO:10s一次的检测,设定可配置 // productor ticker := time.Tick(time.Duration(handler.GetAlertManager().Interval) * time.Second) go func() { handler.GetAlertManager().PolicyProducter() for { select { case <-ticker: fmt.Println("alert policy producting....") handler.GetAlertManager().PolicyProducter() } } }() go func() { handler.GetAlertManager().PolicyConsumer() }() defer handler.CloseAlertManager() fmt.Println(pkg.Red("Start alert Service")) quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt) <-quit ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() fmt.Printf("%s Shutdown Server ... \r\n", pkg.GetCurrentTimeStr()) fmt.Printf("%s Shutdown alert server ... \r\n", pkg.GetCurrentTimeStr()) log.Println("Alert server exiting") if err := srv.Shutdown(ctx); err != nil { log.Fatal("Server Shutdown:", err) } log.Println("Server exiting") return nil } func initRouter() { var r *gin.Engine h := sdk.Runtime.GetEngine() if h == nil { h = gin.New() sdk.Runtime.SetEngine(h) } switch h := h.(type) { case *gin.Engine: r = h default: log.Fatal("not support other engine") os.Exit(-1) } if config.ApplicationConfig.Mode == "dev" { pprof.Register(r) } if config.SslConfig.Enable { r.Use(mhandler.TlsHandler()) } r.Use(otelgin.Middleware("observe-server")) // r.Use(middleware.Metrics()) r.Use(common.Sentinel()). Use(common.RequestId(pkg.TrafficKey)). Use(api.SetRequestLogger) common.InitMiddleware(r) }