main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "strings"
  9. // logrus "github.com/sirupsen/logrus"
  10. "github.com/kardianos/service"
  11. "kpt.xdmy/apiserver/config"
  12. "kpt.xdmy/apiserver/dao"
  13. "kpt.xdmy/apiserver/routers"
  14. "kpt.xdmy/apiserver/routers/api"
  15. pservice "kpt.xdmy/apiserver/service"
  16. "kpt.xdmy/middleware/comm"
  17. "kpt.xdmy/middleware/mdns"
  18. plog "kpt.xdmy/pkg/log"
  19. "kpt.xdmy/pkg/logging"
  20. "kpt.xdmy/pkg/setting"
  21. "kpt.xdmy/pkg/util"
  22. //"unsafe"
  23. )
  24. // @title 科湃腾Go语言后台API
  25. // @version 1.1.2
  26. // @description kptyun_service
  27. // @contact.name 朱红宾
  28. // @contact.url https://www.dairyinfo.com.cn
  29. // @contact.email kptzhu@163.com
  30. // @host
  31. // @BasePath /
  32. func main() {
  33. var path string
  34. path = "/Users/desire/kptdev/xdmy_mod/cmd/"
  35. flag.Parse()
  36. setting.Setup(path)
  37. dao.Setup()
  38. routers.InitRouter()
  39. logging.Setup()
  40. config.Init(path)
  41. plog.Init()
  42. svc := pservice.New(config.Conf)
  43. routers.Init(svc)
  44. api.Init(svc)
  45. svcConfig := &service.Config{
  46. Name: setting.ServerSetting.ServerName, //服务显示名称
  47. DisplayName: setting.ServerSetting.DisplayName, //服务名称
  48. Description: setting.ServerSetting.Description, //服务描述
  49. }
  50. prg := &program{}
  51. s, err := service.New(prg, svcConfig)
  52. if err != nil {
  53. fmt.Println(err, "service.New() err", err)
  54. }
  55. if len(os.Args) > 1 {
  56. if os.Args[1] == "install" {
  57. err = s.Install()
  58. if err != nil {
  59. fmt.Println("install err", err)
  60. } else {
  61. fmt.Println("install success")
  62. }
  63. return
  64. }
  65. if os.Args[1] == "remove" {
  66. err = s.Uninstall()
  67. if err != nil {
  68. fmt.Println("Uninstall err", err)
  69. } else {
  70. fmt.Println("Uninstall success")
  71. }
  72. return
  73. }
  74. }
  75. s.Run()
  76. }
  77. type program struct{}
  78. func (p *program) Start(s service.Service) error {
  79. go p.run()
  80. return nil
  81. }
  82. func (p *program) run() {
  83. // 代码写在这儿
  84. // 邮件定时任务
  85. if setting.ServerSetting.MailStart == "1" {
  86. util.CronMail()
  87. }
  88. // 企业微信定时任务
  89. if setting.ServerSetting.WXworkStart == "1" {
  90. util.CronWXwork()
  91. }
  92. // 系统定时任务
  93. util.CronServer()
  94. // 现代牧业eas 系统对接初始化
  95. util.InitWebS(setting.ServerSetting.EasName, setting.ServerSetting.EasPassword, setting.ServerSetting.EasUrl)
  96. routersInit := routers.InitRouter()
  97. readTimeout := setting.ServerSetting.ReadTimeout
  98. writeTimeout := setting.ServerSetting.WriteTimeout
  99. endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  100. maxHeaderBytes := 1 << 20
  101. server := &http.Server{
  102. Addr: endPoint,
  103. Handler: routersInit,
  104. ReadTimeout: readTimeout,
  105. WriteTimeout: writeTimeout,
  106. MaxHeaderBytes: maxHeaderBytes,
  107. }
  108. if setting.CommSetting.PortName != "" {
  109. go comm.OpenComm()
  110. }
  111. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  112. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  113. }
  114. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  115. if setting.ServerSetting.Https == 1 {
  116. log.Printf("[info] start https server listening port %s", httpsPort)
  117. _ = routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  118. } else if setting.ServerSetting.Https == 2 {
  119. log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort, endPoint)
  120. go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  121. _ = server.ListenAndServe()
  122. } else {
  123. log.Printf("[info] start http server listening port %s", endPoint)
  124. e := server.ListenAndServe()
  125. log.Default().Printf("net http listen error: %v", e)
  126. }
  127. }
  128. func (p *program) Stop(s service.Service) error {
  129. fmt.Println("程序被关闭")
  130. return nil
  131. }