main.go 3.0 KB

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