main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. _ "eta/eta_pub/routers"
  4. "os"
  5. "github.com/beego/beego/v2/server/web"
  6. "github.com/kardianos/service"
  7. )
  8. var logger = service.ConsoleLogger
  9. type program struct{}
  10. func (p *program) Start(s service.Service) error {
  11. go p.run()
  12. return nil
  13. }
  14. func (p *program) run() {
  15. web.Run()
  16. // 运行逻辑
  17. }
  18. func (p *program) Stop(s service.Service) error {
  19. return nil
  20. }
  21. func main() {
  22. svcConfig := &service.Config{
  23. Name: "Trim", //服务显示名称
  24. DisplayName: "Trim Service", //服务名称
  25. Description: "微服务", //服务描述
  26. }
  27. prg := &program{}
  28. s, err := service.New(prg, svcConfig)
  29. if err != nil {
  30. logger.Error(err)
  31. }
  32. if len(os.Args) > 1 {
  33. switch os.Args[1] {
  34. case "install":
  35. s.Install()
  36. logger.Info("服务安装成功!")
  37. s.Start()
  38. logger.Info("服务启动成功!")
  39. break
  40. case "start":
  41. s.Start()
  42. logger.Info("服务启动成功!")
  43. break
  44. case "stop":
  45. s.Stop()
  46. logger.Info("服务关闭成功!")
  47. break
  48. case "restart":
  49. s.Stop()
  50. logger.Info("服务关闭成功!")
  51. s.Start()
  52. logger.Info("服务启动成功!")
  53. break
  54. case "remove":
  55. s.Stop()
  56. logger.Info("服务关闭成功!")
  57. s.Uninstall()
  58. logger.Info("服务卸载成功!")
  59. break
  60. }
  61. return
  62. }
  63. err = s.Run()
  64. if err != nil {
  65. logger.Error(err)
  66. }
  67. if web.BConfig.RunMode == "dev" {
  68. web.BConfig.WebConfig.DirectoryIndex = true
  69. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  70. }
  71. web.Run()
  72. }