main.go 3.8 KB

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