package main import ( "context" "fmt" "net/http" _ "net/http/pprof" "os" "strings" "tmr-watch/conf/setting" ginHttp "tmr-watch/http" "tmr-watch/http/handle/api" "tmr-watch/mdns" "tmr-watch/models" "tmr-watch/pkg/logging" "github.com/kardianos/service" ) func main() { setting.Setup() models.Setup() logging.Setup() svcConfig := &service.Config{ Name: setting.ServerSetting.ServerName, //服务显示名称 DisplayName: setting.ServerSetting.DisplayName, //服务名称 Description: setting.ServerSetting.Description, //服务描述 } prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { fmt.Println(err, "service.New() err", err) } if len(os.Args) > 1 { switch os.Args[1] { case "install": err = s.Install() // 服务注册 case "remove": err = s.Uninstall() } if err != nil { fmt.Println("err", err) } else { fmt.Println("success") } return } _ = s.Run() //启动服务 } type program struct{} func (p *program) Start(s service.Service) error { go p.run() return nil } func (p *program) run() { //路由初始化 httpServer := ginHttp.NewServer(ginHttp.SetRouteOption()) go api.CronScheduled(context.TODO()) if setting.ServerSetting.GRFD == "1" { //格润富德定时任务 api.CronGRFD() } server := &http.Server{ Addr: fmt.Sprintf(":%d", setting.ServerSetting.HttpPort), Handler: httpServer, ReadTimeout: setting.ServerSetting.ReadTimeout, WriteTimeout: setting.ServerSetting.WriteTimeout, MaxHeaderBytes: 1 << 20, } if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" { go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport) } httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort) // 开启http/https服务 if setting.ServerSetting.Https == 1 { logging.Info("[info] start https server listening port %s", httpsPort) _ = httpServer.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath) } else if setting.ServerSetting.Https == 2 { logging.Printf("[info] start https & http server listening port https: %s, http: %s", httpsPort, server.Addr) go httpServer.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath) _ = server.ListenAndServe() } else { logging.Printf("[info] start https server listening port %s", server.Addr) _ = server.ListenAndServe() } } func (p *program) Stop(s service.Service) error { fmt.Println("程序被关闭") return nil }