mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-05 08:46:57 +08:00
add option params
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
package initial
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/zhufuyi/sponge/configs"
|
||||
"github.com/zhufuyi/sponge/internal/config"
|
||||
"github.com/zhufuyi/sponge/internal/model"
|
||||
|
||||
"github.com/zhufuyi/sponge/pkg/logger"
|
||||
"github.com/zhufuyi/sponge/pkg/nacoscli"
|
||||
"github.com/zhufuyi/sponge/pkg/stat"
|
||||
"github.com/zhufuyi/sponge/pkg/tracer"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
var (
|
||||
version string
|
||||
configFile string
|
||||
enableConfigCenter bool
|
||||
)
|
||||
|
||||
// Config 初始化配置
|
||||
func Config() {
|
||||
initConfig()
|
||||
cfg := config.Get()
|
||||
|
||||
// 初始化日志
|
||||
_, _ = logger.Init(
|
||||
logger.WithLevel(cfg.Logger.Level),
|
||||
logger.WithFormat(cfg.Logger.Format),
|
||||
logger.WithSave(cfg.Logger.IsSave),
|
||||
)
|
||||
|
||||
// 初始化数据库
|
||||
model.InitMysql()
|
||||
model.InitCache(cfg.App.CacheType)
|
||||
|
||||
// 初始化链路跟踪
|
||||
if cfg.App.EnableTracing {
|
||||
tracer.InitWithConfig(
|
||||
cfg.App.Name,
|
||||
cfg.App.Env,
|
||||
cfg.App.Version,
|
||||
cfg.Jaeger.AgentHost,
|
||||
strconv.Itoa(cfg.Jaeger.AgentPort),
|
||||
cfg.App.TracingSamplingRate,
|
||||
)
|
||||
}
|
||||
|
||||
// 初始化打印系统和进程资源
|
||||
if cfg.App.EnableStat {
|
||||
stat.Init(stat.WithLog(logger.Get()))
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化配置
|
||||
func initConfig() {
|
||||
flag.StringVar(&version, "version", "", "service Version Number")
|
||||
flag.BoolVar(&enableConfigCenter, "enable-cc", false, "whether to get from the configuration center, "+
|
||||
"if true, the '-c' parameter indicates the configuration center")
|
||||
flag.StringVar(&configFile, "c", "", "configuration file")
|
||||
flag.Parse()
|
||||
|
||||
if enableConfigCenter {
|
||||
// 从配置中心获取配置(先获取nacos配置,再根据nacos配置中心读取服务配置)
|
||||
if configFile == "" {
|
||||
configFile = configs.Path("serverNameExample_cc.yml")
|
||||
}
|
||||
nacosConfig, err := config.NewCenter(configFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
appConfig := &config.Config{}
|
||||
params := &nacoscli.Params{}
|
||||
_ = copier.Copy(params, &nacosConfig.Nacos)
|
||||
err = nacoscli.Init(appConfig, params)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("connect to configuration center err, %v", err))
|
||||
}
|
||||
if appConfig.App.Name == "" {
|
||||
panic("read the config from center error, config data is empty")
|
||||
}
|
||||
config.Set(appConfig)
|
||||
} else {
|
||||
// 从本地配置文件获取配置
|
||||
if configFile == "" {
|
||||
configFile = configs.Path("serverNameExample.yml")
|
||||
}
|
||||
err := config.Init(configFile)
|
||||
if err != nil {
|
||||
panic("init config error: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if version != "" {
|
||||
config.Get().App.Version = version
|
||||
}
|
||||
//fmt.Println(config.Show())
|
||||
}
|
Reference in New Issue
Block a user