main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. _ "net/http/pprof"
  7. "os"
  8. "strings"
  9. "tmr-watch/conf/setting"
  10. ginHttp "tmr-watch/http"
  11. "tmr-watch/http/handle/api"
  12. "tmr-watch/mdns"
  13. "tmr-watch/models"
  14. "tmr-watch/pkg/logging"
  15. "github.com/kardianos/service"
  16. )
  17. func main() {
  18. setting.Setup()
  19. models.Setup()
  20. logging.Setup()
  21. svcConfig := &service.Config{
  22. Name: setting.ServerSetting.ServerName, //服务显示名称
  23. DisplayName: setting.ServerSetting.DisplayName, //服务名称
  24. Description: setting.ServerSetting.Description, //服务描述
  25. }
  26. prg := &program{}
  27. s, err := service.New(prg, svcConfig)
  28. if err != nil {
  29. fmt.Println(err, "service.New() err", err)
  30. }
  31. if len(os.Args) > 1 {
  32. switch os.Args[1] {
  33. case "install":
  34. err = s.Install() // 服务注册
  35. case "remove":
  36. err = s.Uninstall()
  37. }
  38. if err != nil {
  39. fmt.Println("err", err)
  40. } else {
  41. fmt.Println("success")
  42. }
  43. return
  44. }
  45. _ = s.Run() //启动服务
  46. }
  47. type program struct{}
  48. func (p *program) Start(s service.Service) error {
  49. go p.run()
  50. return nil
  51. }
  52. func (p *program) run() {
  53. //路由初始化
  54. httpServer := ginHttp.NewServer(ginHttp.SetRouteOption())
  55. go api.CronScheduled(context.TODO())
  56. if setting.ServerSetting.GRFD == "1" { //格润富德定时任务
  57. api.CronGRFD()
  58. }
  59. server := &http.Server{
  60. Addr: fmt.Sprintf(":%d", setting.ServerSetting.HttpPort),
  61. Handler: httpServer,
  62. ReadTimeout: setting.ServerSetting.ReadTimeout,
  63. WriteTimeout: setting.ServerSetting.WriteTimeout,
  64. MaxHeaderBytes: 1 << 20,
  65. }
  66. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  67. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  68. }
  69. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  70. // 开启http/https服务
  71. if setting.ServerSetting.Https == 1 {
  72. logging.Info("[info] start https server listening port %s", httpsPort)
  73. _ = httpServer.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  74. } else if setting.ServerSetting.Https == 2 {
  75. logging.Printf("[info] start https & http server listening port https: %s, http: %s", httpsPort, server.Addr)
  76. go httpServer.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  77. _ = server.ListenAndServe()
  78. } else {
  79. logging.Printf("[info] start https server listening port %s", server.Addr)
  80. _ = server.ListenAndServe()
  81. }
  82. }
  83. func (p *program) Stop(s service.Service) error {
  84. fmt.Println("程序被关闭")
  85. return nil
  86. }