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

remove duplicated config initialization

This commit is contained in:
Pengfei Xue 2013-12-03 13:15:32 +08:00
parent 9c4414f995
commit 07312e240c

601
config.go
View File

@ -1,340 +1,261 @@
package beego package beego
import ( import (
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
"html/template" "html/template"
"os" "os"
"path" "path"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
) )
var ( var (
BeeApp *App BeeApp *App
AppName string AppName string
AppPath string AppPath string
AppConfigPath string AppConfigPath string
StaticDir map[string]string StaticDir map[string]string
TemplateCache map[string]*template.Template TemplateCache map[string]*template.Template
HttpAddr string HttpAddr string
HttpPort int HttpPort int
HttpTLS bool HttpTLS bool
HttpCertFile string HttpCertFile string
HttpKeyFile string HttpKeyFile string
RecoverPanic bool RecoverPanic bool
AutoRender bool AutoRender bool
PprofOn bool PprofOn bool
ViewsPath string ViewsPath string
RunMode string //"dev" or "prod" RunMode string //"dev" or "prod"
AppConfig config.ConfigContainer AppConfig config.ConfigContainer
//related to session //related to session
GlobalSessions *session.Manager //GlobalSessions GlobalSessions *session.Manager //GlobalSessions
SessionOn bool // whether auto start session,default is false SessionOn bool // whether auto start session,default is false
SessionProvider string // default session provider memory mysql redis SessionProvider string // default session provider memory mysql redis
SessionName string // sessionName cookie's name SessionName string // sessionName cookie's name
SessionGCMaxLifetime int64 // session's gc maxlifetime SessionGCMaxLifetime int64 // session's gc maxlifetime
SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo
SessionHashFunc string SessionHashFunc string
SessionHashKey string SessionHashKey string
SessionCookieLifeTime int SessionCookieLifeTime int
UseFcgi bool UseFcgi bool
MaxMemory int64 MaxMemory int64
EnableGzip bool // enable gzip EnableGzip bool // enable gzip
DirectoryIndex bool //enable DirectoryIndex default is false DirectoryIndex bool //enable DirectoryIndex default is false
EnableHotUpdate bool //enable HotUpdate default is false EnableHotUpdate bool //enable HotUpdate default is false
HttpServerTimeOut int64 //set httpserver timeout HttpServerTimeOut int64 //set httpserver timeout
ErrorsShow bool //set weather show errors ErrorsShow bool //set weather show errors
XSRFKEY string //set XSRF XSRFKEY string //set XSRF
EnableXSRF bool EnableXSRF bool
XSRFExpire int XSRFExpire int
CopyRequestBody bool //When in raw application, You want to the reqeustbody CopyRequestBody bool //When in raw application, You want to the reqeustbody
TemplateLeft string TemplateLeft string
TemplateRight string TemplateRight string
BeegoServerName string BeegoServerName string
EnableAdmin bool //enable admin module to log api time EnableAdmin bool //enable admin module to log api time
AdminHttpAddr string //admin module http addr AdminHttpAddr string //admin module http addr
AdminHttpPort int AdminHttpPort int
) )
func init() { func init() {
os.Chdir(path.Dir(os.Args[0])) os.Chdir(path.Dir(os.Args[0]))
BeeApp = NewApp() BeeApp = NewApp()
AppPath = path.Dir(os.Args[0]) AppPath = path.Dir(os.Args[0])
StaticDir = make(map[string]string) StaticDir = make(map[string]string)
TemplateCache = make(map[string]*template.Template) TemplateCache = make(map[string]*template.Template)
HttpAddr = "" HttpAddr = ""
HttpPort = 8080 HttpPort = 8080
AppName = "beego" AppName = "beego"
RunMode = "dev" //default runmod RunMode = "dev" //default runmod
AutoRender = true AutoRender = true
RecoverPanic = true RecoverPanic = true
PprofOn = false PprofOn = false
ViewsPath = "views" ViewsPath = "views"
SessionOn = false SessionOn = false
SessionProvider = "memory" SessionProvider = "memory"
SessionName = "beegosessionID" SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600 SessionGCMaxLifetime = 3600
SessionSavePath = "" SessionSavePath = ""
SessionHashFunc = "sha1" SessionHashFunc = "sha1"
SessionHashKey = "beegoserversessionkey" SessionHashKey = "beegoserversessionkey"
SessionCookieLifeTime = 3600 SessionCookieLifeTime = 3600
UseFcgi = false UseFcgi = false
MaxMemory = 1 << 26 //64MB MaxMemory = 1 << 26 //64MB
EnableGzip = false EnableGzip = false
StaticDir["/static"] = "static" StaticDir["/static"] = "static"
AppConfigPath = path.Join(AppPath, "conf", "app.conf") AppConfigPath = path.Join(AppPath, "conf", "app.conf")
HttpServerTimeOut = 0 HttpServerTimeOut = 0
ErrorsShow = true ErrorsShow = true
XSRFKEY = "beegoxsrf" XSRFKEY = "beegoxsrf"
XSRFExpire = 0 XSRFExpire = 0
TemplateLeft = "{{" TemplateLeft = "{{"
TemplateRight = "}}" TemplateRight = "}}"
BeegoServerName = "beegoServer" BeegoServerName = "beegoServer"
EnableAdmin = true EnableAdmin = true
AdminHttpAddr = "localhost" AdminHttpAddr = "localhost"
AdminHttpPort = 8088 AdminHttpPort = 8088
ParseConfig() ParseConfig()
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
} }
func ParseConfig() (err error) { func ParseConfig() (err error) {
AppConfig, err = config.NewConfig("ini", AppConfigPath) AppConfig, err = config.NewConfig("ini", AppConfigPath)
if err != nil { if err != nil {
return err return err
} else { } else {
HttpAddr = AppConfig.String("httpaddr") HttpAddr = AppConfig.String("httpaddr")
if v, err := AppConfig.Int("httpport"); err == nil {
HttpPort = v if v, err := AppConfig.Int("HttpPort"); err == nil {
} HttpPort = v
if v, err := AppConfig.Int("HttpPort"); err == nil { }
HttpPort = v
} if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
if maxmemory, err := AppConfig.Int64("maxmemory"); err == nil { MaxMemory = maxmemory
MaxMemory = maxmemory }
}
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { if appname := AppConfig.String("AppName"); appname != "" {
MaxMemory = maxmemory AppName = appname
} }
AppName = AppConfig.String("appname")
if appname := AppConfig.String("AppName"); appname != "" { if runmode := AppConfig.String("RunMode"); runmode != "" {
AppName = appname RunMode = runmode
} }
if runmode := AppConfig.String("runmode"); runmode != "" {
RunMode = runmode if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
} AutoRender = autorender
if runmode := AppConfig.String("RunMode"); runmode != "" { }
RunMode = runmode
} if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
if autorender, err := AppConfig.Bool("autorender"); err == nil { RecoverPanic = autorecover
AutoRender = autorender }
}
if autorender, err := AppConfig.Bool("AutoRender"); err == nil { if views := AppConfig.String("ViewsPath"); views != "" {
AutoRender = autorender ViewsPath = views
} }
if autorecover, err := AppConfig.Bool("autorecover"); err == nil {
RecoverPanic = autorecover if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
} SessionOn = sessionon
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil { }
RecoverPanic = autorecover
} if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
if views := AppConfig.String("viewspath"); views != "" { SessionProvider = sessProvider
ViewsPath = views }
}
if views := AppConfig.String("ViewsPath"); views != "" { if sessName := AppConfig.String("SessionName"); sessName != "" {
ViewsPath = views SessionName = sessName
} }
if sessionon, err := AppConfig.Bool("sessionon"); err == nil {
SessionOn = sessionon if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
} SessionSavePath = sesssavepath
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil { }
SessionOn = sessionon
} if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" {
if sessProvider := AppConfig.String("sessionprovider"); sessProvider != "" { SessionHashFunc = sesshashfunc
SessionProvider = sessProvider }
}
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" { if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" {
SessionProvider = sessProvider SessionHashKey = sesshashkey
} }
if sessName := AppConfig.String("sessionname"); sessName != "" {
SessionName = sessName if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
} int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
if sessName := AppConfig.String("SessionName"); sessName != "" { SessionGCMaxLifetime = int64val
SessionName = sessName }
}
if sesssavepath := AppConfig.String("sessionsavepath"); sesssavepath != "" { if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionSavePath = sesssavepath SessionCookieLifeTime = sesscookielifetime
} }
if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
SessionSavePath = sesssavepath if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
} UseFcgi = usefcgi
if sesshashfunc := AppConfig.String("sessionhashfunc"); sesshashfunc != "" { }
SessionHashFunc = sesshashfunc
} if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" { EnableGzip = enablegzip
SessionHashFunc = sesshashfunc }
}
if sesshashkey := AppConfig.String("sessionhashkey"); sesshashkey != "" { if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
SessionHashKey = sesshashkey DirectoryIndex = directoryindex
} }
if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" {
SessionHashKey = sesshashkey if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil {
} EnableHotUpdate = hotupdate
if sessMaxLifeTime, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && sessMaxLifeTime != 0 { }
int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
SessionGCMaxLifetime = int64val if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
} HttpServerTimeOut = timeout
if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { }
int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
SessionGCMaxLifetime = int64val if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
} ErrorsShow = errorsshow
if sesscookielifetime, err := AppConfig.Int("sessioncookielifelime"); err == nil && sesscookielifetime != 0 { }
SessionCookieLifeTime = sesscookielifetime
} if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { CopyRequestBody = copyrequestbody
SessionCookieLifeTime = sesscookielifetime }
}
if usefcgi, err := AppConfig.Bool("usefcgi"); err == nil { if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
UseFcgi = usefcgi XSRFKEY = xsrfkey
} }
if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
UseFcgi = usefcgi if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
} EnableXSRF = enablexsrf
if enablegzip, err := AppConfig.Bool("enablegzip"); err == nil { }
EnableGzip = enablegzip
} if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { XSRFExpire = expire
EnableGzip = enablegzip }
}
if directoryindex, err := AppConfig.Bool("directoryindex"); err == nil { if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
DirectoryIndex = directoryindex TemplateLeft = tplleft
} }
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
DirectoryIndex = directoryindex if tplright := AppConfig.String("TemplateRight"); tplright != "" {
} TemplateRight = tplright
if hotupdate, err := AppConfig.Bool("hotupdate"); err == nil { }
EnableHotUpdate = hotupdate
} if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil { HttpTLS = httptls
EnableHotUpdate = hotupdate }
}
if timeout, err := AppConfig.Int64("httpservertimeout"); err == nil { if certfile := AppConfig.String("httpcertfile"); certfile != "" {
HttpServerTimeOut = timeout HttpCertFile = certfile
} }
if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
HttpServerTimeOut = timeout if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
} HttpKeyFile = keyfile
if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil { }
ErrorsShow = errorsshow
} if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil { BeegoServerName = serverName
ErrorsShow = errorsshow }
}
if copyrequestbody, err := AppConfig.Bool("copyrequestbody"); err == nil { if sd := AppConfig.String("StaticDir"); sd != "" {
CopyRequestBody = copyrequestbody for k := range StaticDir {
} delete(StaticDir, k)
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil { }
CopyRequestBody = copyrequestbody sds := strings.Fields(sd)
} for _, v := range sds {
if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" { if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
XSRFKEY = xsrfkey StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
} } else {
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" { StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
XSRFKEY = xsrfkey }
} }
if enablexsrf, err := AppConfig.Bool("enablexsrf"); err == nil { }
EnableXSRF = enablexsrf
} if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil { EnableAdmin = enableadmin
EnableXSRF = enablexsrf }
}
if expire, err := AppConfig.Int("xsrfexpire"); err == nil { if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
XSRFExpire = expire AdminHttpAddr = adminhttpaddr
} }
if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
XSRFExpire = expire if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
} AdminHttpPort = adminhttpport
if tplleft := AppConfig.String("templateleft"); tplleft != "" { }
TemplateLeft = tplleft }
} return nil
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" { }
TemplateLeft = tplleft
}
if tplright := AppConfig.String("templateright"); tplright != "" {
TemplateRight = tplright
}
if tplright := AppConfig.String("TemplateRight"); tplright != "" {
TemplateRight = tplright
}
if httptls, err := AppConfig.Bool("httptls"); err == nil {
HttpTLS = httptls
}
if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
HttpTLS = httptls
}
if certfile := AppConfig.String("httpcertfile"); certfile != "" {
HttpCertFile = certfile
}
if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
HttpCertFile = certfile
}
if keyfile := AppConfig.String("httpkeyfile"); keyfile != "" {
HttpKeyFile = keyfile
}
if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile
}
if serverName := AppConfig.String("beegoserverName"); serverName != "" {
BeegoServerName = serverName
}
if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
BeegoServerName = serverName
}
if sd := AppConfig.String("staticdir"); sd != "" {
for k := range StaticDir {
delete(StaticDir, k)
}
sds := strings.Fields(sd)
for _, v := range sds {
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
} else {
StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
}
}
}
if sd := AppConfig.String("StaticDir"); sd != "" {
for k := range StaticDir {
delete(StaticDir, k)
}
sds := strings.Fields(sd)
for _, v := range sds {
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
} else {
StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
}
}
}
if enableadmin, err := AppConfig.Bool("enableadmin"); err == nil {
EnableAdmin = enableadmin
}
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
EnableAdmin = enableadmin
}
if adminhttpaddr := AppConfig.String("admintttpaddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr
}
if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr
}
if adminhttpport, err := AppConfig.Int("adminhttpport"); err == nil {
AdminHttpPort = adminhttpport
}
if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport
}
}
return nil
}