main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. // @title Project access-control-monitor'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. "access-control-monitor/pb"
  9. "access-control-monitor/timetask"
  10. "context"
  11. "flag"
  12. "fmt"
  13. "log"
  14. "net/http"
  15. "os"
  16. "os/signal"
  17. "syscall"
  18. "time"
  19. "access-control-monitor/parser"
  20. "access-control-monitor/route"
  21. "github.com/gin-gonic/gin"
  22. "github.com/jaryhe/gopkgs/database"
  23. "github.com/jaryhe/gopkgs/tasker"
  24. swaggerFiles "github.com/swaggo/files"
  25. ginSwagger "github.com/swaggo/gin-swagger"
  26. )
  27. var (
  28. configFile = flag.String("config", "conf/app.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.MysqlHandler, parser.LoggerHandler)
  46. //parser.Register(parser.JwtHandler, parser.LoggerHandler)
  47. // 执行注册的处理函数
  48. parser.Handle()
  49. //v1.Init()
  50. timetask.Init()
  51. }
  52. func run() {
  53. // 新建engine
  54. engine := gin.New()
  55. handlers := gin.HandlersChain{}
  56. // 根据env_mode设置gin的运行模式
  57. switch parser.Conf.RunMode {
  58. case "test":
  59. gin.SetMode(gin.TestMode)
  60. case "prod":
  61. gin.SetMode(gin.ReleaseMode)
  62. case "dev":
  63. fallthrough
  64. default:
  65. gin.SetMode(gin.DebugMode)
  66. // 根据配置是否生成swagger
  67. engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  68. // 输出打印日志
  69. handlers = append(handlers, gin.Logger())
  70. }
  71. // 处理panic
  72. handlers = append(handlers, gin.Recovery())
  73. engine.Use(handlers...)
  74. // 设定路由
  75. route.SetupRoute(engine)
  76. // 建立rpc客户端
  77. conns := pb.SetupClients()
  78. for _, conn := range conns {
  79. defer conn.Close()
  80. }
  81. // 创建监听服务
  82. server := &http.Server{
  83. Addr: fmt.Sprintf("%s:%d", parser.Conf.AccessControlMonitor.ServiceIp,
  84. parser.Conf.AccessControlMonitor.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. log.Println("Shutdown server finished.")
  110. }
  111. func main() {
  112. defer func() {
  113. database.Close()
  114. }()
  115. flag.Parse()
  116. if *version {
  117. showVersion()
  118. os.Exit(1)
  119. }
  120. prepare(*configFile)
  121. //go utils.Free()
  122. run()
  123. return
  124. }