1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:50:54 +00:00
This commit is contained in:
astaxie 2014-10-01 22:10:33 +08:00
parent 31e6133413
commit 8716185de8

335
config.go
View File

@ -15,7 +15,6 @@
package beego package beego
import ( import (
"errors"
"fmt" "fmt"
"html/template" "html/template"
"os" "os"
@ -48,8 +47,8 @@ var (
RecoverPanic bool // flag of auto recover panic RecoverPanic bool // flag of auto recover panic
AutoRender bool // flag of render template automatically AutoRender bool // flag of render template automatically
ViewsPath string ViewsPath string
AppConfig *beegoAppConfig
RunMode string // run mode, "dev" or "prod" RunMode string // run mode, "dev" or "prod"
AppConfig config.ConfigContainer
GlobalSessions *session.Manager // global session mananger GlobalSessions *session.Manager // global session mananger
SessionOn bool // flag of starting session auto. default is false. SessionOn bool // flag of starting session auto. default is false.
SessionProvider string // default session provider, memory, mysql , redis ,etc. SessionProvider string // default session provider, memory, mysql , redis ,etc.
@ -84,6 +83,107 @@ var (
RouterCaseSensitive bool // router case sensitive default is true RouterCaseSensitive bool // router case sensitive default is true
) )
type beegoAppConfig struct {
innerConfig config.ConfigContainer
}
func newAppConfig(AppConfigProvider, AppConfigPath string) *beegoAppConfig {
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath)
if err != nil {
ac = config.NewFakeConfig()
}
rac := &beegoAppConfig{ac}
return rac
}
func (b *beegoAppConfig) Set(key, val string) error {
return b.innerConfig.Set(key, val)
}
func (b *beegoAppConfig) String(key string) string {
v := b.innerConfig.String(RunMode + "::" + key)
if v == "" {
return b.innerConfig.String(key)
}
return v
}
func (b *beegoAppConfig) Strings(key string) []string {
v := b.innerConfig.Strings(RunMode + "::" + key)
if len(v) == 0 {
return b.innerConfig.Strings(key)
}
return v
}
func (b *beegoAppConfig) Int(key string) (int, error) {
v, err := b.innerConfig.Int(RunMode + "::" + key)
if err != nil {
return b.innerConfig.Int(key)
}
return v, nil
}
func (b *beegoAppConfig) Int64(key string) (int64, error) {
v, err := b.innerConfig.Int64(RunMode + "::" + key)
if err != nil {
return b.innerConfig.Int64(key)
}
return v, nil
}
func (b *beegoAppConfig) Bool(key string) (bool, error) {
v, err := b.innerConfig.Bool(RunMode + "::" + key)
if err != nil {
return b.innerConfig.Bool(key)
}
return v, nil
}
func (b *beegoAppConfig) Float(key string) (float64, error) {
v, err := b.innerConfig.Float(RunMode + "::" + key)
if err != nil {
return b.innerConfig.Float(key)
}
return v, nil
}
func (b *beegoAppConfig) DefaultString(key string, defaultval string) string {
return b.innerConfig.DefaultString(key, defaultval)
}
func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string {
return b.innerConfig.DefaultStrings(key, defaultval)
}
func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int {
return b.innerConfig.DefaultInt(key, defaultval)
}
func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 {
return b.innerConfig.DefaultInt64(key, defaultval)
}
func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool {
return b.innerConfig.DefaultBool(key, defaultval)
}
func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 {
return b.innerConfig.DefaultFloat(key, defaultval)
}
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
return b.innerConfig.DIY(key)
}
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
return b.innerConfig.GetSection(section)
}
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
return b.innerConfig.SaveConfigFile(filename)
}
func init() { func init() {
// create beego application // create beego application
BeeApp = NewApp() BeeApp = NewApp()
@ -186,157 +286,152 @@ func init() {
// ParseConfig parsed default config file. // ParseConfig parsed default config file.
// now only support ini, next will support json. // now only support ini, next will support json.
func ParseConfig() (err error) { func ParseConfig() (err error) {
AppConfig, err = config.NewConfig(AppConfigProvider, AppConfigPath) AppConfig = newAppConfig(AppConfigProvider, AppConfigPath)
if err != nil {
AppConfig = config.NewFakeConfig()
return err
} else {
if v, err := GetConfig("string", "HttpAddr"); err == nil { // set the runmode first
HttpAddr = v.(string) if runmode := AppConfig.String("RunMode"); runmode != "" {
RunMode = runmode
} }
if v, err := GetConfig("int", "HttpPort"); err == nil { HttpAddr = AppConfig.String("HttpAddr")
HttpPort = v.(int)
if v, err := AppConfig.Int("HttpPort"); err == nil {
HttpPort = v
} }
if v, err := GetConfig("bool", "EnableHttpListen"); err == nil { if v, err := AppConfig.Bool("EnableHttpListen"); err == nil {
EnableHttpListen = v.(bool) EnableHttpListen = v
} }
if maxmemory, err := GetConfig("int64", "MaxMemory"); err == nil { if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
MaxMemory = maxmemory.(int64) MaxMemory = maxmemory
} }
if appname, _ := GetConfig("string", "AppName"); appname != "" { if appname := AppConfig.String("AppName"); appname != "" {
AppName = appname.(string) AppName = appname
} }
if runmode, _ := GetConfig("string", "RunMode"); runmode != "" { if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
RunMode = runmode.(string) AutoRender = autorender
} }
if autorender, err := GetConfig("bool", "AutoRender"); err == nil { if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
AutoRender = autorender.(bool) RecoverPanic = autorecover
} }
if autorecover, err := GetConfig("bool", "RecoverPanic"); err == nil { if views := AppConfig.String("ViewsPath"); views != "" {
RecoverPanic = autorecover.(bool) ViewsPath = views
} }
if views, _ := GetConfig("string", "ViewsPath"); views != "" { if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
ViewsPath = views.(string) SessionOn = sessionon
} }
if sessionon, err := GetConfig("bool", "SessionOn"); err == nil { if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
SessionOn = sessionon.(bool) SessionProvider = sessProvider
} }
if sessProvider, _ := GetConfig("string", "SessionProvider"); sessProvider != "" { if sessName := AppConfig.String("SessionName"); sessName != "" {
SessionProvider = sessProvider.(string) SessionName = sessName
} }
if sessName, _ := GetConfig("string", "SessionName"); sessName != "" { if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
SessionName = sessName.(string) SessionSavePath = sesssavepath
} }
if sesssavepath, _ := GetConfig("string", "SessionSavePath"); sesssavepath != "" { if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" {
SessionSavePath = sesssavepath.(string) SessionHashFunc = sesshashfunc
} }
if sesshashfunc, _ := GetConfig("string", "SessionHashFunc"); sesshashfunc != "" { if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" {
SessionHashFunc = sesshashfunc.(string) SessionHashKey = sesshashkey
} }
if sesshashkey, _ := GetConfig("string", "SessionHashKey"); sesshashkey != "" { if sessMaxLifeTime, err := AppConfig.Int64("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
SessionHashKey = sesshashkey.(string) SessionGCMaxLifetime = sessMaxLifeTime
} }
if sessMaxLifeTime, err := GetConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionGCMaxLifetime = sessMaxLifeTime.(int64) SessionCookieLifeTime = sesscookielifetime
} }
if sesscookielifetime, err := GetConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
SessionCookieLifeTime = sesscookielifetime.(int) UseFcgi = usefcgi
} }
if usefcgi, err := GetConfig("bool", "UseFcgi"); err == nil { if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
UseFcgi = usefcgi.(bool) EnableGzip = enablegzip
} }
if enablegzip, err := GetConfig("bool", "EnableGzip"); err == nil { if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
EnableGzip = enablegzip.(bool) DirectoryIndex = directoryindex
} }
if directoryindex, err := GetConfig("bool", "DirectoryIndex"); err == nil { if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
DirectoryIndex = directoryindex.(bool) HttpServerTimeOut = timeout
} }
if timeout, err := GetConfig("int64", "HttpServerTimeOut"); err == nil { if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
HttpServerTimeOut = timeout.(int64) ErrorsShow = errorsshow
} }
if errorsshow, err := GetConfig("bool", "ErrorsShow"); err == nil { if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
ErrorsShow = errorsshow.(bool) CopyRequestBody = copyrequestbody
} }
if copyrequestbody, err := GetConfig("bool", "CopyRequestBody"); err == nil { if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
CopyRequestBody = copyrequestbody.(bool) XSRFKEY = xsrfkey
} }
if xsrfkey, _ := GetConfig("string", "XSRFKEY"); xsrfkey != "" { if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
XSRFKEY = xsrfkey.(string) EnableXSRF = enablexsrf
} }
if enablexsrf, err := GetConfig("bool", "EnableXSRF"); err == nil { if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
EnableXSRF = enablexsrf.(bool) XSRFExpire = expire
} }
if expire, err := GetConfig("int", "XSRFExpire"); err == nil { if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
XSRFExpire = expire.(int) TemplateLeft = tplleft
} }
if tplleft, _ := GetConfig("string", "TemplateLeft"); tplleft != "" { if tplright := AppConfig.String("TemplateRight"); tplright != "" {
TemplateLeft = tplleft.(string) TemplateRight = tplright
} }
if tplright, _ := GetConfig("string", "TemplateRight"); tplright != "" { if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil {
TemplateRight = tplright.(string) EnableHttpTLS = httptls
} }
if httptls, err := GetConfig("bool", "EnableHttpTLS"); err == nil { if httpsport, err := AppConfig.Int("HttpsPort"); err == nil {
EnableHttpTLS = httptls.(bool) HttpsPort = httpsport
} }
if httpsport, err := GetConfig("int", "HttpsPort"); err == nil { if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
HttpsPort = httpsport.(int) HttpCertFile = certfile
} }
if certfile, _ := GetConfig("string", "HttpCertFile"); certfile != "" { if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
HttpCertFile = certfile.(string) HttpKeyFile = keyfile
} }
if keyfile, _ := GetConfig("string", "HttpKeyFile"); keyfile != "" { if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
HttpKeyFile = keyfile.(string) BeegoServerName = serverName
} }
if serverName, _ := GetConfig("string", "BeegoServerName"); serverName != "" { if flashname := AppConfig.String("FlashName"); flashname != "" {
BeegoServerName = serverName.(string) FlashName = flashname
} }
if flashname, _ := GetConfig("string", "FlashName"); flashname != "" { if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" {
FlashName = flashname.(string) FlashSeperator = flashseperator
} }
if flashseperator, _ := GetConfig("string", "FlashSeperator"); flashseperator != "" { if sd := AppConfig.String("StaticDir"); sd != "" {
FlashSeperator = flashseperator.(string)
}
if sd, _ := GetConfig("string", "StaticDir"); sd != "" {
for k := range StaticDir { for k := range StaticDir {
delete(StaticDir, k) delete(StaticDir, k)
} }
sds := strings.Fields(sd.(string)) 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 {
StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1] StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
@ -346,8 +441,8 @@ func ParseConfig() (err error) {
} }
} }
if sgz, _ := GetConfig("string", "StaticExtensionsToGzip"); sgz != "" { if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz.(string), ",") extensions := strings.Split(sgz, ",")
if len(extensions) > 0 { if len(extensions) > 0 {
StaticExtensionsToGzip = []string{} StaticExtensionsToGzip = []string{}
for _, ext := range extensions { for _, ext := range extensions {
@ -363,78 +458,24 @@ func ParseConfig() (err error) {
} }
} }
if enableadmin, err := GetConfig("bool", "EnableAdmin"); err == nil { if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
EnableAdmin = enableadmin.(bool) EnableAdmin = enableadmin
} }
if adminhttpaddr, _ := GetConfig("string", "AdminHttpAddr"); adminhttpaddr != "" { if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr.(string) AdminHttpAddr = adminhttpaddr
} }
if adminhttpport, err := GetConfig("int", "AdminHttpPort"); err == nil { if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport.(int) AdminHttpPort = adminhttpport
} }
if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil { if enabledocs, err := AppConfig.Bool("EnableDocs"); err == nil {
EnableDocs = enabledocs.(bool) EnableDocs = enabledocs
} }
if casesensitive, err := GetConfig("bool", "RouterCaseSensitive"); err == nil { if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
RouterCaseSensitive = casesensitive.(bool) RouterCaseSensitive = casesensitive
}
} }
return nil return nil
} }
// 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 {
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")
}