123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package main
- import (
- "flag"
- "fmt"
- "gd_gateway/controllers/ctrl_v1"
- "gd_gateway/rpc_apis_v1"
- "gd_gateway/utils"
- "os"
- "strings"
- gconfig "gd_gateway/common.in/config"
- "gd_gateway/common.in/clinit"
- "gd_gateway/common.in/config"
- "gd_gateway/common.in/httper"
- "gd_gateway/common.in/logger"
- "gd_gateway/common.in/task"
- cutils "gd_gateway/common.in/utils"
- _ "gd_gateway/routers"
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/orm"
- _ "github.com/go-sql-driver/mysql"
- )
- var (
- // 这里要改默认值
- configFile = flag.String("config", "/etc/gd_gateway/app.conf", "config file location")
- //configFile = flag.String("config", "./conf/app.conf", "config file location")
- version = flag.Bool("version", false, "config file location")
- GitCommit = "library-import"
- Version = "library-import"
- )
- func showVersion() {
- fmt.Println("Version: ", Version)
- fmt.Println("GitCommit:", GitCommit)
- }
- func prepare(projectName, runmode, key, logDir string, etcdAddrs []string) {
- discoveryType := beego.AppConfig.String("discovery_type")
- var conf *config.Configure
- utils.SetDiscoveryType(discoveryType)
- // 先行于读配置
- if discoveryType == "k8s" {
- conf = config.GetConfigForK8s()
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- rpc_apis_v1.InitForK8s(conf)
- } else {
- clinit.InitEtcd(etcdAddrs)
- conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- // 初始化所调用的微服务
- //rpc_apis.Init(etcdAddrs, conf)
- rpc_apis_v1.Init(etcdAddrs, conf)
- }
- appname := conf.Cgi.GdGateway.Name
- // 初始化logger
- ms, _ := conf.Cgi.GdGateway.Log.MaxSize.Int64()
- mb, _ := conf.Cgi.GdGateway.Log.MaxBackups.Int64()
- ma, _ := conf.Cgi.GdGateway.Log.MaxAge.Int64()
- maxSize := int(ms)
- maxBackups := int(mb)
- maxAge := int(ma)
- disableStacktrace := (conf.Cgi.GdGateway.Log.DisableStacktrace == "true")
- // 通用logger
- commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Cgi.GdGateway.Log.Level,
- maxSize, maxBackups, maxAge, disableStacktrace)
- // 单独设置
- accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), appname, conf.Cgi.GdGateway.Log.Level,
- maxSize, maxBackups, maxAge)
- // 设置需要使用logger的地方
- // common日志
- ctrl_v1.SetLogger(commonLogger)
- utils.SetLogger(commonLogger)
- // access日志
- task.SetLogger(accessLogger)
- // 打开第三方包调试开关
- if runmode != "prod" {
- orm.Debug = true
- //redis.Debug = true
- httper.Debug = true
- httper.DebugLog = commonLogger
- }
- go utils.LoopLevelDb()
- }
- func main() {
- flag.Parse()
- if *version {
- showVersion()
- }
- if err := beego.LoadAppConfig("ini", *configFile); err != nil {
- fmt.Printf("Fail to read file: %v\n\n", err)
- os.Exit(1)
- }
- //serveAddr := beego.AppConfig.String("serve_addr")
- //appname := beego.AppConfig.String("appname")
- //imageDir := beego.AppConfig.String("weather_image_dir")
- runmode := beego.AppConfig.String("runmode")
- logDir := beego.AppConfig.String("log_dir")
- etcdAddrs := strings.Split(beego.AppConfig.String("etcd_addrs"), ",")
- encryptKey := beego.AppConfig.String("encrypt_key")
- projectName := beego.AppConfig.String("project_name")
- swaggerDir := beego.AppConfig.String("swagger_dir")
- prepare(projectName, runmode, encryptKey, logDir, etcdAddrs)
- /*if imageDir == "" {
- beego.SetStaticPath("/image", "static")
- } else {
- beego.SetStaticPath("/image", imageDir)
- }*/
- if runmode == "dev" || runmode == "test" {
- beego.BConfig.WebConfig.DirectoryIndex = true
- if swaggerDir == "" {
- beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
- } else {
- beego.BConfig.WebConfig.StaticDir["/swagger"] = swaggerDir
- }
- }
- go cutils.Free()
- cutils.HandleExitSignal()
- serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Cgi.GdGateway.ServiceName,gconfig.Conf.Cgi.GdGateway.ServicePort.String())
- beego.Run(serveAddr)
- }
|