main.go 3.0 KB

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