1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:10:54 +00:00

beego:confgi support difference run mode section

runmode = dev
appname = doraemon
[dev]
httpport = 8880
sessionon = true

[prod]
httpport = 8888
sessionon = true

[test]
httpport = 8080
sessionon = false
This commit is contained in:
astaxie 2014-06-11 12:00:50 +08:00
parent 3db9633ebd
commit deb553be7f

208
config.go
View File

@ -7,12 +7,12 @@
package beego package beego
import ( import (
"errors"
"fmt" "fmt"
"html/template" "html/template"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
@ -178,154 +178,152 @@ func ParseConfig() (err error) {
AppConfig = config.NewFakeConfig() AppConfig = config.NewFakeConfig()
return err return err
} else { } else {
if v := AppConfig.String(RunMode + "::HttpAddr"); v != "" {
HttpAddr = v if v, err := getConfig("string", "HttpAddr"); err == nil {
} else { HttpAddr = v.(string)
HttpAddr = AppConfig.String("HttpAddr")
} }
if v, err := AppConfig.Int("HttpPort"); err == nil { if v, err := getConfig("int", "HttpPort"); err == nil {
HttpPort = v HttpPort = v.(int)
} }
if v, err := AppConfig.Bool("EnableHttpListen"); err == nil { if v, err := getConfig("bool", "EnableHttpListen"); err == nil {
EnableHttpListen = v EnableHttpListen = v.(bool)
} }
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { if maxmemory, err := getConfig("int64", "MaxMemory"); err == nil {
MaxMemory = maxmemory MaxMemory = maxmemory.(int64)
} }
if appname := AppConfig.String("AppName"); appname != "" { if appname, _ := getConfig("string", "AppName"); appname != "" {
AppName = appname AppName = appname.(string)
} }
if runmode := AppConfig.String("RunMode"); runmode != "" { if runmode, _ := getConfig("string", "RunMode"); runmode != "" {
RunMode = runmode RunMode = runmode.(string)
} }
if autorender, err := AppConfig.Bool("AutoRender"); err == nil { if autorender, err := getConfig("bool", "AutoRender"); err == nil {
AutoRender = autorender AutoRender = autorender.(bool)
} }
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil { if autorecover, err := getConfig("bool", "RecoverPanic"); err == nil {
RecoverPanic = autorecover RecoverPanic = autorecover.(bool)
} }
if views := AppConfig.String("ViewsPath"); views != "" { if views, _ := getConfig("string", "ViewsPath"); views != "" {
ViewsPath = views ViewsPath = views.(string)
} }
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil { if sessionon, err := getConfig("bool", "SessionOn"); err == nil {
SessionOn = sessionon SessionOn = sessionon.(bool)
} }
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" { if sessProvider, _ := getConfig("string", "SessionProvider"); sessProvider != "" {
SessionProvider = sessProvider SessionProvider = sessProvider.(string)
} }
if sessName := AppConfig.String("SessionName"); sessName != "" { if sessName, _ := getConfig("string", "SessionName"); sessName != "" {
SessionName = sessName SessionName = sessName.(string)
} }
if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" { if sesssavepath, _ := getConfig("string", "SessionSavePath"); sesssavepath != "" {
SessionSavePath = sesssavepath SessionSavePath = sesssavepath.(string)
} }
if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" { if sesshashfunc, _ := getConfig("string", "SessionHashFunc"); sesshashfunc != "" {
SessionHashFunc = sesshashfunc SessionHashFunc = sesshashfunc.(string)
} }
if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" { if sesshashkey, _ := getConfig("string", "SessionHashKey"); sesshashkey != "" {
SessionHashKey = sesshashkey SessionHashKey = sesshashkey.(string)
} }
if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { if sessMaxLifeTime, err := getConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64) SessionGCMaxLifetime = sessMaxLifeTime.(int64)
SessionGCMaxLifetime = int64val
} }
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { if sesscookielifetime, err := getConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionCookieLifeTime = sesscookielifetime SessionCookieLifeTime = sesscookielifetime.(int)
} }
if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil { if usefcgi, err := getConfig("bool", "UseFcgi"); err == nil {
UseFcgi = usefcgi UseFcgi = usefcgi.(bool)
} }
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { if enablegzip, err := getConfig("bool", "EnableGzip"); err == nil {
EnableGzip = enablegzip EnableGzip = enablegzip.(bool)
} }
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil { if directoryindex, err := getConfig("bool", "DirectoryIndex"); err == nil {
DirectoryIndex = directoryindex DirectoryIndex = directoryindex.(bool)
} }
if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil { if timeout, err := getConfig("int64", "HttpServerTimeOut"); err == nil {
HttpServerTimeOut = timeout HttpServerTimeOut = timeout.(int64)
} }
if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil { if errorsshow, err := getConfig("bool", "ErrorsShow"); err == nil {
ErrorsShow = errorsshow ErrorsShow = errorsshow.(bool)
} }
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil { if copyrequestbody, err := getConfig("bool", "CopyRequestBody"); err == nil {
CopyRequestBody = copyrequestbody CopyRequestBody = copyrequestbody.(bool)
} }
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" { if xsrfkey, _ := getConfig("string", "XSRFKEY"); xsrfkey != "" {
XSRFKEY = xsrfkey XSRFKEY = xsrfkey.(string)
} }
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil { if enablexsrf, err := getConfig("bool", "EnableXSRF"); err == nil {
EnableXSRF = enablexsrf EnableXSRF = enablexsrf.(bool)
} }
if expire, err := AppConfig.Int("XSRFExpire"); err == nil { if expire, err := getConfig("int", "XSRFExpire"); err == nil {
XSRFExpire = expire XSRFExpire = expire.(int)
} }
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" { if tplleft, _ := getConfig("string", "TemplateLeft"); tplleft != "" {
TemplateLeft = tplleft TemplateLeft = tplleft.(string)
} }
if tplright := AppConfig.String("TemplateRight"); tplright != "" { if tplright, _ := getConfig("string", "TemplateRight"); tplright != "" {
TemplateRight = tplright TemplateRight = tplright.(string)
} }
if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil { if httptls, err := getConfig("bool", "EnableHttpTLS"); err == nil {
EnableHttpTLS = httptls EnableHttpTLS = httptls.(bool)
} }
if httpsport, err := AppConfig.Int("HttpsPort"); err == nil { if httpsport, err := getConfig("int", "HttpsPort"); err == nil {
HttpsPort = httpsport HttpsPort = httpsport.(int)
} }
if certfile := AppConfig.String("HttpCertFile"); certfile != "" { if certfile, _ := getConfig("string", "HttpCertFile"); certfile != "" {
HttpCertFile = certfile HttpCertFile = certfile.(string)
} }
if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" { if keyfile, _ := getConfig("string", "HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile HttpKeyFile = keyfile.(string)
} }
if serverName := AppConfig.String("BeegoServerName"); serverName != "" { if serverName, _ := getConfig("string", "BeegoServerName"); serverName != "" {
BeegoServerName = serverName BeegoServerName = serverName.(string)
} }
if flashname := AppConfig.String("FlashName"); flashname != "" { if flashname, _ := getConfig("string", "FlashName"); flashname != "" {
FlashName = flashname FlashName = flashname.(string)
} }
if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" { if flashseperator, _ := getConfig("string", "FlashSeperator"); flashseperator != "" {
FlashSeperator = flashseperator FlashSeperator = flashseperator.(string)
} }
if sd := AppConfig.String("StaticDir"); sd != "" { if sd, _ := getConfig("string", "StaticDir"); sd != "" {
for k := range StaticDir { for k := range StaticDir {
delete(StaticDir, k) delete(StaticDir, k)
} }
sds := strings.Fields(sd) sds := strings.Fields(sd.(string))
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 {
StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1] StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
@ -335,8 +333,8 @@ func ParseConfig() (err error) {
} }
} }
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" { if sgz, _ := getConfig("string", "StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz, ",") extensions := strings.Split(sgz.(string), ",")
if len(extensions) > 0 { if len(extensions) > 0 {
StaticExtensionsToGzip = []string{} StaticExtensionsToGzip = []string{}
for _, ext := range extensions { for _, ext := range extensions {
@ -352,17 +350,59 @@ func ParseConfig() (err error) {
} }
} }
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil { if enableadmin, err := getConfig("bool", "EnableAdmin"); err == nil {
EnableAdmin = enableadmin EnableAdmin = enableadmin.(bool)
} }
if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" { if adminhttpaddr, _ := getConfig("string", "AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr AdminHttpAddr = adminhttpaddr.(string)
} }
if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil { if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport AdminHttpPort = adminhttpport.(int)
} }
} }
return nil return nil
} }
func getConfig(typ, key string) (interface{}, error) {
switch typ {
case "string":
v := AppConfig.String(RunMode + "::" + key)
if v == "" {
v = AppConfig.String(key)
}
return v, nil
case "strings":
v := AppConfig.Strings(RunMode + "::" + key)
if len(v) == 0 {
v = AppConfig.Strings(key)
}
return v, nil
case "int":
v, err := AppConfig.Int(RunMode + "::" + key)
if err != nil || v == 0 {
return AppConfig.Int(key)
}
return v, nil
case "bool":
v, err := AppConfig.Bool(RunMode + "::" + key)
if err != nil {
return AppConfig.Bool(key)
}
return v, nil
case "int64":
v, err := AppConfig.Int64(RunMode + "::" + key)
if err != nil || v == 0 {
return AppConfig.Int64(key)
}
return v, nil
case "float":
v, err := AppConfig.Float(RunMode + "::" + key)
if err != nil || v == 0 {
return AppConfig.Float(key)
}
return v, nil
}
return "", errors.New("not support type")
}