Skip to content

Commit

Permalink
read config by env
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Aug 12, 2020
1 parent 6f26d98 commit b1926f7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
38 changes: 30 additions & 8 deletions Abstractions/Configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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}
}
Expand Down
1 change: 1 addition & 0 deletions Abstractions/IServiceHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion Examples/SimpleWeb/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
application:
name: demo1
metadata: "hello world"
profile: "Dev"
profile: "dev"
server:
type: "fasthttp"
address: ":8080"
Expand Down
4 changes: 4 additions & 0 deletions Examples/SimpleWeb/config_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
application:
name: demo_dev
server:
address: ":8080"
4 changes: 4 additions & 0 deletions Examples/SimpleWeb/config_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
application:
name: demo_prod
server:
address: ":8081"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b1926f7

Please sign in to comment.