main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. // @title Project xingjia-management-gateway's APIs
  4. // @version 1.1
  5. // @description This is a gateway server. On the page, you can go to do testing for every API.
  6. package main
  7. import (
  8. "context"
  9. "flag"
  10. "fmt"
  11. "log"
  12. "net/http"
  13. "os"
  14. "os/signal"
  15. "syscall"
  16. "time"
  17. "xingjia-management-gateway/impl/v1/local_objs"
  18. "xingjia-management-gateway/impl/v1/user"
  19. "xingjia-management-gateway/parser"
  20. "xingjia-management-gateway/route"
  21. "git.getensh.com/common/gopkgs/database"
  22. "git.getensh.com/common/gopkgs/tasker"
  23. "github.com/gin-gonic/gin"
  24. swaggerFiles "github.com/swaggo/files"
  25. ginSwagger "github.com/swaggo/gin-swagger"
  26. )
  27. var (
  28. configFile = flag.String("config", "conf/common.yaml", "config file location")
  29. version = flag.Bool("version", false, "print the version")
  30. GitCommit = "library-import"
  31. Version = "library-import"
  32. )
  33. func showVersion() {
  34. fmt.Println("Version: ", Version)
  35. fmt.Println("GitCommit:", GitCommit)
  36. }
  37. func prepare(filename string) {
  38. // 加载配置
  39. err := parser.LoadConfig(filename)
  40. if err != nil {
  41. fmt.Printf("get conf failed, err: %+v\n\n", err)
  42. os.Exit(1)
  43. }
  44. // 注册处理函数
  45. parser.Register(parser.JwtHandler, parser.MysqlHandler, parser.LoggerHandler)
  46. //parser.Register(parser.JwtHandler, parser.LoggerHandler)
  47. // 执行注册的处理函数
  48. parser.Handle()
  49. }
  50. func run() {
  51. // 新建engine
  52. engine := gin.New()
  53. handlers := gin.HandlersChain{}
  54. // 根据env_mode设置gin的运行模式
  55. switch parser.Conf.RunMode {
  56. case "test":
  57. gin.SetMode(gin.TestMode)
  58. case "prod":
  59. gin.SetMode(gin.ReleaseMode)
  60. case "dev":
  61. fallthrough
  62. default:
  63. gin.SetMode(gin.DebugMode)
  64. // 根据配置是否生成swagger
  65. engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  66. // 输出打印日志
  67. handlers = append(handlers, gin.Logger())
  68. }
  69. // 处理panic
  70. handlers = append(handlers, gin.Recovery())
  71. engine.Use(handlers...)
  72. // 设定路由
  73. route.SetupRoute(engine)
  74. // 创建监听服务
  75. server := &http.Server{
  76. Addr: fmt.Sprintf("%s:%d", parser.Conf.XingjiaManagementGateway.ServiceIp,
  77. parser.Conf.XingjiaManagementGateway.ServicePort),
  78. Handler: engine,
  79. }
  80. user.InitUser()
  81. go local_objs.DelTask()
  82. // 开始监听
  83. log.Printf("Listening and serving HTTP on %s\n", server.Addr)
  84. go func() {
  85. if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  86. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  87. }
  88. }()
  89. // 优雅关闭服务器
  90. sigChan := make(chan os.Signal, 1)
  91. // 捕获信号
  92. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  93. sigValue := <-sigChan
  94. log.Printf("Got a signal:%v", sigValue)
  95. // 让tasker安全退出
  96. tasker.SignalNotify(sigValue)
  97. // 不管什么行为,都等待5秒退出
  98. log.Println("Start to shutdown server...")
  99. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  100. defer cancel()
  101. if err := server.Shutdown(ctx); err != nil {
  102. log.Printf("Shutdown server failed. err: %s", err.Error())
  103. }
  104. log.Println("Shutdown server finished.")
  105. }
  106. func main() {
  107. defer func() {
  108. database.Close()
  109. }()
  110. flag.Parse()
  111. if *version {
  112. showVersion()
  113. os.Exit(1)
  114. }
  115. prepare(*configFile)
  116. //go utils.Free()
  117. run()
  118. return
  119. }