diff --git a/Abstractions/Configuration.go b/Abstractions/Configuration.go index 33aa19ee..7917c1df 100644 --- a/Abstractions/Configuration.go +++ b/Abstractions/Configuration.go @@ -11,20 +11,35 @@ type Configuration struct { } func NewConfiguration(configContext *ConfigurationContext) *Configuration { - v := viper.New() - - //设置配置文件的名字 - v.SetConfigName(configContext.configName) - v.AddConfigPath("./") - v.SetConfigType(configContext.configType) + defaultConfig := viper.New() + defaultConfig.AddConfigPath(".") + defaultConfig.SetConfigName(configContext.configName) + defaultConfig.SetConfigType(configContext.configType) + if err := defaultConfig.ReadInConfig(); err != nil { + return nil + } - if err := v.ReadInConfig(); err != nil { + profile := defaultConfig.Get("application.profile") + var profileConfig *viper.Viper + if profile != nil { + profileConfig = viper.New() + profileConfig.AddConfigPath(".") + profileConfig.SetConfigName(configContext.configName + "_" + profile.(string)) + profileConfig.SetConfigType(configContext.configType) + configs := defaultConfig.AllSettings() + // 将default中的配置全部以默认配置写入 + for k, v := range configs { + profileConfig.Set(k, v) + } + if err := profileConfig.ReadInConfig(); err != nil { + profileConfig = defaultConfig + } } return &Configuration{ context: configContext, - config: v, + config: profileConfig, } } @@ -34,6 +49,13 @@ func (c *Configuration) Get(name string) interface{} { func (c *Configuration) GetSection(name string) IConfiguration { section := c.config.Sub(name) + + configs := c.config.AllSettings() + // 将default中的配置全部以默认配置写入 + for k, v := range configs { + section.Set(k, v) + } + if section != nil { return &Configuration{config: section} } diff --git a/Abstractions/IServiceHost.go b/Abstractions/IServiceHost.go index ac9ab260..3a73a425 100644 --- a/Abstractions/IServiceHost.go +++ b/Abstractions/IServiceHost.go @@ -30,6 +30,7 @@ func PrintLogo(l *log.Logger, env *Context.HostEnvironment) { l.Printf("machine host ip : %s", ConsoleColors.Blue(env.Host)) l.Printf("listening on port : %s", ConsoleColors.Blue(env.Port)) l.Printf("application running pid : %s", ConsoleColors.Blue(strconv.Itoa(env.PID))) + l.Printf("application name : %s", ConsoleColors.Blue(env.ApplicationName)) l.Printf("application environment : %s", ConsoleColors.Blue(env.Profile)) l.Printf("application exec path : %s", ConsoleColors.Yellow(Utils.GetCurrentDirectory())) l.Printf("running in %s mode , change (Dev,Test,Prod) mode by HostBuilder.SetEnvironment .", ConsoleColors.Blue(env.Profile)) diff --git a/Examples/SimpleWeb/config.yml b/Examples/SimpleWeb/config.yml index ae80d77f..2e241b9b 100644 --- a/Examples/SimpleWeb/config.yml +++ b/Examples/SimpleWeb/config.yml @@ -1,7 +1,7 @@ application: name: demo1 metadata: "hello world" - profile: "Dev" + profile: "dev" server: type: "fasthttp" address: ":8080" diff --git a/Examples/SimpleWeb/config_dev.yml b/Examples/SimpleWeb/config_dev.yml new file mode 100644 index 00000000..1797bcf9 --- /dev/null +++ b/Examples/SimpleWeb/config_dev.yml @@ -0,0 +1,4 @@ +application: + name: demo_dev + server: + address: ":8080" \ No newline at end of file diff --git a/Examples/SimpleWeb/config_prod.yml b/Examples/SimpleWeb/config_prod.yml new file mode 100644 index 00000000..fbee0564 --- /dev/null +++ b/Examples/SimpleWeb/config_prod.yml @@ -0,0 +1,4 @@ +application: + name: demo_prod + server: + address: ":8081" \ No newline at end of file diff --git a/go.mod b/go.mod index 0a6ad89f..d2bceec4 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/magiconair/properties v1.8.1 github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v0.9.3 - github.com/spf13/viper v1.7.0 + github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.6.1 github.com/ugorji/go/codec v1.1.7 github.com/valyala/fasthttp v1.14.0 diff --git a/go.sum b/go.sum index 4d93c4a4..ea145e64 100644 --- a/go.sum +++ b/go.sum @@ -190,6 +190,8 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM= github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= +github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk= +github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=