1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:00:55 +00:00

simplify config.go

This commit is contained in:
fuxiaohei 2015-12-17 20:05:00 +08:00
parent e5096be32b
commit f9138c5a99

View File

@ -177,8 +177,7 @@ func ParseConfig() (err error) {
if utils.FileExists(filepath.Join("conf", "app.conf")) { if utils.FileExists(filepath.Join("conf", "app.conf")) {
AppConfigPath = filepath.Join("conf", "app.conf") AppConfigPath = filepath.Join("conf", "app.conf")
} else { } else {
ac := config.NewFakeConfig() AppConfig = &beegoAppConfig{config.NewFakeConfig()}
AppConfig = &beegoAppConfig{ac}
return return
} }
} }
@ -186,9 +185,8 @@ func ParseConfig() (err error) {
if err != nil { if err != nil {
return err return err
} }
envRunMode := os.Getenv("BEEGO_RUNMODE")
// set the runmode first // set the runmode first
if envRunMode != "" { if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
BConfig.RunMode = envRunMode BConfig.RunMode = envRunMode
} else if runmode := AppConfig.String("RunMode"); runmode != "" { } else if runmode := AppConfig.String("RunMode"); runmode != "" {
BConfig.RunMode = runmode BConfig.RunMode = runmode
@ -277,109 +275,95 @@ func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
rac := &beegoAppConfig{ac} return &beegoAppConfig{ac}, nil
return rac, nil
} }
func (b *beegoAppConfig) Set(key, val string) error { func (b *beegoAppConfig) Set(key, val string) error {
err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val) if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
if err == nil {
return err return err
} }
return b.innerConfig.Set(key, val) return b.innerConfig.Set(key, val)
} }
func (b *beegoAppConfig) String(key string) string { func (b *beegoAppConfig) String(key string) string {
v := b.innerConfig.String(BConfig.RunMode + "::" + key) if v := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" {
if v == "" { return v
return b.innerConfig.String(key)
} }
return v return b.innerConfig.String(key)
} }
func (b *beegoAppConfig) Strings(key string) []string { func (b *beegoAppConfig) Strings(key string) []string {
v := b.innerConfig.Strings(BConfig.RunMode + "::" + key) if v := b.innerConfig.Strings(BConfig.RunMode + "::" + key); v[0] != "" {
if v[0] == "" { return v[0]
return b.innerConfig.Strings(key)
} }
return v return b.innerConfig.Strings(key)
} }
func (b *beegoAppConfig) Int(key string) (int, error) { func (b *beegoAppConfig) Int(key string) (int, error) {
v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key) if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Int(key)
} }
return v, nil return b.innerConfig.Int(key)
} }
func (b *beegoAppConfig) Int64(key string) (int64, error) { func (b *beegoAppConfig) Int64(key string) (int64, error) {
v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key) if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Int64(key)
} }
return v, nil return b.innerConfig.Int64(key)
} }
func (b *beegoAppConfig) Bool(key string) (bool, error) { func (b *beegoAppConfig) Bool(key string) (bool, error) {
v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key) if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Bool(key)
} }
return v, nil return b.innerConfig.Bool(key)
} }
func (b *beegoAppConfig) Float(key string) (float64, error) { func (b *beegoAppConfig) Float(key string) (float64, error) {
v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key) if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Float(key)
} }
return v, nil return b.innerConfig.Float(key)
} }
func (b *beegoAppConfig) DefaultString(key string, defaultval string) string { func (b *beegoAppConfig) DefaultString(key string, defaultval string) string {
v := b.String(key) if v := b.String(key); v != "" {
if v != "" {
return v return v
} }
return defaultval return defaultval
} }
func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string { func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string {
v := b.Strings(key) if v := b.Strings(key); len(v) != 0 {
if len(v) != 0 {
return v return v
} }
return defaultval return defaultval
} }
func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int { func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int {
v, err := b.Int(key) if v, err := b.Int(key); err == nil {
if err == nil {
return v return v
} }
return defaultval return defaultval
} }
func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 { func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 {
v, err := b.Int64(key) if v, err := b.Int64(key); err == nil {
if err == nil {
return v return v
} }
return defaultval return defaultval
} }
func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool { func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool {
v, err := b.Bool(key) if v, err := b.Bool(key); err == nil {
if err == nil {
return v return v
} }
return defaultval return defaultval
} }
func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 { func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 {
v, err := b.Float(key) if v, err := b.Float(key); err == nil {
if err == nil {
return v return v
} }
return defaultval return defaultval