package main import ( "flag" "fmt" "jconfig" "jconsul" "jlog" "jredis" "jrest" "jversion" "log" "third-party-gateway/api/client" "third-party-gateway/api/route" "third-party-gateway/utils" ) const ServiceName = "third-party-gateway" type ConsulConf struct { Url string `yaml:"url"` } type MPConfig struct { AppId string `yaml:"appid"` AppSecret string `yaml:"appsecret"` } type Config struct { HttpPort int32 `yaml:"api-port"` Redis string `yaml:"redis"` MP MPConfig `yaml:"mini-program"` Debug bool `yaml:"debug"` } func main() { jversion.Init(VERSION) jversion.LogVersion() env := flag.String("env", "dev", "environment") flag.Parse() serviceNameEnv := ServiceName + "-" + *env consulConf := ConsulConf{} err := jconfig.ParseConfig("config/"+serviceNameEnv+".consul.yaml", &consulConf) if err != nil { return } jconsul.InitConsul(consulConf.Url) config := &Config{} if err := jconsul.LoadConfig(serviceNameEnv, config); err != nil { log.Println("load config error", err) return } jlog.Init(config.Debug) log.Println("config:", config) jconsul.RegisterServices(fmt.Sprintf("%s:%d", serviceNameEnv, config.HttpPort)) log.Println("conf:", config) jredis.Init(config.Redis) defer jredis.Close() client.InitMiniProgramClient(config.MP.AppId, config.MP.AppSecret) jrest.ServerWithHandlers(config.Debug, utils.TracingHandler, utils.ErrorHandler) route.InitRouter() jrest.ServerRun(config.HttpPort) }