main.go 3.3 KB

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