1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-30 10:14:12 +00:00

make the getconfig public

// Getconfig throw the Runmode
// [dev]
// name = astaixe
// IsEnable = false
// [prod]
// name = slene
// IsEnable = true
//
// usage:
// GetConfig("string", "name")
// GetConfig("bool", "IsEnable")
This commit is contained in:
astaxie 2014-08-11 22:13:18 +08:00
parent 00eac0e4cb
commit 3d74a1a436

View File

@ -184,147 +184,147 @@ func ParseConfig() (err error) {
return err return err
} else { } else {
if v, err := getConfig("string", "HttpAddr"); err == nil { if v, err := GetConfig("string", "HttpAddr"); err == nil {
HttpAddr = v.(string) HttpAddr = v.(string)
} }
if v, err := getConfig("int", "HttpPort"); err == nil { if v, err := GetConfig("int", "HttpPort"); err == nil {
HttpPort = v.(int) HttpPort = v.(int)
} }
if v, err := getConfig("bool", "EnableHttpListen"); err == nil { if v, err := GetConfig("bool", "EnableHttpListen"); err == nil {
EnableHttpListen = v.(bool) EnableHttpListen = v.(bool)
} }
if maxmemory, err := getConfig("int64", "MaxMemory"); err == nil { if maxmemory, err := GetConfig("int64", "MaxMemory"); err == nil {
MaxMemory = maxmemory.(int64) MaxMemory = maxmemory.(int64)
} }
if appname, _ := getConfig("string", "AppName"); appname != "" { if appname, _ := GetConfig("string", "AppName"); appname != "" {
AppName = appname.(string) AppName = appname.(string)
} }
if runmode, _ := getConfig("string", "RunMode"); runmode != "" { if runmode, _ := GetConfig("string", "RunMode"); runmode != "" {
RunMode = runmode.(string) RunMode = runmode.(string)
} }
if autorender, err := getConfig("bool", "AutoRender"); err == nil { if autorender, err := GetConfig("bool", "AutoRender"); err == nil {
AutoRender = autorender.(bool) AutoRender = autorender.(bool)
} }
if autorecover, err := getConfig("bool", "RecoverPanic"); err == nil { if autorecover, err := GetConfig("bool", "RecoverPanic"); err == nil {
RecoverPanic = autorecover.(bool) RecoverPanic = autorecover.(bool)
} }
if views, _ := getConfig("string", "ViewsPath"); views != "" { if views, _ := GetConfig("string", "ViewsPath"); views != "" {
ViewsPath = views.(string) ViewsPath = views.(string)
} }
if sessionon, err := getConfig("bool", "SessionOn"); err == nil { if sessionon, err := GetConfig("bool", "SessionOn"); err == nil {
SessionOn = sessionon.(bool) SessionOn = sessionon.(bool)
} }
if sessProvider, _ := getConfig("string", "SessionProvider"); sessProvider != "" { if sessProvider, _ := GetConfig("string", "SessionProvider"); sessProvider != "" {
SessionProvider = sessProvider.(string) SessionProvider = sessProvider.(string)
} }
if sessName, _ := getConfig("string", "SessionName"); sessName != "" { if sessName, _ := GetConfig("string", "SessionName"); sessName != "" {
SessionName = sessName.(string) SessionName = sessName.(string)
} }
if sesssavepath, _ := getConfig("string", "SessionSavePath"); sesssavepath != "" { if sesssavepath, _ := GetConfig("string", "SessionSavePath"); sesssavepath != "" {
SessionSavePath = sesssavepath.(string) SessionSavePath = sesssavepath.(string)
} }
if sesshashfunc, _ := getConfig("string", "SessionHashFunc"); sesshashfunc != "" { if sesshashfunc, _ := GetConfig("string", "SessionHashFunc"); sesshashfunc != "" {
SessionHashFunc = sesshashfunc.(string) SessionHashFunc = sesshashfunc.(string)
} }
if sesshashkey, _ := getConfig("string", "SessionHashKey"); sesshashkey != "" { if sesshashkey, _ := GetConfig("string", "SessionHashKey"); sesshashkey != "" {
SessionHashKey = sesshashkey.(string) SessionHashKey = sesshashkey.(string)
} }
if sessMaxLifeTime, err := getConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { if sessMaxLifeTime, err := GetConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
SessionGCMaxLifetime = sessMaxLifeTime.(int64) SessionGCMaxLifetime = sessMaxLifeTime.(int64)
} }
if sesscookielifetime, err := getConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { if sesscookielifetime, err := GetConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionCookieLifeTime = sesscookielifetime.(int) SessionCookieLifeTime = sesscookielifetime.(int)
} }
if usefcgi, err := getConfig("bool", "UseFcgi"); err == nil { if usefcgi, err := GetConfig("bool", "UseFcgi"); err == nil {
UseFcgi = usefcgi.(bool) UseFcgi = usefcgi.(bool)
} }
if enablegzip, err := getConfig("bool", "EnableGzip"); err == nil { if enablegzip, err := GetConfig("bool", "EnableGzip"); err == nil {
EnableGzip = enablegzip.(bool) EnableGzip = enablegzip.(bool)
} }
if directoryindex, err := getConfig("bool", "DirectoryIndex"); err == nil { if directoryindex, err := GetConfig("bool", "DirectoryIndex"); err == nil {
DirectoryIndex = directoryindex.(bool) DirectoryIndex = directoryindex.(bool)
} }
if timeout, err := getConfig("int64", "HttpServerTimeOut"); err == nil { if timeout, err := GetConfig("int64", "HttpServerTimeOut"); err == nil {
HttpServerTimeOut = timeout.(int64) HttpServerTimeOut = timeout.(int64)
} }
if errorsshow, err := getConfig("bool", "ErrorsShow"); err == nil { if errorsshow, err := GetConfig("bool", "ErrorsShow"); err == nil {
ErrorsShow = errorsshow.(bool) ErrorsShow = errorsshow.(bool)
} }
if copyrequestbody, err := getConfig("bool", "CopyRequestBody"); err == nil { if copyrequestbody, err := GetConfig("bool", "CopyRequestBody"); err == nil {
CopyRequestBody = copyrequestbody.(bool) CopyRequestBody = copyrequestbody.(bool)
} }
if xsrfkey, _ := getConfig("string", "XSRFKEY"); xsrfkey != "" { if xsrfkey, _ := GetConfig("string", "XSRFKEY"); xsrfkey != "" {
XSRFKEY = xsrfkey.(string) XSRFKEY = xsrfkey.(string)
} }
if enablexsrf, err := getConfig("bool", "EnableXSRF"); err == nil { if enablexsrf, err := GetConfig("bool", "EnableXSRF"); err == nil {
EnableXSRF = enablexsrf.(bool) EnableXSRF = enablexsrf.(bool)
} }
if expire, err := getConfig("int", "XSRFExpire"); err == nil { if expire, err := GetConfig("int", "XSRFExpire"); err == nil {
XSRFExpire = expire.(int) XSRFExpire = expire.(int)
} }
if tplleft, _ := getConfig("string", "TemplateLeft"); tplleft != "" { if tplleft, _ := GetConfig("string", "TemplateLeft"); tplleft != "" {
TemplateLeft = tplleft.(string) TemplateLeft = tplleft.(string)
} }
if tplright, _ := getConfig("string", "TemplateRight"); tplright != "" { if tplright, _ := GetConfig("string", "TemplateRight"); tplright != "" {
TemplateRight = tplright.(string) TemplateRight = tplright.(string)
} }
if httptls, err := getConfig("bool", "EnableHttpTLS"); err == nil { if httptls, err := GetConfig("bool", "EnableHttpTLS"); err == nil {
EnableHttpTLS = httptls.(bool) EnableHttpTLS = httptls.(bool)
} }
if httpsport, err := getConfig("int", "HttpsPort"); err == nil { if httpsport, err := GetConfig("int", "HttpsPort"); err == nil {
HttpsPort = httpsport.(int) HttpsPort = httpsport.(int)
} }
if certfile, _ := getConfig("string", "HttpCertFile"); certfile != "" { if certfile, _ := GetConfig("string", "HttpCertFile"); certfile != "" {
HttpCertFile = certfile.(string) HttpCertFile = certfile.(string)
} }
if keyfile, _ := getConfig("string", "HttpKeyFile"); keyfile != "" { if keyfile, _ := GetConfig("string", "HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile.(string) HttpKeyFile = keyfile.(string)
} }
if serverName, _ := getConfig("string", "BeegoServerName"); serverName != "" { if serverName, _ := GetConfig("string", "BeegoServerName"); serverName != "" {
BeegoServerName = serverName.(string) BeegoServerName = serverName.(string)
} }
if flashname, _ := getConfig("string", "FlashName"); flashname != "" { if flashname, _ := GetConfig("string", "FlashName"); flashname != "" {
FlashName = flashname.(string) FlashName = flashname.(string)
} }
if flashseperator, _ := getConfig("string", "FlashSeperator"); flashseperator != "" { if flashseperator, _ := GetConfig("string", "FlashSeperator"); flashseperator != "" {
FlashSeperator = flashseperator.(string) FlashSeperator = flashseperator.(string)
} }
if sd, _ := getConfig("string", "StaticDir"); sd != "" { if sd, _ := GetConfig("string", "StaticDir"); sd != "" {
for k := range StaticDir { for k := range StaticDir {
delete(StaticDir, k) delete(StaticDir, k)
} }
@ -338,7 +338,7 @@ func ParseConfig() (err error) {
} }
} }
if sgz, _ := getConfig("string", "StaticExtensionsToGzip"); sgz != "" { if sgz, _ := GetConfig("string", "StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz.(string), ",") extensions := strings.Split(sgz.(string), ",")
if len(extensions) > 0 { if len(extensions) > 0 {
StaticExtensionsToGzip = []string{} StaticExtensionsToGzip = []string{}
@ -355,26 +355,37 @@ func ParseConfig() (err error) {
} }
} }
if enableadmin, err := getConfig("bool", "EnableAdmin"); err == nil { if enableadmin, err := GetConfig("bool", "EnableAdmin"); err == nil {
EnableAdmin = enableadmin.(bool) EnableAdmin = enableadmin.(bool)
} }
if adminhttpaddr, _ := getConfig("string", "AdminHttpAddr"); adminhttpaddr != "" { if adminhttpaddr, _ := GetConfig("string", "AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr.(string) AdminHttpAddr = adminhttpaddr.(string)
} }
if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil { if adminhttpport, err := GetConfig("int", "AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport.(int) AdminHttpPort = adminhttpport.(int)
} }
if enabledocs, err := getConfig("bool", "EnableDocs"); err == nil { if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil {
EnableDocs = enabledocs.(bool) EnableDocs = enabledocs.(bool)
} }
} }
return nil return nil
} }
func getConfig(typ, key string) (interface{}, error) { // Getconfig throw the Runmode
// [dev]
// name = astaixe
// IsEnable = false
// [prod]
// name = slene
// IsEnable = true
//
// usage:
// GetConfig("string", "name")
// GetConfig("bool", "IsEnable")
func GetConfig(typ, key string) (interface{}, error) {
switch typ { switch typ {
case "string": case "string":
v := AppConfig.String(RunMode + "::" + key) v := AppConfig.String(RunMode + "::" + key)