db3f4cda612ef615991079e28cdd18fa338da6d3.svn-base 2.9 KB

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