main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/kardianos/service"
  5. "github.com/kptyun/KPTCOMM/comm"
  6. "github.com/kptyun/KPTCOMM/mdns"
  7. "github.com/kptyun/KPTCOMM/models"
  8. "github.com/kptyun/KPTCOMM/pkg/setting"
  9. "os"
  10. "strings"
  11. "time"
  12. //"unsafe"
  13. )
  14. // @title 科湃腾TMRWatchComm
  15. // @version 2.0.0
  16. // @description kptyun_service
  17. // @contact.name 朱红宾
  18. // @contact.url https://www.dairyinfo.com.cn
  19. // @contact.email kptzhu@163.com
  20. type program struct{}
  21. func (p *program) Start(s service.Service) error {
  22. go p.run()
  23. return nil
  24. }
  25. func (p *program) run() {
  26. // 代码写在这儿
  27. comm.StopCharP = make(chan int, 1)
  28. comm.StopCharMqtt = make(chan int, 1)
  29. if setting.CommSetting.PortName != "" {
  30. go comm.ProtectRun()
  31. }
  32. if setting.MqttSetting.Enable {
  33. go comm.Mq_Consumer()
  34. }
  35. if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
  36. go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
  37. }
  38. //routersInit := routers.InitRouter()
  39. //readTimeout := setting.ServerSetting.ReadTimeout
  40. //writeTimeout := setting.ServerSetting.WriteTimeout
  41. //endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
  42. //maxHeaderBytes := 1 << 20
  43. //
  44. //server := &http.Server{
  45. // Addr: endPoint,
  46. // Handler: routersInit,
  47. // ReadTimeout: readTimeout,
  48. // WriteTimeout: writeTimeout,
  49. // MaxHeaderBytes: maxHeaderBytes,
  50. //}
  51. //
  52. //_ = server.ListenAndServe()
  53. }
  54. func (p *program) Stop(s service.Service) error {
  55. comm.StopCharP <- 1
  56. comm.StopCharMqtt <- 1
  57. <-time.After(time.Second * 1)
  58. return nil
  59. }
  60. /**
  61. * MAIN函数,程序入口
  62. */
  63. func main() {
  64. svcConfig := &service.Config{
  65. Name: "TMRWatchCOM", //服务显示名称
  66. DisplayName: "TMRWatchComm", //服务名称
  67. Description: "TMRWatch后台通讯服务", //服务描述
  68. }
  69. prg := &program{}
  70. s, _ := service.New(prg, svcConfig)
  71. if len(os.Args) > 1 {
  72. if os.Args[1] == "install" || os.Args[1] == "-install" || os.Args[1] == "-i" || os.Args[1] == "/install" {
  73. err := s.Install()
  74. if err != nil {
  75. fmt.Println(err)
  76. }
  77. return
  78. }
  79. if os.Args[1] == "remove" || os.Args[1] == "-uninstall" || os.Args[1] == "-remove" || os.Args[1] == "/uninstall" || os.Args[1] == "-u" {
  80. err := s.Uninstall()
  81. if err != nil {
  82. fmt.Println(err)
  83. }
  84. return
  85. }
  86. }
  87. //lock()
  88. setting.Setup("")
  89. //logging.Setup()
  90. models.Setup()
  91. _ = s.Run()
  92. }