123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- // @title Project xingjia-management-gateway's APIs
- // @version 1.1
- // @description This is a gateway server. On the page, you can go to do testing for every API.
- package main
- import (
- "context"
- "flag"
- "fmt"
- "log"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- "xingjia-management-gateway/impl/v1/local_objs"
- "xingjia-management-gateway/impl/v1/user"
- "xingjia-management-gateway/parser"
- "xingjia-management-gateway/route"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/tasker"
- "github.com/gin-gonic/gin"
- swaggerFiles "github.com/swaggo/files"
- ginSwagger "github.com/swaggo/gin-swagger"
- )
- var (
- configFile = flag.String("config", "conf/common.yaml", "config file location")
- version = flag.Bool("version", false, "print the version")
- GitCommit = "library-import"
- Version = "library-import"
- )
- func showVersion() {
- fmt.Println("Version: ", Version)
- fmt.Println("GitCommit:", GitCommit)
- }
- func prepare(filename string) {
- // 加载配置
- err := parser.LoadConfig(filename)
- if err != nil {
- fmt.Printf("get conf failed, err: %+v\n\n", err)
- os.Exit(1)
- }
- // 注册处理函数
- parser.Register(parser.JwtHandler, parser.MysqlHandler, parser.LoggerHandler)
- //parser.Register(parser.JwtHandler, parser.LoggerHandler)
- // 执行注册的处理函数
- parser.Handle()
- }
- func run() {
- // 新建engine
- engine := gin.New()
- handlers := gin.HandlersChain{}
- // 根据env_mode设置gin的运行模式
- switch parser.Conf.RunMode {
- case "test":
- gin.SetMode(gin.TestMode)
- case "prod":
- gin.SetMode(gin.ReleaseMode)
- case "dev":
- fallthrough
- default:
- gin.SetMode(gin.DebugMode)
- // 根据配置是否生成swagger
- engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
- // 输出打印日志
- handlers = append(handlers, gin.Logger())
- }
- // 处理panic
- handlers = append(handlers, gin.Recovery())
- engine.Use(handlers...)
- // 设定路由
- route.SetupRoute(engine)
- // 创建监听服务
- server := &http.Server{
- Addr: fmt.Sprintf("%s:%d", parser.Conf.XingjiaManagementGateway.ServiceIp,
- parser.Conf.XingjiaManagementGateway.ServicePort),
- Handler: engine,
- }
- user.InitUser()
- go local_objs.DelTask()
- // 开始监听
- log.Printf("Listening and serving HTTP on %s\n", server.Addr)
- go func() {
- if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
- }
- }()
- // 优雅关闭服务器
- sigChan := make(chan os.Signal, 1)
- // 捕获信号
- signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
- sigValue := <-sigChan
- log.Printf("Got a signal:%v", sigValue)
- // 让tasker安全退出
- tasker.SignalNotify(sigValue)
- // 不管什么行为,都等待5秒退出
- log.Println("Start to shutdown server...")
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if err := server.Shutdown(ctx); err != nil {
- log.Printf("Shutdown server failed. err: %s", err.Error())
- }
- log.Println("Shutdown server finished.")
- }
- func main() {
- defer func() {
- database.Close()
- }()
- flag.Parse()
- if *version {
- showVersion()
- os.Exit(1)
- }
- prepare(*configFile)
- //go utils.Free()
- run()
- return
- }
|