main.go 3.1 KB

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