main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "gd_gateway/controllers/ctrl_v1"
  6. "gd_gateway/rpc_apis_v1"
  7. "gd_gateway/utils"
  8. "os"
  9. "strings"
  10. gconfig "gd_gateway/common.in/config"
  11. "gd_gateway/common.in/clinit"
  12. "gd_gateway/common.in/config"
  13. "gd_gateway/common.in/httper"
  14. "gd_gateway/common.in/logger"
  15. "gd_gateway/common.in/task"
  16. cutils "gd_gateway/common.in/utils"
  17. _ "gd_gateway/routers"
  18. "github.com/astaxie/beego"
  19. "github.com/astaxie/beego/orm"
  20. _ "github.com/go-sql-driver/mysql"
  21. )
  22. var (
  23. // 这里要改默认值
  24. configFile = flag.String("config", "/etc/gd_gateway/app.conf", "config file location")
  25. //configFile = flag.String("config", "./conf/app.conf", "config file location")
  26. version = flag.Bool("version", false, "config file location")
  27. GitCommit = "library-import"
  28. Version = "library-import"
  29. )
  30. func showVersion() {
  31. fmt.Println("Version: ", Version)
  32. fmt.Println("GitCommit:", GitCommit)
  33. }
  34. func prepare(projectName, runmode, key, logDir string, etcdAddrs []string) {
  35. discoveryType := beego.AppConfig.String("discovery_type")
  36. var conf *config.Configure
  37. utils.SetDiscoveryType(discoveryType)
  38. // 先行于读配置
  39. if discoveryType == "k8s" {
  40. conf = config.GetConfigForK8s()
  41. if conf == nil {
  42. fmt.Printf("get conf failed\n\n")
  43. os.Exit(1)
  44. }
  45. rpc_apis_v1.InitForK8s(conf)
  46. } else {
  47. clinit.InitEtcd(etcdAddrs)
  48. conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
  49. if conf == nil {
  50. fmt.Printf("get conf failed\n\n")
  51. os.Exit(1)
  52. }
  53. // 初始化所调用的微服务
  54. //rpc_apis.Init(etcdAddrs, conf)
  55. rpc_apis_v1.Init(etcdAddrs, conf)
  56. }
  57. appname := conf.Cgi.GdGateway.Name
  58. // 初始化logger
  59. ms, _ := conf.Cgi.GdGateway.Log.MaxSize.Int64()
  60. mb, _ := conf.Cgi.GdGateway.Log.MaxBackups.Int64()
  61. ma, _ := conf.Cgi.GdGateway.Log.MaxAge.Int64()
  62. maxSize := int(ms)
  63. maxBackups := int(mb)
  64. maxAge := int(ma)
  65. disableStacktrace := (conf.Cgi.GdGateway.Log.DisableStacktrace == "true")
  66. // 通用logger
  67. commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Cgi.GdGateway.Log.Level,
  68. maxSize, maxBackups, maxAge, disableStacktrace)
  69. // 单独设置
  70. accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), appname, conf.Cgi.GdGateway.Log.Level,
  71. maxSize, maxBackups, maxAge)
  72. // 设置需要使用logger的地方
  73. // common日志
  74. ctrl_v1.SetLogger(commonLogger)
  75. utils.SetLogger(commonLogger)
  76. // access日志
  77. task.SetLogger(accessLogger)
  78. // 打开第三方包调试开关
  79. if runmode != "prod" {
  80. orm.Debug = true
  81. //redis.Debug = true
  82. httper.Debug = true
  83. httper.DebugLog = commonLogger
  84. }
  85. go utils.LoopLevelDb()
  86. }
  87. func main() {
  88. flag.Parse()
  89. if *version {
  90. showVersion()
  91. }
  92. if err := beego.LoadAppConfig("ini", *configFile); err != nil {
  93. fmt.Printf("Fail to read file: %v\n\n", err)
  94. os.Exit(1)
  95. }
  96. //serveAddr := beego.AppConfig.String("serve_addr")
  97. //appname := beego.AppConfig.String("appname")
  98. //imageDir := beego.AppConfig.String("weather_image_dir")
  99. runmode := beego.AppConfig.String("runmode")
  100. logDir := beego.AppConfig.String("log_dir")
  101. etcdAddrs := strings.Split(beego.AppConfig.String("etcd_addrs"), ",")
  102. encryptKey := beego.AppConfig.String("encrypt_key")
  103. projectName := beego.AppConfig.String("project_name")
  104. swaggerDir := beego.AppConfig.String("swagger_dir")
  105. prepare(projectName, runmode, encryptKey, logDir, etcdAddrs)
  106. /*if imageDir == "" {
  107. beego.SetStaticPath("/image", "static")
  108. } else {
  109. beego.SetStaticPath("/image", imageDir)
  110. }*/
  111. if runmode == "dev" || runmode == "test" {
  112. beego.BConfig.WebConfig.DirectoryIndex = true
  113. if swaggerDir == "" {
  114. beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  115. } else {
  116. beego.BConfig.WebConfig.StaticDir["/swagger"] = swaggerDir
  117. }
  118. }
  119. go cutils.Free()
  120. cutils.HandleExitSignal()
  121. serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Cgi.GdGateway.ServiceName,gconfig.Conf.Cgi.GdGateway.ServicePort.String())
  122. beego.Run(serveAddr)
  123. }