main.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package main
  2. import (
  3. "gd_management_gateway/controllers/ctrl_v1"
  4. _ "gd_management_gateway/routers"
  5. "gd_management_gateway/rpc_apis"
  6. "flag"
  7. "fmt"
  8. "os"
  9. "strings"
  10. gconfig "gd_management_gateway/common.in/config"
  11. //"gd_management_gateway/common.in/cache"
  12. "gd_management_gateway/common.in/cache"
  13. "gd_management_gateway/common.in/clinit"
  14. "gd_management_gateway/common.in/config"
  15. "gd_management_gateway/common.in/httper"
  16. "gd_management_gateway/common.in/logger"
  17. dsignal "gd_management_gateway/common.in/signal"
  18. "gd_management_gateway/common.in/task"
  19. "github.com/astaxie/beego"
  20. "github.com/astaxie/beego/orm"
  21. _ "github.com/go-sql-driver/mysql"
  22. //"github.com/gomodule/redigo/redis"
  23. )
  24. var (
  25. // 这里要改默认值
  26. configFile = flag.String("config", "/etc/gd_management_gateway/app.conf", "config file location")
  27. version = flag.Bool("version", false, "config file location")
  28. GitCommit = "library-import"
  29. Version = "library-import"
  30. )
  31. func showVersion() {
  32. fmt.Println("Version: ", Version)
  33. fmt.Println("GitCommit:", GitCommit)
  34. }
  35. func prepare(projectName, runmode, key, logDir string, etcdAddrs []string) {
  36. discoveryType := beego.AppConfig.String("discovery_type")
  37. var conf *config.Configure
  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.InitForK8s(conf)
  46. ctrl_v1.PdfFont1File = "mplus-1p-bold.ttf"
  47. ctrl_v1.PdfFont2File = "microsoft.ttf"
  48. } else {
  49. clinit.InitEtcd(etcdAddrs)
  50. conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
  51. if conf == nil {
  52. fmt.Printf("get conf failed\n\n")
  53. os.Exit(1)
  54. }
  55. // 初始化所调用的微服务
  56. //rpc_apis.Init(etcdAddrs, conf)
  57. rpc_apis.Init(etcdAddrs, conf)
  58. }
  59. appname := conf.Cgi.Gateway.Name
  60. redisdb := conf.Redis.DefaultDB
  61. cache.InitRedis(&cache.RedisConfig{
  62. Addrs: strings.Split(conf.Redis.Addrs, ","),
  63. Password: conf.Redis.Password,
  64. DB: redisdb,
  65. PoolSize: conf.Redis.PoolSize,
  66. MinIdleConns: conf.Redis.MinIdleConns,
  67. MaxRetries: conf.Redis.MaxRetries,
  68. IsCluster: conf.Redis.IsCluster,
  69. })
  70. // 初始化logger
  71. ms, _ := conf.Cgi.Gateway.Log.MaxSize.Int64()
  72. mb, _ := conf.Cgi.Gateway.Log.MaxBackups.Int64()
  73. ma, _ := conf.Cgi.Gateway.Log.MaxAge.Int64()
  74. maxSize := int(ms)
  75. maxBackups := int(mb)
  76. maxAge := int(ma)
  77. disableStacktrace := (conf.Cgi.Gateway.Log.DisableStacktrace == "true")
  78. // 通用logger
  79. commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname),
  80. maxSize, maxBackups, maxAge, disableStacktrace)
  81. // 单独设置
  82. accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname),
  83. maxSize, maxBackups, maxAge)
  84. // 设置需要使用logger的地方
  85. // common日志
  86. ctrl_v1.SetLogger(commonLogger)
  87. // access日志
  88. task.SetLogger(accessLogger)
  89. // 打开第三方包调试开关
  90. if runmode != "prod" {
  91. orm.Debug = true
  92. //redis.Debug = true
  93. httper.Debug = true
  94. httper.DebugLog = commonLogger
  95. }
  96. }
  97. func main() {
  98. flag.Parse()
  99. if *version {
  100. showVersion()
  101. }
  102. if err := beego.LoadAppConfig("ini", *configFile); err != nil {
  103. fmt.Printf("Fail to read file: %v\n\n", err)
  104. os.Exit(1)
  105. }
  106. runmode := beego.AppConfig.String("runmode")
  107. //serveAddr := beego.AppConfig.String("serve_addr")
  108. //appname := beego.AppConfig.String("appname")
  109. logDir := beego.AppConfig.String("log_dir")
  110. etcdAddrs := strings.Split(beego.AppConfig.String("etcd_addrs"), ",")
  111. encryptKey := beego.AppConfig.String("encrypt_key")
  112. projectName := beego.AppConfig.String("project_name")
  113. swaggerDir := beego.AppConfig.String("swagger_dir")
  114. exportDir := beego.AppConfig.String("export_dir")
  115. apiTaskDir := beego.AppConfig.String("api_task_dir")
  116. outip := beego.AppConfig.String("outip")
  117. prepare(projectName, runmode, encryptKey, logDir, etcdAddrs)
  118. fmt.Printf("swagger:%s\n", swaggerDir)
  119. if runmode == "dev" || runmode == "test" {
  120. beego.BConfig.WebConfig.DirectoryIndex = true
  121. if swaggerDir == "" {
  122. beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  123. } else {
  124. beego.BConfig.WebConfig.StaticDir["/swagger"] = swaggerDir
  125. }
  126. }
  127. if exportDir == "" {
  128. beego.BConfig.WebConfig.StaticDir["/export"] = "/mnt/"
  129. } else {
  130. if strings.HasSuffix(exportDir, "/") {
  131. beego.BConfig.WebConfig.StaticDir["/export"] = exportDir
  132. } else {
  133. beego.BConfig.WebConfig.StaticDir["/export"] = exportDir + "/"
  134. }
  135. }
  136. if beego.AppConfig.String("discovery_type") == "k8s" {
  137. ctrl_v1.DiscoveryType = "k8s"
  138. }
  139. if apiTaskDir == "" {
  140. beego.BConfig.WebConfig.StaticDir["/try_api_task_dir"] = "/mnt/try_api_task/"
  141. os.Mkdir(beego.BConfig.WebConfig.StaticDir["/try_api_task_dir"], 0666)
  142. os.Chmod(beego.BConfig.WebConfig.StaticDir["/try_api_task_dir"], 0777)
  143. } else {
  144. if strings.HasSuffix(apiTaskDir, "/") {
  145. beego.BConfig.WebConfig.StaticDir["/try_api_task_dir"] = apiTaskDir
  146. } else {
  147. beego.BConfig.WebConfig.StaticDir["/try_api_task_dir"] = apiTaskDir + "/"
  148. }
  149. }
  150. serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Cgi.Gateway.ServiceName,gconfig.Conf.Cgi.Gateway.ServicePort.String())
  151. array := strings.Split(serveAddr, ":")
  152. if len(array) > 1 {
  153. ctrl_v1.ServerPort = array[1]
  154. ctrl_v1.ServerIp = outip
  155. }
  156. fmt.Printf("!!!!:%v,%v\n", runmode, swaggerDir)
  157. ctrl_v1.Watch()
  158. dsignal.HandleExitSignal()
  159. beego.Run(serveAddr)
  160. }