main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "./comm"
  9. "./mdns"
  10. "./models"
  11. "./pkg/logging"
  12. "./pkg/setting"
  13. "./pkg/util"
  14. "./routers"
  15. "github.com/kardianos/service"
  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. if os.Args[1] == "install" {
  42. err = s.Install()
  43. if err != nil {
  44. fmt.Println("install err", err)
  45. } else {
  46. fmt.Println("install success")
  47. }
  48. return
  49. }
  50. if os.Args[1] == "remove" {
  51. err = s.Uninstall()
  52. if err != nil {
  53. fmt.Println("Uninstall err", err)
  54. } else {
  55. fmt.Println("Uninstall success")
  56. }
  57. return
  58. }
  59. }
  60. _ = s.Run()
  61. }
  62. type program struct{}
  63. func (p *program) Start(s service.Service) error {
  64. go p.run()
  65. return nil
  66. }
  67. func (p *program) run() {
  68. // 代码写在这儿
  69. // 邮件定时任务
  70. if setting.ServerSetting.MailStart == "1" {
  71. util.CronMail()
  72. }
  73. // 企业微信定时任务
  74. if setting.ServerSetting.WXworkStart == "1" {
  75. util.CronWXwork()
  76. }
  77. // 系统定时任务
  78. util.CronServer()
  79. // 现代牧业eas 系统对接初始化
  80. util.InitWebS(setting.ServerSetting.EasName, setting.ServerSetting.EasPassword, setting.ServerSetting.EasUrl)
  81. routersInit := routers.InitRouter()
  82. readTimeout := setting.ServerSetting.ReadTimeout
  83. writeTimeout := setting.ServerSetting.WriteTimeout
  84. endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  85. maxHeaderBytes := 1 << 20
  86. server := &http.Server{
  87. Addr: endPoint,
  88. Handler: routersInit,
  89. ReadTimeout: readTimeout,
  90. WriteTimeout: writeTimeout,
  91. MaxHeaderBytes: maxHeaderBytes,
  92. }
  93. if setting.CommSetting.PortName != "" {
  94. go comm.OpenComm()
  95. }
  96. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  97. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  98. }
  99. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  100. if setting.ServerSetting.Https == 1 {
  101. log.Printf("[info] start https server listening port %s", httpsPort)
  102. _ = routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  103. } else if setting.ServerSetting.Https == 2 {
  104. log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort, endPoint)
  105. go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  106. _ = server.ListenAndServe()
  107. } else {
  108. log.Printf("[info] start http server listening port %s", endPoint)
  109. _ = server.ListenAndServe()
  110. }
  111. }
  112. func (p *program) Stop(s service.Service) error {
  113. fmt.Println("程序被关闭")
  114. return nil
  115. }