// Copyright 2019 utimes.cc. All rights reserved. // Use of this source code is governed by utimes.cc. package main import ( "gd_auth_check/limit" "flag" "fmt" "log" "os" "os/signal" "strings" "syscall" "time" "gd_auth_check/impl" "gd_auth_check/impl/data_api_check" "gd_auth_check/common.in/cache" "gd_auth_check/common.in/clinit" "gd_auth_check/common.in/config" "gd_auth_check/common.in/logger" "gd_auth_check/common.in/task" "gd_auth_check/common.in/utils" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" //"github.com/gomodule/redigo/redis" "github.com/smallnest/rpcx/server" "github.com/smallnest/rpcx/serverplugin" gconfig "gd_auth_check/common.in/config" "gd_auth_check/rpc_apis" "github.com/rcrowley/go-metrics" "gopkg.in/ini.v1" ) var ( // 这里可以改默认值 configFile = flag.String("config", "/etc/gd_auth_check/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 string) { var conf *config.Configure if discoveryType == "k8s" { conf = config.GetConfigForK8s() if conf == nil { fmt.Printf("get conf failed\n\n") os.Exit(1) } rpc_apis.InitForK8s(conf) } else { // 先行于读配置 clinit.InitEtcd(etcdAddrs) utils.SetEtcdKeyApi(clinit.GetEtcdClient()) 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) } appname := conf.Rpc.AuthCheck.Name // 指定mysql数据库,若无则使用默认数据库 mysqldb := conf.Rpc.AuthCheck.MysqlDB if mysqldb == "" { mysqldb = conf.Mysql.DefaultDB } // 指定redis数据库,若无则使用默认数据库 redisdb := conf.Rpc.AuthCheck.RedisDB if redisdb == "" { redisdb = conf.Redis.DefaultDB } // 连接数据库服务器 clinit.InitMySQL(&clinit.MysqlConfig{ User: conf.Mysql.User, Password: conf.Mysql.Password, Addr: conf.Mysql.Addr, DB: mysqldb, Charset: conf.Mysql.Charset, MaxIdle: conf.Mysql.MaxIdle, MaxConn: conf.Mysql.MaxConn, }) // 连接redis服务器 cache.InitRedis(&cache.RedisConfig{ Addrs: strings.Split(conf.Redis.Addrs, ","), Password: conf.Redis.Password, DB: redisdb, PoolSize: conf.Redis.PoolSize, MinIdleConns: conf.Redis.MinIdleConns, MaxRetries: conf.Redis.MaxRetries, IsCluster: conf.Redis.IsCluster, }) //初始化logger ms, _ := conf.Rpc.AuthCheck.Log.MaxSize.Int64() mb, _ := conf.Rpc.AuthCheck.Log.MaxBackups.Int64() ma, _ := conf.Rpc.AuthCheck.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.AuthCheck.Log.DisableStacktrace == "true") //通用logger commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), maxSize, maxBackups, maxAge, disableStacktrace) // 单独设置 accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), maxSize, maxBackups, maxAge) //thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname), // maxSize, maxBackups, maxAge) // 设置需要使用logger的地方 // common日志 data_api_check.SetLogger(commonLogger) task.SetLogger(accessLogger) if runmode != "prod" { orm.Debug = true //redis.Debug = true } impl.RegisterModel() limit.InitLimiter() } func start(conf *config.Configure, serveAddr string, etcdAddrs []string) { s := server.NewServer() fmt.Printf("base path:%v\n", conf.Rpc.BasePath) r := &serverplugin.EtcdRegisterPlugin{ ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr), EtcdServers: etcdAddrs, BasePath: conf.Rpc.BasePath, Metrics: metrics.NewRegistry(), UpdateInterval: time.Minute, } if err := r.Start(); err != nil { fmt.Printf("start failed. error:%s\n\n", err) os.Exit(1) } s.Plugins.Add(r) s.RegisterName(conf.Rpc.AuthCheck.Name, new(impl.Rcvr), "") go func() { if err := s.Serve("tcp", serveAddr); err != nil { 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) r.Stop() time.Sleep(5 * time.Second) log.Println("Shutdown server finished.") } func startPeerToPeer(conf *config.Configure, serveAddr string) { s := server.NewServer() s.RegisterName(conf.Rpc.AuthCheck.Name, new(impl.Rcvr), "") s.Serve("tcp", serveAddr) } func main() { flag.Parse() if *version { showVersion() } cfg, err := ini.Load(*configFile) if err != nil { fmt.Printf("Fail to read file: %v\n\n", err) os.Exit(1) } //appname := cfg.Section("").Key("appname").String() logDir := cfg.Section("").Key("log_dir").String() runmode := cfg.Section("").Key("runmode").String() //serveAddr := cfg.Section("").Key("serve_addr").String() etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",") encryptKey := cfg.Section("").Key("encrypt_key").String() discoveryType := cfg.Section("").Key("discovery_type").String() projectName := cfg.Section("").Key("project_name").String() utils.SetRunmode(runmode) prepare(projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType) data_api_check.Watch() data_api_check.LoadRedis() go utils.Free() go data_api_check.CleanLocalCache() data_api_check.GetFilterRule() serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.AuthCheck.ServiceName,gconfig.Conf.Rpc.AuthCheck.ServicePort.String()) if discoveryType == "etcd" { start(gconfig.Conf, serveAddr, etcdAddrs) } else { startPeerToPeer(gconfig.Conf, serveAddr) } return }