main.go 3.5 KB

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