7b2fea4db9a032e14c2e59c7ea2430fceeb38659.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/comm"
  11. "github.com/kptyun/KPTCOMM/mdns"
  12. "log"
  13. "net/http"
  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. 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. routersInit := routers.InitRouter()
  70. if setting.ServerSetting.GRFD == "1"{ //格润富德定时任务
  71. api.CronGRFD()
  72. }
  73. readTimeout := setting.ServerSetting.ReadTimeout
  74. writeTimeout := setting.ServerSetting.WriteTimeout
  75. endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  76. maxHeaderBytes := 1 << 20
  77. server := &http.Server{
  78. Addr: endPoint,
  79. Handler: routersInit,
  80. ReadTimeout: readTimeout,
  81. WriteTimeout: writeTimeout,
  82. MaxHeaderBytes: maxHeaderBytes,
  83. //fmt.Println(unsafe.Alignof(unintptr(0)))
  84. //log.Printf("[info] start http server listening %s", endPoint)
  85. }
  86. if setting.CommSetting.PortName != "" {
  87. go comm.OpenComm()
  88. }
  89. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  90. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  91. }
  92. httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
  93. if setting.ServerSetting.Https == 1{
  94. log.Printf("[info] start https server listening port %s", httpsPort)
  95. _ =routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  96. }else if setting.ServerSetting.Https == 2{
  97. log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort,endPoint)
  98. go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
  99. _ = server.ListenAndServe()
  100. }else {
  101. log.Printf("[info] start https server listening port %s", endPoint)
  102. _ = server.ListenAndServe()
  103. }
  104. }
  105. func (p *program) Stop(s service.Service) error {
  106. fmt.Println("程序被关闭")
  107. return nil
  108. }