main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. // @title Project property-household-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. "property-household-gateway/utils"
  16. "syscall"
  17. "time"
  18. "property-household-gateway/parser"
  19. "property-household-gateway/pb"
  20. "property-household-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.RedisHandler, parser.LoggerHandler, parser.EtcdHandler)
  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. // 建立rpc客户端
  75. conns := pb.SetupClients()
  76. for _, conn := range conns {
  77. defer conn.Close()
  78. }
  79. ctxObjTask, cancelObjTask := context.WithCancel(context.Background())
  80. go utils.ObjTask(ctxObjTask)
  81. // 创建监听服务
  82. server := &http.Server{
  83. Addr: fmt.Sprintf("%s:%d", parser.Conf.PropertyHouseholdGateway.ServiceIp,
  84. parser.Conf.PropertyHouseholdGateway.ServicePort),
  85. Handler: engine,
  86. }
  87. // 开始监听
  88. log.Printf("Listening and serving HTTP on %s\n", server.Addr)
  89. go func() {
  90. if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  91. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  92. }
  93. }()
  94. // 优雅关闭服务器
  95. sigChan := make(chan os.Signal, 1)
  96. // 捕获信号
  97. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  98. sigValue := <-sigChan
  99. log.Printf("Got a signal:%v", sigValue)
  100. // 让tasker安全退出
  101. tasker.SignalNotify(sigValue)
  102. // 不管什么行为,都等待5秒退出
  103. log.Println("Start to shutdown server...")
  104. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  105. defer cancel()
  106. if err := server.Shutdown(ctx); err != nil {
  107. log.Printf("Shutdown server failed. err: %s", err.Error())
  108. }
  109. cancelObjTask()
  110. log.Println("Shutdown server finished.")
  111. }
  112. func main() {
  113. defer func() {
  114. database.Close()
  115. }()
  116. flag.Parse()
  117. if *version {
  118. showVersion()
  119. os.Exit(1)
  120. }
  121. prepare(*configFile)
  122. //go utils.Free()
  123. run()
  124. return
  125. }