main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  36. prg := &program{}
  37. s, err := service.New(prg, svcConfig)
  38. if err != nil {
  39. fmt.Println(err, "service.New() err", err)
  40. }
  41. if len(os.Args) > 1 {
  42. // 服务注册
  43. if os.Args[1] == "install" {
  44. err = s.Install()
  45. if err != nil {
  46. fmt.Println("install err", err)
  47. } else {
  48. fmt.Println("install success")
  49. }
  50. return
  51. }
  52. // 服务移除
  53. if os.Args[1] == "remove" {
  54. err = s.Uninstall()
  55. if err != nil {
  56. fmt.Println("Uninstall err3123123", err)
  57. } else {
  58. fmt.Println("Uninstall success")
  59. }
  60. return
  61. }
  62. }
  63. //启动服务
  64. _ = s.Run()
  65. }
  66. type program struct{}
  67. func (p *program) Start(s service.Service) error {
  68. go p.run()
  69. return nil
  70. }
  71. func (p *program) run() {
  72. // 代码写在这儿
  73. //路由初始化
  74. routersInit := routers.InitRouter()
  75. if setting.ServerSetting.GRFD == "1" { //格润富德定时任务
  76. api.CronGRFD()
  77. }
  78. readTimeout := setting.ServerSetting.ReadTimeout
  79. writeTimeout := setting.ServerSetting.WriteTimeout
  80. endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  81. maxHeaderBytes := 1 << 20
  82. server := &http.Server{
  83. Addr: endPoint,
  84. Handler: routersInit,
  85. ReadTimeout: readTimeout,
  86. WriteTimeout: writeTimeout,
  87. MaxHeaderBytes: maxHeaderBytes,
  88. }
  89. //if setting.CommSetting.PortName != "" {
  90. // go comm.OpenComm()
  91. //}
  92. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  93. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  94. }
  95. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  96. // 开启http/https服务
  97. if setting.ServerSetting.Https == 1 {
  98. log.Printf("[info] start https server listening port %s", httpsPort)
  99. _ = routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  100. } else if setting.ServerSetting.Https == 2 {
  101. log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort, endPoint)
  102. go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  103. _ = server.ListenAndServe()
  104. } else {
  105. log.Printf("[info] start https server listening port %s", endPoint)
  106. _ = server.ListenAndServe()
  107. }
  108. }
  109. func (p *program) Stop(s service.Service) error {
  110. fmt.Println("程序被关闭")
  111. return nil
  112. }