mirror of
https://github.com/astaxie/beego.git
synced 2024-12-23 11:10:50 +00:00
config test
This commit is contained in:
parent
4cd2408248
commit
0c32255d14
69
config.go
69
config.go
@ -117,7 +117,30 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
BConfig = &Config{
|
||||
BConfig = newBConfig()
|
||||
var err error
|
||||
if AppPath, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
workPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
appConfigPath = filepath.Join(workPath, "conf", "app.conf")
|
||||
if !utils.FileExists(appConfigPath) {
|
||||
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
||||
if !utils.FileExists(appConfigPath) {
|
||||
AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
|
||||
return
|
||||
}
|
||||
}
|
||||
if err = parseConfig(appConfigPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func newBConfig() *Config {
|
||||
return &Config{
|
||||
AppName: "beego",
|
||||
RunMode: DEV,
|
||||
RouterCaseSensitive: true,
|
||||
@ -176,25 +199,6 @@ func init() {
|
||||
Outputs: map[string]string{"console": ""},
|
||||
},
|
||||
}
|
||||
var err error
|
||||
if AppPath, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
workPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
appConfigPath = filepath.Join(workPath, "conf", "app.conf")
|
||||
if !utils.FileExists(appConfigPath) {
|
||||
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
||||
if !utils.FileExists(appConfigPath) {
|
||||
AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
|
||||
return
|
||||
}
|
||||
}
|
||||
if err = parseConfig(appConfigPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// now only support ini, next will support json.
|
||||
@ -203,21 +207,23 @@ func parseConfig(appConfigPath string) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return assignConfig(AppConfig)
|
||||
}
|
||||
|
||||
func assignConfig(ac config.Configer) error {
|
||||
// set the run mode first
|
||||
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
||||
BConfig.RunMode = envRunMode
|
||||
} else if runMode := AppConfig.String("RunMode"); runMode != "" {
|
||||
} else if runMode := ac.String("RunMode"); runMode != "" {
|
||||
BConfig.RunMode = runMode
|
||||
}
|
||||
|
||||
for _, i := range []interface{}{BConfig, &BConfig.Listen, &BConfig.WebConfig, &BConfig.Log, &BConfig.WebConfig.Session} {
|
||||
assignConfig(i, AppConfig)
|
||||
assignSingleConfig(i, ac)
|
||||
}
|
||||
|
||||
if sd := AppConfig.String("StaticDir"); sd != "" {
|
||||
for k := range BConfig.WebConfig.StaticDir {
|
||||
delete(BConfig.WebConfig.StaticDir, k)
|
||||
}
|
||||
if sd := ac.String("StaticDir"); sd != "" {
|
||||
BConfig.WebConfig.StaticDir = map[string]string{}
|
||||
sds := strings.Fields(sd)
|
||||
for _, v := range sds {
|
||||
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
|
||||
@ -228,7 +234,7 @@ func parseConfig(appConfigPath string) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
|
||||
if sgz := ac.String("StaticExtensionsToGzip"); sgz != "" {
|
||||
extensions := strings.Split(sgz, ",")
|
||||
fileExts := []string{}
|
||||
for _, ext := range extensions {
|
||||
@ -246,7 +252,7 @@ func parseConfig(appConfigPath string) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
if lo := AppConfig.String("LogOutputs"); lo != "" {
|
||||
if lo := ac.String("LogOutputs"); lo != "" {
|
||||
los := strings.Split(lo, ";")
|
||||
for _, v := range los {
|
||||
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
|
||||
@ -260,7 +266,7 @@ func parseConfig(appConfigPath string) (err error) {
|
||||
//init log
|
||||
logs.Reset()
|
||||
for adaptor, config := range BConfig.Log.Outputs {
|
||||
err = logs.SetLogger(adaptor, config)
|
||||
err := logs.SetLogger(adaptor, config)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "%s with the config `%s` got err:%s\n", adaptor, config, err)
|
||||
}
|
||||
@ -270,7 +276,7 @@ func parseConfig(appConfigPath string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func assignConfig(p interface{}, ac config.Configer) {
|
||||
func assignSingleConfig(p interface{}, ac config.Configer) {
|
||||
pt := reflect.TypeOf(p)
|
||||
if pt.Kind() != reflect.Ptr {
|
||||
return
|
||||
@ -295,9 +301,8 @@ func assignConfig(p interface{}, ac config.Configer) {
|
||||
case reflect.Bool:
|
||||
pf.SetBool(ac.DefaultBool(name, pf.Bool()))
|
||||
case reflect.Struct:
|
||||
//do nothing here
|
||||
default:
|
||||
logs.Critical("beego not support such kind of config filed", pt.Name(), pf.Kind())
|
||||
//do nothing here
|
||||
}
|
||||
}
|
||||
|
||||
|
113
config_test.go
113
config_test.go
@ -15,7 +15,11 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/config"
|
||||
)
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
@ -27,3 +31,112 @@ func TestDefaults(t *testing.T) {
|
||||
t.Errorf("FlashName was not set to default.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignConfig_01(t *testing.T) {
|
||||
_BConfig := &Config{}
|
||||
_BConfig.AppName = "beego_test"
|
||||
jcf := &config.JSONConfig{}
|
||||
ac, _ := jcf.ParseData([]byte(`{"AppName":"beego_json"}`))
|
||||
assignSingleConfig(_BConfig, ac)
|
||||
if _BConfig.AppName != "beego_json" {
|
||||
t.Log(_BConfig)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignConfig_02(t *testing.T) {
|
||||
_BConfig := &Config{}
|
||||
bs, _ := json.Marshal(newBConfig())
|
||||
|
||||
jsonMap := map[string]interface{}{}
|
||||
json.Unmarshal(bs, &jsonMap)
|
||||
|
||||
configMap := map[string]interface{}{}
|
||||
for k, v := range jsonMap {
|
||||
if reflect.TypeOf(v).Kind() == reflect.Map {
|
||||
for k1, v1 := range v.(map[string]interface{}) {
|
||||
if reflect.TypeOf(v1).Kind() == reflect.Map {
|
||||
for k2, v2 := range v1.(map[string]interface{}) {
|
||||
configMap[k2] = v2
|
||||
}
|
||||
} else {
|
||||
configMap[k1] = v1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
configMap[k] = v
|
||||
}
|
||||
}
|
||||
configMap["MaxMemory"] = 1024
|
||||
configMap["Graceful"] = true
|
||||
configMap["XSRFExpire"] = 32
|
||||
configMap["SessionProviderConfig"] = "file"
|
||||
configMap["FileLineNum"] = true
|
||||
|
||||
jcf := &config.JSONConfig{}
|
||||
bs, _ = json.Marshal(configMap)
|
||||
ac, _ := jcf.ParseData([]byte(bs))
|
||||
|
||||
for _, i := range []interface{}{_BConfig, &_BConfig.Listen, &_BConfig.WebConfig, &_BConfig.Log, &_BConfig.WebConfig.Session} {
|
||||
assignSingleConfig(i, ac)
|
||||
}
|
||||
|
||||
if _BConfig.MaxMemory != 1024 {
|
||||
t.Log(_BConfig.MaxMemory)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !_BConfig.Listen.Graceful {
|
||||
t.Log(_BConfig.Listen.Graceful)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if _BConfig.WebConfig.XSRFExpire != 32 {
|
||||
t.Log(_BConfig.WebConfig.XSRFExpire)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if _BConfig.WebConfig.Session.SessionProviderConfig != "file" {
|
||||
t.Log(_BConfig.WebConfig.Session.SessionProviderConfig)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !_BConfig.Log.FileLineNum {
|
||||
t.Log(_BConfig.Log.FileLineNum)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestAssignConfig_03(t *testing.T) {
|
||||
jcf := &config.JSONConfig{}
|
||||
ac, _ := jcf.ParseData([]byte(`{"AppName":"beego"}`))
|
||||
ac.Set("AppName", "test_app")
|
||||
ac.Set("RunMode", "online")
|
||||
ac.Set("StaticDir", "download:down download2:down2")
|
||||
ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
|
||||
ac.Set("LogOutputs", `"file": ""`)
|
||||
assignConfig(ac)
|
||||
|
||||
|
||||
//t.Logf("%#v",BConfig)
|
||||
if BConfig.AppName != "test_app" {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if BConfig.RunMode != "online" {
|
||||
t.FailNow()
|
||||
}
|
||||
if BConfig.WebConfig.StaticDir["download"] != "down" {
|
||||
t.FailNow()
|
||||
}
|
||||
if BConfig.WebConfig.StaticDir["download2"] != "down2" {
|
||||
t.FailNow()
|
||||
}
|
||||
if len(BConfig.WebConfig.StaticExtensionsToGzip) != 5 {
|
||||
t.FailNow()
|
||||
}
|
||||
if _, ok := BConfig.Log.Outputs["file"]; !ok {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user