mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:40:55 +00:00
commit
70f3f6b8b1
74
admin.go
74
admin.go
@ -23,6 +23,8 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/astaxie/beego/grace"
|
"github.com/astaxie/beego/grace"
|
||||||
"github.com/astaxie/beego/logs"
|
"github.com/astaxie/beego/logs"
|
||||||
"github.com/astaxie/beego/toolbox"
|
"github.com/astaxie/beego/toolbox"
|
||||||
@ -91,57 +93,9 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
switch command {
|
switch command {
|
||||||
case "conf":
|
case "conf":
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
|
list("BConfig", BConfig, m)
|
||||||
m["AppConfigPath"] = appConfigPath
|
m["AppConfigPath"] = appConfigPath
|
||||||
m["AppConfigProvider"] = appConfigProvider
|
m["AppConfigProvider"] = appConfigProvider
|
||||||
m["BConfig.AppName"] = BConfig.AppName
|
|
||||||
m["BConfig.RunMode"] = BConfig.RunMode
|
|
||||||
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
|
||||||
m["BConfig.ServerName"] = BConfig.ServerName
|
|
||||||
m["BConfig.RecoverPanic"] = BConfig.RecoverPanic
|
|
||||||
m["BConfig.CopyRequestBody"] = BConfig.CopyRequestBody
|
|
||||||
m["BConfig.EnableGzip"] = BConfig.EnableGzip
|
|
||||||
m["BConfig.MaxMemory"] = BConfig.MaxMemory
|
|
||||||
m["BConfig.EnableErrorsShow"] = BConfig.EnableErrorsShow
|
|
||||||
m["BConfig.Listen.Graceful"] = BConfig.Listen.Graceful
|
|
||||||
m["BConfig.Listen.ServerTimeOut"] = BConfig.Listen.ServerTimeOut
|
|
||||||
m["BConfig.Listen.ListenTCP4"] = BConfig.Listen.ListenTCP4
|
|
||||||
m["BConfig.Listen.EnableHTTP"] = BConfig.Listen.EnableHTTP
|
|
||||||
m["BConfig.Listen.HTTPAddr"] = BConfig.Listen.HTTPAddr
|
|
||||||
m["BConfig.Listen.HTTPPort"] = BConfig.Listen.HTTPPort
|
|
||||||
m["BConfig.Listen.EnableHTTPS"] = BConfig.Listen.EnableHTTPS
|
|
||||||
m["BConfig.Listen.HTTPSAddr"] = BConfig.Listen.HTTPSAddr
|
|
||||||
m["BConfig.Listen.HTTPSPort"] = BConfig.Listen.HTTPSPort
|
|
||||||
m["BConfig.Listen.HTTPSCertFile"] = BConfig.Listen.HTTPSCertFile
|
|
||||||
m["BConfig.Listen.HTTPSKeyFile"] = BConfig.Listen.HTTPSKeyFile
|
|
||||||
m["BConfig.Listen.EnableAdmin"] = BConfig.Listen.EnableAdmin
|
|
||||||
m["BConfig.Listen.AdminAddr"] = BConfig.Listen.AdminAddr
|
|
||||||
m["BConfig.Listen.AdminPort"] = BConfig.Listen.AdminPort
|
|
||||||
m["BConfig.Listen.EnableFcgi"] = BConfig.Listen.EnableFcgi
|
|
||||||
m["BConfig.Listen.EnableStdIo"] = BConfig.Listen.EnableStdIo
|
|
||||||
m["BConfig.WebConfig.AutoRender"] = BConfig.WebConfig.AutoRender
|
|
||||||
m["BConfig.WebConfig.EnableDocs"] = BConfig.WebConfig.EnableDocs
|
|
||||||
m["BConfig.WebConfig.FlashName"] = BConfig.WebConfig.FlashName
|
|
||||||
m["BConfig.WebConfig.FlashSeparator"] = BConfig.WebConfig.FlashSeparator
|
|
||||||
m["BConfig.WebConfig.DirectoryIndex"] = BConfig.WebConfig.DirectoryIndex
|
|
||||||
m["BConfig.WebConfig.StaticDir"] = BConfig.WebConfig.StaticDir
|
|
||||||
m["BConfig.WebConfig.StaticExtensionsToGzip"] = BConfig.WebConfig.StaticExtensionsToGzip
|
|
||||||
m["BConfig.WebConfig.TemplateLeft"] = BConfig.WebConfig.TemplateLeft
|
|
||||||
m["BConfig.WebConfig.TemplateRight"] = BConfig.WebConfig.TemplateRight
|
|
||||||
m["BConfig.WebConfig.ViewsPath"] = BConfig.WebConfig.ViewsPath
|
|
||||||
m["BConfig.WebConfig.EnableXSRF"] = BConfig.WebConfig.EnableXSRF
|
|
||||||
m["BConfig.WebConfig.XSRFKEY"] = BConfig.WebConfig.XSRFKey
|
|
||||||
m["BConfig.WebConfig.XSRFExpire"] = BConfig.WebConfig.XSRFExpire
|
|
||||||
m["BConfig.WebConfig.Session.SessionOn"] = BConfig.WebConfig.Session.SessionOn
|
|
||||||
m["BConfig.WebConfig.Session.SessionProvider"] = BConfig.WebConfig.Session.SessionProvider
|
|
||||||
m["BConfig.WebConfig.Session.SessionName"] = BConfig.WebConfig.Session.SessionName
|
|
||||||
m["BConfig.WebConfig.Session.SessionGCMaxLifetime"] = BConfig.WebConfig.Session.SessionGCMaxLifetime
|
|
||||||
m["BConfig.WebConfig.Session.SessionProviderConfig"] = BConfig.WebConfig.Session.SessionProviderConfig
|
|
||||||
m["BConfig.WebConfig.Session.SessionCookieLifeTime"] = BConfig.WebConfig.Session.SessionCookieLifeTime
|
|
||||||
m["BConfig.WebConfig.Session.SessionAutoSetCookie"] = BConfig.WebConfig.Session.SessionAutoSetCookie
|
|
||||||
m["BConfig.WebConfig.Session.SessionDomain"] = BConfig.WebConfig.Session.SessionDomain
|
|
||||||
m["BConfig.Log.AccessLogs"] = BConfig.Log.AccessLogs
|
|
||||||
m["BConfig.Log.FileLineNum"] = BConfig.Log.FileLineNum
|
|
||||||
m["BConfig.Log.Outputs"] = BConfig.Log.Outputs
|
|
||||||
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
|
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
|
||||||
tmpl = template.Must(tmpl.Parse(configTpl))
|
tmpl = template.Must(tmpl.Parse(configTpl))
|
||||||
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
|
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
|
||||||
@ -224,6 +178,28 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func list(root string, p interface{}, m map[string]interface{}) {
|
||||||
|
pt := reflect.TypeOf(p)
|
||||||
|
pv := reflect.ValueOf(p)
|
||||||
|
if pt.Kind() == reflect.Ptr {
|
||||||
|
pt = pt.Elem()
|
||||||
|
pv = pv.Elem()
|
||||||
|
}
|
||||||
|
for i := 0; i < pv.NumField(); i++ {
|
||||||
|
var key string
|
||||||
|
if root == "" {
|
||||||
|
key = pt.Field(i).Name
|
||||||
|
} else {
|
||||||
|
key = root + "." + pt.Field(i).Name
|
||||||
|
}
|
||||||
|
if pv.Field(i).Kind() == reflect.Struct {
|
||||||
|
list(key, pv.Field(i).Interface(), m)
|
||||||
|
} else {
|
||||||
|
m[key] = pv.Field(i).Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func printTree(resultList *[][]string, t *Tree) {
|
func printTree(resultList *[][]string, t *Tree) {
|
||||||
for _, tr := range t.fixrouters {
|
for _, tr := range t.fixrouters {
|
||||||
printTree(resultList, tr)
|
printTree(resultList, tr)
|
||||||
|
72
admin_test.go
Normal file
72
admin_test.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package beego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestList_01(t *testing.T) {
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
list("BConfig", BConfig, m)
|
||||||
|
t.Log(m)
|
||||||
|
om := oldMap()
|
||||||
|
for k, v := range om {
|
||||||
|
if fmt.Sprint(m[k])!= fmt.Sprint(v) {
|
||||||
|
t.Log(k, "old-key",v,"new-key", m[k])
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldMap() map[string]interface{} {
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
m["BConfig.AppName"] = BConfig.AppName
|
||||||
|
m["BConfig.RunMode"] = BConfig.RunMode
|
||||||
|
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
||||||
|
m["BConfig.ServerName"] = BConfig.ServerName
|
||||||
|
m["BConfig.RecoverPanic"] = BConfig.RecoverPanic
|
||||||
|
m["BConfig.CopyRequestBody"] = BConfig.CopyRequestBody
|
||||||
|
m["BConfig.EnableGzip"] = BConfig.EnableGzip
|
||||||
|
m["BConfig.MaxMemory"] = BConfig.MaxMemory
|
||||||
|
m["BConfig.EnableErrorsShow"] = BConfig.EnableErrorsShow
|
||||||
|
m["BConfig.Listen.Graceful"] = BConfig.Listen.Graceful
|
||||||
|
m["BConfig.Listen.ServerTimeOut"] = BConfig.Listen.ServerTimeOut
|
||||||
|
m["BConfig.Listen.ListenTCP4"] = BConfig.Listen.ListenTCP4
|
||||||
|
m["BConfig.Listen.EnableHTTP"] = BConfig.Listen.EnableHTTP
|
||||||
|
m["BConfig.Listen.HTTPAddr"] = BConfig.Listen.HTTPAddr
|
||||||
|
m["BConfig.Listen.HTTPPort"] = BConfig.Listen.HTTPPort
|
||||||
|
m["BConfig.Listen.EnableHTTPS"] = BConfig.Listen.EnableHTTPS
|
||||||
|
m["BConfig.Listen.HTTPSAddr"] = BConfig.Listen.HTTPSAddr
|
||||||
|
m["BConfig.Listen.HTTPSPort"] = BConfig.Listen.HTTPSPort
|
||||||
|
m["BConfig.Listen.HTTPSCertFile"] = BConfig.Listen.HTTPSCertFile
|
||||||
|
m["BConfig.Listen.HTTPSKeyFile"] = BConfig.Listen.HTTPSKeyFile
|
||||||
|
m["BConfig.Listen.EnableAdmin"] = BConfig.Listen.EnableAdmin
|
||||||
|
m["BConfig.Listen.AdminAddr"] = BConfig.Listen.AdminAddr
|
||||||
|
m["BConfig.Listen.AdminPort"] = BConfig.Listen.AdminPort
|
||||||
|
m["BConfig.Listen.EnableFcgi"] = BConfig.Listen.EnableFcgi
|
||||||
|
m["BConfig.Listen.EnableStdIo"] = BConfig.Listen.EnableStdIo
|
||||||
|
m["BConfig.WebConfig.AutoRender"] = BConfig.WebConfig.AutoRender
|
||||||
|
m["BConfig.WebConfig.EnableDocs"] = BConfig.WebConfig.EnableDocs
|
||||||
|
m["BConfig.WebConfig.FlashName"] = BConfig.WebConfig.FlashName
|
||||||
|
m["BConfig.WebConfig.FlashSeparator"] = BConfig.WebConfig.FlashSeparator
|
||||||
|
m["BConfig.WebConfig.DirectoryIndex"] = BConfig.WebConfig.DirectoryIndex
|
||||||
|
m["BConfig.WebConfig.StaticDir"] = BConfig.WebConfig.StaticDir
|
||||||
|
m["BConfig.WebConfig.StaticExtensionsToGzip"] = BConfig.WebConfig.StaticExtensionsToGzip
|
||||||
|
m["BConfig.WebConfig.TemplateLeft"] = BConfig.WebConfig.TemplateLeft
|
||||||
|
m["BConfig.WebConfig.TemplateRight"] = BConfig.WebConfig.TemplateRight
|
||||||
|
m["BConfig.WebConfig.ViewsPath"] = BConfig.WebConfig.ViewsPath
|
||||||
|
m["BConfig.WebConfig.EnableXSRF"] = BConfig.WebConfig.EnableXSRF
|
||||||
|
m["BConfig.WebConfig.XSRFExpire"] = BConfig.WebConfig.XSRFExpire
|
||||||
|
m["BConfig.WebConfig.Session.SessionOn"] = BConfig.WebConfig.Session.SessionOn
|
||||||
|
m["BConfig.WebConfig.Session.SessionProvider"] = BConfig.WebConfig.Session.SessionProvider
|
||||||
|
m["BConfig.WebConfig.Session.SessionName"] = BConfig.WebConfig.Session.SessionName
|
||||||
|
m["BConfig.WebConfig.Session.SessionGCMaxLifetime"] = BConfig.WebConfig.Session.SessionGCMaxLifetime
|
||||||
|
m["BConfig.WebConfig.Session.SessionProviderConfig"] = BConfig.WebConfig.Session.SessionProviderConfig
|
||||||
|
m["BConfig.WebConfig.Session.SessionCookieLifeTime"] = BConfig.WebConfig.Session.SessionCookieLifeTime
|
||||||
|
m["BConfig.WebConfig.Session.SessionAutoSetCookie"] = BConfig.WebConfig.Session.SessionAutoSetCookie
|
||||||
|
m["BConfig.WebConfig.Session.SessionDomain"] = BConfig.WebConfig.Session.SessionDomain
|
||||||
|
m["BConfig.Log.AccessLogs"] = BConfig.Log.AccessLogs
|
||||||
|
m["BConfig.Log.FileLineNum"] = BConfig.Log.FileLineNum
|
||||||
|
m["BConfig.Log.Outputs"] = BConfig.Log.Outputs
|
||||||
|
return m
|
||||||
|
}
|
145
config.go
145
config.go
@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/astaxie/beego/config"
|
"github.com/astaxie/beego/config"
|
||||||
@ -116,7 +117,30 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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",
|
AppName: "beego",
|
||||||
RunMode: DEV,
|
RunMode: DEV,
|
||||||
RouterCaseSensitive: true,
|
RouterCaseSensitive: true,
|
||||||
@ -175,25 +199,6 @@ func init() {
|
|||||||
Outputs: map[string]string{"console": ""},
|
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.
|
// now only support ini, next will support json.
|
||||||
@ -202,63 +207,23 @@ func parseConfig(appConfigPath string) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return assignConfig(AppConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assignConfig(ac config.Configer) error {
|
||||||
// set the run mode first
|
// set the run mode first
|
||||||
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
||||||
BConfig.RunMode = envRunMode
|
BConfig.RunMode = envRunMode
|
||||||
} else if runMode := AppConfig.String("RunMode"); runMode != "" {
|
} else if runMode := ac.String("RunMode"); runMode != "" {
|
||||||
BConfig.RunMode = runMode
|
BConfig.RunMode = runMode
|
||||||
}
|
}
|
||||||
|
|
||||||
BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName)
|
for _, i := range []interface{}{BConfig, &BConfig.Listen, &BConfig.WebConfig, &BConfig.Log, &BConfig.WebConfig.Session} {
|
||||||
BConfig.RecoverPanic = AppConfig.DefaultBool("RecoverPanic", BConfig.RecoverPanic)
|
assignSingleConfig(i, ac)
|
||||||
BConfig.RouterCaseSensitive = AppConfig.DefaultBool("RouterCaseSensitive", BConfig.RouterCaseSensitive)
|
}
|
||||||
BConfig.ServerName = AppConfig.DefaultString("ServerName", BConfig.ServerName)
|
|
||||||
BConfig.EnableGzip = AppConfig.DefaultBool("EnableGzip", BConfig.EnableGzip)
|
|
||||||
BConfig.EnableErrorsShow = AppConfig.DefaultBool("EnableErrorsShow", BConfig.EnableErrorsShow)
|
|
||||||
BConfig.CopyRequestBody = AppConfig.DefaultBool("CopyRequestBody", BConfig.CopyRequestBody)
|
|
||||||
BConfig.MaxMemory = AppConfig.DefaultInt64("MaxMemory", BConfig.MaxMemory)
|
|
||||||
BConfig.Listen.Graceful = AppConfig.DefaultBool("Graceful", BConfig.Listen.Graceful)
|
|
||||||
BConfig.Listen.HTTPAddr = AppConfig.String("HTTPAddr")
|
|
||||||
BConfig.Listen.HTTPPort = AppConfig.DefaultInt("HTTPPort", BConfig.Listen.HTTPPort)
|
|
||||||
BConfig.Listen.ListenTCP4 = AppConfig.DefaultBool("ListenTCP4", BConfig.Listen.ListenTCP4)
|
|
||||||
BConfig.Listen.EnableHTTP = AppConfig.DefaultBool("EnableHTTP", BConfig.Listen.EnableHTTP)
|
|
||||||
BConfig.Listen.EnableHTTPS = AppConfig.DefaultBool("EnableHTTPS", BConfig.Listen.EnableHTTPS)
|
|
||||||
BConfig.Listen.HTTPSAddr = AppConfig.DefaultString("HTTPSAddr", BConfig.Listen.HTTPSAddr)
|
|
||||||
BConfig.Listen.HTTPSPort = AppConfig.DefaultInt("HTTPSPort", BConfig.Listen.HTTPSPort)
|
|
||||||
BConfig.Listen.HTTPSCertFile = AppConfig.DefaultString("HTTPSCertFile", BConfig.Listen.HTTPSCertFile)
|
|
||||||
BConfig.Listen.HTTPSKeyFile = AppConfig.DefaultString("HTTPSKeyFile", BConfig.Listen.HTTPSKeyFile)
|
|
||||||
BConfig.Listen.EnableAdmin = AppConfig.DefaultBool("EnableAdmin", BConfig.Listen.EnableAdmin)
|
|
||||||
BConfig.Listen.AdminAddr = AppConfig.DefaultString("AdminAddr", BConfig.Listen.AdminAddr)
|
|
||||||
BConfig.Listen.AdminPort = AppConfig.DefaultInt("AdminPort", BConfig.Listen.AdminPort)
|
|
||||||
BConfig.Listen.EnableFcgi = AppConfig.DefaultBool("EnableFcgi", BConfig.Listen.EnableFcgi)
|
|
||||||
BConfig.Listen.EnableStdIo = AppConfig.DefaultBool("EnableStdIo", BConfig.Listen.EnableStdIo)
|
|
||||||
BConfig.Listen.ServerTimeOut = AppConfig.DefaultInt64("ServerTimeOut", BConfig.Listen.ServerTimeOut)
|
|
||||||
BConfig.WebConfig.AutoRender = AppConfig.DefaultBool("AutoRender", BConfig.WebConfig.AutoRender)
|
|
||||||
BConfig.WebConfig.ViewsPath = AppConfig.DefaultString("ViewsPath", BConfig.WebConfig.ViewsPath)
|
|
||||||
BConfig.WebConfig.DirectoryIndex = AppConfig.DefaultBool("DirectoryIndex", BConfig.WebConfig.DirectoryIndex)
|
|
||||||
BConfig.WebConfig.FlashName = AppConfig.DefaultString("FlashName", BConfig.WebConfig.FlashName)
|
|
||||||
BConfig.WebConfig.FlashSeparator = AppConfig.DefaultString("FlashSeparator", BConfig.WebConfig.FlashSeparator)
|
|
||||||
BConfig.WebConfig.EnableDocs = AppConfig.DefaultBool("EnableDocs", BConfig.WebConfig.EnableDocs)
|
|
||||||
BConfig.WebConfig.XSRFKey = AppConfig.DefaultString("XSRFKEY", BConfig.WebConfig.XSRFKey)
|
|
||||||
BConfig.WebConfig.EnableXSRF = AppConfig.DefaultBool("EnableXSRF", BConfig.WebConfig.EnableXSRF)
|
|
||||||
BConfig.WebConfig.XSRFExpire = AppConfig.DefaultInt("XSRFExpire", BConfig.WebConfig.XSRFExpire)
|
|
||||||
BConfig.WebConfig.TemplateLeft = AppConfig.DefaultString("TemplateLeft", BConfig.WebConfig.TemplateLeft)
|
|
||||||
BConfig.WebConfig.TemplateRight = AppConfig.DefaultString("TemplateRight", BConfig.WebConfig.TemplateRight)
|
|
||||||
BConfig.WebConfig.Session.SessionOn = AppConfig.DefaultBool("SessionOn", BConfig.WebConfig.Session.SessionOn)
|
|
||||||
BConfig.WebConfig.Session.SessionProvider = AppConfig.DefaultString("SessionProvider", BConfig.WebConfig.Session.SessionProvider)
|
|
||||||
BConfig.WebConfig.Session.SessionName = AppConfig.DefaultString("SessionName", BConfig.WebConfig.Session.SessionName)
|
|
||||||
BConfig.WebConfig.Session.SessionProviderConfig = AppConfig.DefaultString("SessionProviderConfig", BConfig.WebConfig.Session.SessionProviderConfig)
|
|
||||||
BConfig.WebConfig.Session.SessionGCMaxLifetime = AppConfig.DefaultInt64("SessionGCMaxLifetime", BConfig.WebConfig.Session.SessionGCMaxLifetime)
|
|
||||||
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
|
|
||||||
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
|
|
||||||
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
|
|
||||||
BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs)
|
|
||||||
BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum)
|
|
||||||
|
|
||||||
if sd := AppConfig.String("StaticDir"); sd != "" {
|
if sd := ac.String("StaticDir"); sd != "" {
|
||||||
for k := range BConfig.WebConfig.StaticDir {
|
BConfig.WebConfig.StaticDir = map[string]string{}
|
||||||
delete(BConfig.WebConfig.StaticDir, k)
|
|
||||||
}
|
|
||||||
sds := strings.Fields(sd)
|
sds := strings.Fields(sd)
|
||||||
for _, v := range sds {
|
for _, v := range sds {
|
||||||
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
|
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
|
||||||
@ -269,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, ",")
|
extensions := strings.Split(sgz, ",")
|
||||||
fileExts := []string{}
|
fileExts := []string{}
|
||||||
for _, ext := range extensions {
|
for _, ext := range extensions {
|
||||||
@ -287,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, ";")
|
los := strings.Split(lo, ";")
|
||||||
for _, v := range los {
|
for _, v := range los {
|
||||||
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
|
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
|
||||||
@ -301,9 +266,9 @@ func parseConfig(appConfigPath string) (err error) {
|
|||||||
//init log
|
//init log
|
||||||
logs.Reset()
|
logs.Reset()
|
||||||
for adaptor, config := range BConfig.Log.Outputs {
|
for adaptor, config := range BConfig.Log.Outputs {
|
||||||
err = logs.SetLogger(adaptor, config)
|
err := logs.SetLogger(adaptor, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err)
|
fmt.Fprintln(os.Stderr, "%s with the config `%s` got err:%s\n", adaptor, config, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logs.SetLogFuncCall(BConfig.Log.FileLineNum)
|
logs.SetLogFuncCall(BConfig.Log.FileLineNum)
|
||||||
@ -311,6 +276,38 @@ func parseConfig(appConfigPath string) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assignSingleConfig(p interface{}, ac config.Configer) {
|
||||||
|
pt := reflect.TypeOf(p)
|
||||||
|
if pt.Kind() != reflect.Ptr {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pt = pt.Elem()
|
||||||
|
if pt.Kind() != reflect.Struct {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pv := reflect.ValueOf(p).Elem()
|
||||||
|
|
||||||
|
for i := 0; i < pt.NumField(); i++ {
|
||||||
|
pf := pv.Field(i)
|
||||||
|
if !pf.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := pt.Field(i).Name
|
||||||
|
switch pf.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
pf.SetString(ac.DefaultString(name, pf.String()))
|
||||||
|
case reflect.Int, reflect.Int64:
|
||||||
|
pf.SetInt(int64(ac.DefaultInt64(name, pf.Int())))
|
||||||
|
case reflect.Bool:
|
||||||
|
pf.SetBool(ac.DefaultBool(name, pf.Bool()))
|
||||||
|
case reflect.Struct:
|
||||||
|
default:
|
||||||
|
//do nothing here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// LoadAppConfig allow developer to apply a config file
|
// LoadAppConfig allow developer to apply a config file
|
||||||
func LoadAppConfig(adapterName, configPath string) error {
|
func LoadAppConfig(adapterName, configPath string) error {
|
||||||
absConfigPath, err := filepath.Abs(configPath)
|
absConfigPath, err := filepath.Abs(configPath)
|
||||||
|
110
config_test.go
110
config_test.go
@ -15,7 +15,11 @@
|
|||||||
package beego
|
package beego
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDefaults(t *testing.T) {
|
func TestDefaults(t *testing.T) {
|
||||||
@ -27,3 +31,109 @@ func TestDefaults(t *testing.T) {
|
|||||||
t.Errorf("FlashName was not set to default.")
|
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")
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user