main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. _ "net/http/pprof"
  7. "os"
  8. "strings"
  9. "./models"
  10. "./pkg/logging"
  11. "./pkg/setting"
  12. "./routers"
  13. "./routers/api"
  14. "github.com/kardianos/service"
  15. "github.com/kptyun/KPTCOMM/mdns"
  16. //"unsafe"
  17. )
  18. // sss
  19. // @title 科湃腾Go语言后台API
  20. // @version 1.1.2
  21. // @description kptyun_service
  22. // @contact.name 朱红宾
  23. // @contact.url https://www.dairyinfo.com.cn
  24. // @contact.email kptzhu@163.com
  25. // @host
  26. // @BasePath /
  27. func main() {
  28. setting.Setup("")
  29. models.Setup()
  30. logging.Setup()
  31. svcConfig := &service.Config{
  32. Name: setting.ServerSetting.ServerName, //服务显示名称
  33. DisplayName: setting.ServerSetting.DisplayName, //服务名称
  34. Description: setting.ServerSetting.Description, //服务描述
  35. // Dependencies: []string{"TMRWatchDB"},
  36. }
  37. prg := &program{}
  38. s, err := service.New(prg, svcConfig)
  39. if err != nil {
  40. fmt.Println(err, "service.New() err", err)
  41. }
  42. if len(os.Args) > 1 {
  43. // 服务注册
  44. if os.Args[1] == "install" {
  45. err = s.Install()
  46. if err != nil {
  47. // ceshi
  48. fmt.Println("install err", err)
  49. } else {
  50. fmt.Println("install success")
  51. }
  52. return
  53. }
  54. // 服务移除
  55. if os.Args[1] == "remove" {
  56. err = s.Uninstall()
  57. if err != nil {
  58. fmt.Println("Uninstall err", err)
  59. } else {
  60. fmt.Println("Uninstall success")
  61. }
  62. return
  63. }
  64. }
  65. //启动服务
  66. _ = s.Run()
  67. }
  68. type program struct{}
  69. func (p *program) Start(s service.Service) error {
  70. go p.run()
  71. return nil
  72. }
  73. func (p *program) run() {
  74. // 代码写在这儿
  75. //路由初始化
  76. routersInit := routers.InitRouter()
  77. if setting.ServerSetting.GRFD == "1" { //格润富德定时任务
  78. api.CronGRFD()
  79. }
  80. readTimeout := setting.ServerSetting.ReadTimeout
  81. writeTimeout := setting.ServerSetting.WriteTimeout
  82. endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  83. maxHeaderBytes := 1 << 20
  84. server := &http.Server{
  85. Addr: endPoint,
  86. Handler: routersInit,
  87. ReadTimeout: readTimeout,
  88. WriteTimeout: writeTimeout,
  89. MaxHeaderBytes: maxHeaderBytes,
  90. }
  91. //if setting.CommSetting.PortName != "" {
  92. // go comm.OpenComm()
  93. //}
  94. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  95. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  96. }
  97. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  98. // 开启http/https服务
  99. if setting.ServerSetting.Https == 1 {
  100. log.Printf("[info] start https server listening port %s", httpsPort)
  101. _ = routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  102. } else if setting.ServerSetting.Https == 2 {
  103. log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort, endPoint)
  104. go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  105. _ = server.ListenAndServe()
  106. } else {
  107. log.Printf("[info] start https server listening port %s", endPoint)
  108. _ = server.ListenAndServe()
  109. }
  110. }
  111. func (p *program) Stop(s service.Service) error {
  112. fmt.Println("程序被关闭")
  113. return nil
  114. }