1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 04:20:54 +00:00
Beego/config.go

179 lines
5.5 KiB
Go
Raw Normal View History

2012-12-15 15:53:19 +00:00
package beego
import (
2013-09-09 16:00:11 +00:00
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/session"
"html/template"
2012-12-15 15:53:19 +00:00
"os"
2013-09-09 16:00:11 +00:00
"path"
"runtime"
2012-12-15 15:53:19 +00:00
"strconv"
)
var (
2013-09-09 16:00:11 +00:00
BeeApp *App
AppName string
AppPath string
AppConfigPath string
StaticDir map[string]string
TemplateCache map[string]*template.Template
HttpAddr string
HttpPort int
HttpTLS bool
HttpCertFile string
HttpKeyFile string
RecoverPanic bool
AutoRender bool
PprofOn bool
ViewsPath string
RunMode string //"dev" or "prod"
AppConfig config.ConfigContainer
//related to session
GlobalSessions *session.Manager //GlobalSessions
SessionOn bool // whether auto start session,default is false
SessionProvider string // default session provider memory mysql redis
SessionName string // sessionName cookie's name
SessionGCMaxLifetime int64 // session's gc maxlifetime
SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo
UseFcgi bool
MaxMemory int64
EnableGzip bool // enable gzip
DirectoryIndex bool //enable DirectoryIndex default is false
EnableHotUpdate bool //enable HotUpdate default is false
HttpServerTimeOut int64 //set httpserver timeout
ErrorsShow bool //set weather show errors
XSRFKEY string //set XSRF
EnableXSRF bool
XSRFExpire int
CopyRequestBody bool //When in raw application, You want to the reqeustbody
TemplateLeft string
TemplateRight string
2012-12-15 15:53:19 +00:00
)
2013-09-09 16:00:11 +00:00
func init() {
os.Chdir(path.Dir(os.Args[0]))
BeeApp = NewApp()
AppPath, _ = os.Getwd()
StaticDir = make(map[string]string)
TemplateCache = make(map[string]*template.Template)
HttpAddr = ""
HttpPort = 8080
AppName = "beego"
RunMode = "dev" //default runmod
AutoRender = true
RecoverPanic = true
PprofOn = false
ViewsPath = "views"
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
SessionSavePath = ""
UseFcgi = false
MaxMemory = 1 << 26 //64MB
EnableGzip = false
StaticDir["/static"] = "static"
AppConfigPath = path.Join(AppPath, "conf", "app.conf")
HttpServerTimeOut = 0
ErrorsShow = true
XSRFKEY = "beegoxsrf"
XSRFExpire = 0
2013-09-09 16:00:11 +00:00
TemplateLeft = "{{"
TemplateRight = "}}"
ParseConfig()
runtime.GOMAXPROCS(runtime.NumCPU())
2012-12-15 15:53:19 +00:00
}
2013-05-05 15:03:59 +00:00
func ParseConfig() (err error) {
2013-09-10 06:47:48 +00:00
AppConfig, err = config.NewConfig("ini", AppConfigPath)
2013-05-05 15:03:59 +00:00
if err != nil {
return err
} else {
HttpAddr = AppConfig.String("httpaddr")
if v, err := AppConfig.Int("httpport"); err == nil {
HttpPort = v
}
if maxmemory, err := AppConfig.Int64("maxmemory"); err == nil {
MaxMemory = maxmemory
2013-05-05 15:03:59 +00:00
}
AppName = AppConfig.String("appname")
if runmode := AppConfig.String("runmode"); runmode != "" {
RunMode = runmode
}
if autorender, err := AppConfig.Bool("autorender"); err == nil {
AutoRender = autorender
2013-05-05 15:03:59 +00:00
}
if autorecover, err := AppConfig.Bool("autorecover"); err == nil {
RecoverPanic = autorecover
2013-05-05 15:03:59 +00:00
}
if pprofon, err := AppConfig.Bool("pprofon"); err == nil {
PprofOn = pprofon
2013-05-05 15:03:59 +00:00
}
if views := AppConfig.String("viewspath"); views != "" {
ViewsPath = views
}
if sessionon, err := AppConfig.Bool("sessionon"); err == nil {
SessionOn = sessionon
2013-05-05 15:03:59 +00:00
}
if sessProvider := AppConfig.String("sessionprovider"); sessProvider != "" {
SessionProvider = sessProvider
2013-05-05 15:03:59 +00:00
}
if sessName := AppConfig.String("sessionname"); sessName != "" {
SessionName = sessName
2013-05-05 15:03:59 +00:00
}
if sesssavepath := AppConfig.String("sessionsavepath"); sesssavepath != "" {
SessionSavePath = sesssavepath
2013-05-05 15:03:59 +00:00
}
if sessMaxLifeTime, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && sessMaxLifeTime != 0 {
int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
2013-05-05 15:03:59 +00:00
SessionGCMaxLifetime = int64val
}
if usefcgi, err := AppConfig.Bool("usefcgi"); err == nil {
UseFcgi = usefcgi
2013-05-05 15:03:59 +00:00
}
if enablegzip, err := AppConfig.Bool("enablegzip"); err == nil {
EnableGzip = enablegzip
2013-05-05 15:03:59 +00:00
}
if directoryindex, err := AppConfig.Bool("directoryindex"); err == nil {
DirectoryIndex = directoryindex
}
if hotupdate, err := AppConfig.Bool("hotupdate"); err == nil {
EnableHotUpdate = hotupdate
2013-06-24 15:24:13 +00:00
}
2013-07-03 07:29:54 +00:00
if timeout, err := AppConfig.Int64("httpservertimeout"); err == nil {
HttpServerTimeOut = timeout
}
2013-07-07 09:58:50 +00:00
if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil {
ErrorsShow = errorsshow
}
2013-07-08 15:12:31 +00:00
if copyrequestbody, err := AppConfig.Bool("copyrequestbody"); err == nil {
CopyRequestBody = copyrequestbody
}
2013-07-08 08:17:08 +00:00
if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" {
XSRFKEY = xsrfkey
}
2013-08-06 15:21:52 +00:00
if enablexsrf, err := AppConfig.Bool("enablexsrf"); err == nil {
EnableXSRF = enablexsrf
}
2013-08-07 03:22:23 +00:00
if expire, err := AppConfig.Int("xsrfexpire"); err == nil {
XSRFExpire = expire
}
if tplleft := AppConfig.String("templateleft"); tplleft != "" {
2013-08-19 05:02:18 +00:00
TemplateLeft = tplleft
}
if tplright := AppConfig.String("templateright"); tplright != "" {
2013-08-19 05:02:18 +00:00
TemplateRight = tplright
}
2013-09-09 16:00:11 +00:00
if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
HttpTLS = httptls
}
if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
HttpCertFile = certfile
}
if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile
}
2013-05-05 15:03:59 +00:00
}
return nil
}