mirror of
https://github.com/astaxie/beego.git
synced 2025-07-11 06:31:03 +00:00
refact beego config
This commit is contained in:
762
config.go
762
config.go
@ -15,131 +15,372 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/config"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/session"
|
||||
"github.com/astaxie/beego/utils"
|
||||
)
|
||||
|
||||
type BeegoConfig struct {
|
||||
AppName string //Application name
|
||||
RunMode string //Running Mode: dev | prod
|
||||
RouterCaseSensitive bool
|
||||
ServerName string
|
||||
RecoverPanic bool
|
||||
CopyRequestBody bool
|
||||
EnableGzip bool
|
||||
MaxMemory int64
|
||||
EnableErrorsShow bool
|
||||
Listen Listen
|
||||
WebConfig WebConfig
|
||||
Log LogConfig
|
||||
}
|
||||
|
||||
type Listen struct {
|
||||
Graceful bool // Graceful means use graceful module to start the server
|
||||
ServerTimeOut int64
|
||||
ListenTCP4 bool
|
||||
HTTPEnable bool
|
||||
HTTPAddr string
|
||||
HTTPPort int
|
||||
HTTPSEnable bool
|
||||
HTTPSAddr string
|
||||
HTTPSPort int
|
||||
HTTPSCertFile string
|
||||
HTTPSKeyFile string
|
||||
AdminEnable bool
|
||||
AdminAddr string
|
||||
AdminPort int
|
||||
EnableFcgi bool
|
||||
EnableStdIo bool // EnableStdIo works with EnableFcgi Use FCGI via standard I/O
|
||||
}
|
||||
|
||||
type WebConfig struct {
|
||||
AutoRender bool
|
||||
EnableDocs bool
|
||||
FlashName string
|
||||
FlashSeperator string
|
||||
DirectoryIndex bool
|
||||
StaticDir map[string]string
|
||||
StaticExtensionsToGzip []string
|
||||
TemplateLeft string
|
||||
TemplateRight string
|
||||
ViewsPath string
|
||||
EnableXSRF bool
|
||||
XSRFKEY string
|
||||
XSRFExpire int
|
||||
Session SessionConfig
|
||||
}
|
||||
|
||||
type SessionConfig struct {
|
||||
SessionOn bool
|
||||
SessionProvider string
|
||||
SessionName string
|
||||
SessionGCMaxLifetime int64
|
||||
SessionProviderConfig string
|
||||
SessionCookieLifeTime int
|
||||
SessionAutoSetCookie bool
|
||||
SessionDomain string
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
AccessLogs bool
|
||||
FileLineNum bool
|
||||
Output map[string]string // Store Adaptor : config
|
||||
}
|
||||
|
||||
var (
|
||||
// AccessLogs represent whether output the access logs, default is false
|
||||
AccessLogs bool
|
||||
// AdminHTTPAddr is address for admin
|
||||
AdminHTTPAddr string
|
||||
// AdminHTTPPort is listens port for admin
|
||||
AdminHTTPPort int
|
||||
// BConfig is the default config for Application
|
||||
BConfig *BeegoConfig
|
||||
// AppConfig is the instance of Config, store the config information from file
|
||||
AppConfig *beegoAppConfig
|
||||
// AppName represent Application name, always the project folder name
|
||||
AppName string
|
||||
// AppPath is the path to the application
|
||||
AppPath string
|
||||
// AppConfigPath is the path to the config files
|
||||
AppConfigPath string
|
||||
// AppConfigProvider is the provider for the config, default is ini
|
||||
AppConfigProvider string
|
||||
// AutoRender is a flag of render template automatically. It's always turn off in API application
|
||||
// default is true
|
||||
AutoRender bool
|
||||
// BeegoServerName exported in response header.
|
||||
BeegoServerName string
|
||||
// CopyRequestBody is just useful for raw request body in context. default is false
|
||||
CopyRequestBody bool
|
||||
// DirectoryIndex wheather display directory index. default is false.
|
||||
DirectoryIndex bool
|
||||
// EnableAdmin means turn on admin module to log every request info.
|
||||
EnableAdmin bool
|
||||
// EnableDocs enable generate docs & server docs API Swagger
|
||||
EnableDocs bool
|
||||
// EnableErrorsShow wheather show errors in page. if true, show error and trace info in page rendered with error template.
|
||||
EnableErrorsShow bool
|
||||
// EnableFcgi turn on the fcgi Listen, default is false
|
||||
EnableFcgi bool
|
||||
// EnableGzip means gzip the response
|
||||
EnableGzip bool
|
||||
// EnableHTTPListen represent whether turn on the HTTP, default is true
|
||||
EnableHTTPListen bool
|
||||
// EnableHTTPTLS represent whether turn on the HTTPS, default is true
|
||||
EnableHTTPTLS bool
|
||||
// EnableStdIo works with EnableFcgi Use FCGI via standard I/O
|
||||
EnableStdIo bool
|
||||
// EnableXSRF whether turn on xsrf. default is false
|
||||
EnableXSRF bool
|
||||
// FlashName is the name of the flash variable found in response header and cookie
|
||||
FlashName string
|
||||
// FlashSeperator used to seperate flash key:value, default is BEEGOFLASH
|
||||
FlashSeperator string
|
||||
AppConfigProvider = "ini"
|
||||
// TemplateCache stores template caching
|
||||
TemplateCache map[string]*template.Template
|
||||
// GlobalSessions is the instance for the session manager
|
||||
GlobalSessions *session.Manager
|
||||
// Graceful means use graceful module to start the server
|
||||
Graceful bool
|
||||
// workPath is always the same as AppPath, but sometime when it started with other
|
||||
// program, like supervisor
|
||||
workPath string
|
||||
// ListenTCP4 represent only Listen in TCP4, default is false
|
||||
ListenTCP4 bool
|
||||
// MaxMemory The whole request body is parsed and up to a total of maxMemory
|
||||
// bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files
|
||||
MaxMemory int64
|
||||
// HTTPAddr is the TCP network address addr for HTTP
|
||||
HTTPAddr string
|
||||
// HTTPPort is listens port for HTTP
|
||||
HTTPPort int
|
||||
// HTTPSPort is listens port for HTTPS
|
||||
HTTPSPort int
|
||||
// HTTPCertFile is the path to certificate file
|
||||
HTTPCertFile string
|
||||
// HTTPKeyFile is the path to private key file
|
||||
HTTPKeyFile string
|
||||
// HTTPServerTimeOut HTTP server timeout. default is 0, no timeout
|
||||
HTTPServerTimeOut int64
|
||||
// RecoverPanic is a flag for auto recover panic, default is true
|
||||
RecoverPanic bool
|
||||
// RouterCaseSensitive means whether router case sensitive, default is true
|
||||
RouterCaseSensitive bool
|
||||
// RunMode represent the staging, "dev" or "prod"
|
||||
RunMode string
|
||||
// SessionOn means whether turn on the session auto when application started. default is false.
|
||||
SessionOn bool
|
||||
// SessionProvider means session provider, e.q memory, mysql, redis,etc.
|
||||
SessionProvider string
|
||||
// SessionName is the cookie name when saving session id into cookie.
|
||||
SessionName string
|
||||
// SessionGCMaxLifetime for auto cleaning expired session.
|
||||
SessionGCMaxLifetime int64
|
||||
// SessionProviderConfig is for the provider config, define save path or connection info.
|
||||
SessionProviderConfig string
|
||||
// SessionCookieLifeTime means the life time of session id in cookie.
|
||||
SessionCookieLifeTime int
|
||||
// SessionAutoSetCookie auto setcookie
|
||||
SessionAutoSetCookie bool
|
||||
// SessionDomain means the cookie domain default is empty
|
||||
SessionDomain string
|
||||
// StaticDir store the static path, key is path, value is the folder
|
||||
StaticDir map[string]string
|
||||
// StaticExtensionsToGzip stores the extensions which need to gzip(.js,.css,etc)
|
||||
StaticExtensionsToGzip []string
|
||||
// TemplateCache store the caching template
|
||||
TemplateCache map[string]*template.Template
|
||||
// TemplateLeft left delimiter
|
||||
TemplateLeft string
|
||||
// TemplateRight right delimiter
|
||||
TemplateRight string
|
||||
// ViewsPath means the template folder
|
||||
ViewsPath string
|
||||
// XSRFKEY xsrf hash salt string.
|
||||
XSRFKEY string
|
||||
// XSRFExpire is the expiry of xsrf value.
|
||||
XSRFExpire int
|
||||
)
|
||||
|
||||
func init() {
|
||||
BConfig = &BeegoConfig{
|
||||
AppName: "beego",
|
||||
RunMode: "dev",
|
||||
RouterCaseSensitive: true,
|
||||
ServerName: "beegoServer:" + VERSION,
|
||||
RecoverPanic: true,
|
||||
CopyRequestBody: false,
|
||||
EnableGzip: false,
|
||||
MaxMemory: 1 << 26, //64MB
|
||||
EnableErrorsShow: true,
|
||||
Listen: Listen{
|
||||
Graceful: false,
|
||||
ServerTimeOut: 0,
|
||||
ListenTCP4: false,
|
||||
HTTPEnable: true,
|
||||
HTTPAddr: "",
|
||||
HTTPPort: 8080,
|
||||
HTTPSEnable: false,
|
||||
HTTPSAddr: "",
|
||||
HTTPSPort: 10443,
|
||||
HTTPSCertFile: "",
|
||||
HTTPSKeyFile: "",
|
||||
AdminEnable: false,
|
||||
AdminAddr: "",
|
||||
AdminPort: 8088,
|
||||
EnableFcgi: false,
|
||||
EnableStdIo: false,
|
||||
},
|
||||
WebConfig: WebConfig{
|
||||
AutoRender: true,
|
||||
EnableDocs: false,
|
||||
FlashName: "BEEGO_FLASH",
|
||||
FlashSeperator: "BEEGOFLASH",
|
||||
DirectoryIndex: false,
|
||||
StaticDir: map[string]string{"/static": "static"},
|
||||
StaticExtensionsToGzip: []string{".css", ".js"},
|
||||
TemplateLeft: "{{",
|
||||
TemplateRight: "}}",
|
||||
ViewsPath: "views",
|
||||
EnableXSRF: false,
|
||||
XSRFKEY: "beegoxsrf",
|
||||
XSRFExpire: 0,
|
||||
Session: SessionConfig{
|
||||
SessionOn: false,
|
||||
SessionProvider: "memory",
|
||||
SessionName: "beegosessionID",
|
||||
SessionGCMaxLifetime: 3600,
|
||||
SessionProviderConfig: "",
|
||||
SessionCookieLifeTime: 0, //set cookie default is the brower life
|
||||
SessionAutoSetCookie: true,
|
||||
SessionDomain: "",
|
||||
},
|
||||
},
|
||||
Log: LogConfig{
|
||||
AccessLogs: false,
|
||||
FileLineNum: true,
|
||||
Output: map[string]string{"console": ""},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ParseConfig parsed default config file.
|
||||
// now only support ini, next will support json.
|
||||
func ParseConfig() (err error) {
|
||||
if AppConfigPath == "" {
|
||||
if utils.FileExists(filepath.Join("conf", "app.conf")) {
|
||||
AppConfigPath = filepath.Join("conf", "app.conf")
|
||||
} else {
|
||||
ac := config.NewFakeConfig()
|
||||
AppConfig = &beegoAppConfig{ac}
|
||||
return
|
||||
}
|
||||
}
|
||||
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
envRunMode := os.Getenv("BEEGO_RUNMODE")
|
||||
// set the runmode first
|
||||
if envRunMode != "" {
|
||||
BConfig.RunMode = envRunMode
|
||||
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
|
||||
BConfig.RunMode = runmode
|
||||
}
|
||||
|
||||
BConfig.Listen.HTTPAddr = AppConfig.String("HTTPAddr")
|
||||
|
||||
if v, err := AppConfig.Int("HTTPPort"); err == nil {
|
||||
BConfig.Listen.HTTPPort = v
|
||||
}
|
||||
|
||||
if v, err := AppConfig.Bool("ListenTCP4"); err == nil {
|
||||
BConfig.Listen.ListenTCP4 = v
|
||||
}
|
||||
|
||||
if v, err := AppConfig.Bool("EnableHTTPListen"); err == nil {
|
||||
BConfig.Listen.HTTPEnable = v
|
||||
}
|
||||
|
||||
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
|
||||
BConfig.MaxMemory = maxmemory
|
||||
}
|
||||
|
||||
if appname := AppConfig.String("AppName"); appname != "" {
|
||||
BConfig.AppName = appname
|
||||
}
|
||||
|
||||
if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
|
||||
BConfig.WebConfig.AutoRender = autorender
|
||||
}
|
||||
|
||||
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
|
||||
BConfig.RecoverPanic = autorecover
|
||||
}
|
||||
|
||||
if views := AppConfig.String("ViewsPath"); views != "" {
|
||||
BConfig.WebConfig.ViewsPath = views
|
||||
}
|
||||
|
||||
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
|
||||
BConfig.WebConfig.Session.SessionOn = sessionon
|
||||
}
|
||||
|
||||
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
|
||||
BConfig.WebConfig.Session.SessionProvider = sessProvider
|
||||
}
|
||||
|
||||
if sessName := AppConfig.String("SessionName"); sessName != "" {
|
||||
BConfig.WebConfig.Session.SessionName = sessName
|
||||
}
|
||||
|
||||
if sessProvConfig := AppConfig.String("SessionProviderConfig"); sessProvConfig != "" {
|
||||
BConfig.WebConfig.Session.SessionProviderConfig = sessProvConfig
|
||||
}
|
||||
|
||||
if sessMaxLifeTime, err := AppConfig.Int64("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
|
||||
BConfig.WebConfig.Session.SessionGCMaxLifetime = sessMaxLifeTime
|
||||
}
|
||||
|
||||
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
|
||||
BConfig.WebConfig.Session.SessionCookieLifeTime = sesscookielifetime
|
||||
}
|
||||
|
||||
if enableFcgi, err := AppConfig.Bool("EnableFcgi"); err == nil {
|
||||
BConfig.Listen.EnableFcgi = enableFcgi
|
||||
}
|
||||
|
||||
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
|
||||
BConfig.EnableGzip = enablegzip
|
||||
}
|
||||
|
||||
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
|
||||
BConfig.WebConfig.DirectoryIndex = directoryindex
|
||||
}
|
||||
|
||||
if timeout, err := AppConfig.Int64("HTTPServerTimeOut"); err == nil {
|
||||
BConfig.Listen.ServerTimeOut = timeout
|
||||
}
|
||||
|
||||
if errorsshow, err := AppConfig.Bool("EnableErrorsShow"); err == nil {
|
||||
BConfig.EnableErrorsShow = errorsshow
|
||||
}
|
||||
|
||||
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
|
||||
BConfig.CopyRequestBody = copyrequestbody
|
||||
}
|
||||
|
||||
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
|
||||
BConfig.WebConfig.XSRFKEY = xsrfkey
|
||||
}
|
||||
|
||||
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
|
||||
BConfig.WebConfig.EnableXSRF = enablexsrf
|
||||
}
|
||||
|
||||
if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
|
||||
BConfig.WebConfig.XSRFExpire = expire
|
||||
}
|
||||
|
||||
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
|
||||
BConfig.WebConfig.TemplateLeft = tplleft
|
||||
}
|
||||
|
||||
if tplright := AppConfig.String("TemplateRight"); tplright != "" {
|
||||
BConfig.WebConfig.TemplateRight = tplright
|
||||
}
|
||||
|
||||
if httptls, err := AppConfig.Bool("EnableHTTPTLS"); err == nil {
|
||||
BConfig.Listen.HTTPSEnable = httptls
|
||||
}
|
||||
|
||||
if httpsport, err := AppConfig.Int("HTTPSPort"); err == nil {
|
||||
BConfig.Listen.HTTPSPort = httpsport
|
||||
}
|
||||
|
||||
if certfile := AppConfig.String("HTTPCertFile"); certfile != "" {
|
||||
BConfig.Listen.HTTPSCertFile = certfile
|
||||
}
|
||||
|
||||
if keyfile := AppConfig.String("HTTPKeyFile"); keyfile != "" {
|
||||
BConfig.Listen.HTTPSKeyFile = keyfile
|
||||
}
|
||||
|
||||
if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
|
||||
BConfig.ServerName = serverName
|
||||
}
|
||||
|
||||
if flashname := AppConfig.String("FlashName"); flashname != "" {
|
||||
BConfig.WebConfig.FlashName = flashname
|
||||
}
|
||||
|
||||
if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" {
|
||||
BConfig.WebConfig.FlashSeperator = flashseperator
|
||||
}
|
||||
|
||||
if sd := AppConfig.String("StaticDir"); sd != "" {
|
||||
for k := range BConfig.WebConfig.StaticDir {
|
||||
delete(BConfig.WebConfig.StaticDir, k)
|
||||
}
|
||||
sds := strings.Fields(sd)
|
||||
for _, v := range sds {
|
||||
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
|
||||
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
|
||||
} else {
|
||||
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
|
||||
extensions := strings.Split(sgz, ",")
|
||||
fileExts := []string{}
|
||||
for _, ext := range extensions {
|
||||
ext = strings.TrimSpace(ext)
|
||||
if ext == "" {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(ext, ".") {
|
||||
ext = "." + ext
|
||||
}
|
||||
fileExts = append(fileExts, ext)
|
||||
}
|
||||
if len(fileExts) > 0 {
|
||||
BConfig.WebConfig.StaticExtensionsToGzip = fileExts
|
||||
}
|
||||
}
|
||||
|
||||
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
|
||||
BConfig.Listen.AdminEnable = enableadmin
|
||||
}
|
||||
|
||||
if adminhttpaddr := AppConfig.String("AdminHTTPAddr"); adminhttpaddr != "" {
|
||||
BConfig.Listen.AdminAddr = adminhttpaddr
|
||||
}
|
||||
|
||||
if adminhttpport, err := AppConfig.Int("AdminHTTPPort"); err == nil {
|
||||
BConfig.Listen.AdminPort = adminhttpport
|
||||
}
|
||||
|
||||
if enabledocs, err := AppConfig.Bool("EnableDocs"); err == nil {
|
||||
BConfig.WebConfig.EnableDocs = enabledocs
|
||||
}
|
||||
|
||||
if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
|
||||
BConfig.RouterCaseSensitive = casesensitive
|
||||
}
|
||||
if graceful, err := AppConfig.Bool("Graceful"); err == nil {
|
||||
BConfig.Listen.Graceful = graceful
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type beegoAppConfig struct {
|
||||
innerConfig config.Configer
|
||||
}
|
||||
@ -154,7 +395,7 @@ func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, err
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Set(key, val string) error {
|
||||
err := b.innerConfig.Set(RunMode+"::"+key, val)
|
||||
err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val)
|
||||
if err == nil {
|
||||
return err
|
||||
}
|
||||
@ -162,7 +403,7 @@ func (b *beegoAppConfig) Set(key, val string) error {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) String(key string) string {
|
||||
v := b.innerConfig.String(RunMode + "::" + key)
|
||||
v := b.innerConfig.String(BConfig.RunMode + "::" + key)
|
||||
if v == "" {
|
||||
return b.innerConfig.String(key)
|
||||
}
|
||||
@ -170,7 +411,7 @@ func (b *beegoAppConfig) String(key string) string {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Strings(key string) []string {
|
||||
v := b.innerConfig.Strings(RunMode + "::" + key)
|
||||
v := b.innerConfig.Strings(BConfig.RunMode + "::" + key)
|
||||
if v[0] == "" {
|
||||
return b.innerConfig.Strings(key)
|
||||
}
|
||||
@ -178,7 +419,7 @@ func (b *beegoAppConfig) Strings(key string) []string {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int(key string) (int, error) {
|
||||
v, err := b.innerConfig.Int(RunMode + "::" + key)
|
||||
v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key)
|
||||
if err != nil {
|
||||
return b.innerConfig.Int(key)
|
||||
}
|
||||
@ -186,7 +427,7 @@ func (b *beegoAppConfig) Int(key string) (int, error) {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int64(key string) (int64, error) {
|
||||
v, err := b.innerConfig.Int64(RunMode + "::" + key)
|
||||
v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key)
|
||||
if err != nil {
|
||||
return b.innerConfig.Int64(key)
|
||||
}
|
||||
@ -194,7 +435,7 @@ func (b *beegoAppConfig) Int64(key string) (int64, error) {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Bool(key string) (bool, error) {
|
||||
v, err := b.innerConfig.Bool(RunMode + "::" + key)
|
||||
v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key)
|
||||
if err != nil {
|
||||
return b.innerConfig.Bool(key)
|
||||
}
|
||||
@ -202,7 +443,7 @@ func (b *beegoAppConfig) Bool(key string) (bool, error) {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Float(key string) (float64, error) {
|
||||
v, err := b.innerConfig.Float(RunMode + "::" + key)
|
||||
v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key)
|
||||
if err != nil {
|
||||
return b.innerConfig.Float(key)
|
||||
}
|
||||
@ -268,300 +509,3 @@ func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
|
||||
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
|
||||
return b.innerConfig.SaveConfigFile(filename)
|
||||
}
|
||||
|
||||
func init() {
|
||||
workPath, _ = os.Getwd()
|
||||
workPath, _ = filepath.Abs(workPath)
|
||||
// initialize default configurations
|
||||
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
|
||||
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
||||
|
||||
if workPath != AppPath {
|
||||
if utils.FileExists(AppConfigPath) {
|
||||
os.Chdir(AppPath)
|
||||
} else {
|
||||
AppConfigPath = filepath.Join(workPath, "conf", "app.conf")
|
||||
}
|
||||
}
|
||||
|
||||
AppConfigProvider = "ini"
|
||||
|
||||
StaticDir = make(map[string]string)
|
||||
StaticDir["/static"] = "static"
|
||||
|
||||
StaticExtensionsToGzip = []string{".css", ".js"}
|
||||
|
||||
TemplateCache = make(map[string]*template.Template)
|
||||
|
||||
// set this to 0.0.0.0 to make this app available to externally
|
||||
EnableHTTPListen = true //default enable http Listen
|
||||
|
||||
HTTPAddr = ""
|
||||
HTTPPort = 8080
|
||||
|
||||
HTTPSPort = 10443
|
||||
|
||||
AppName = "beego"
|
||||
|
||||
RunMode = "dev" //default runmod
|
||||
|
||||
AutoRender = true
|
||||
|
||||
RecoverPanic = true
|
||||
|
||||
ViewsPath = "views"
|
||||
|
||||
SessionOn = false
|
||||
SessionProvider = "memory"
|
||||
SessionName = "beegosessionID"
|
||||
SessionGCMaxLifetime = 3600
|
||||
SessionProviderConfig = ""
|
||||
SessionCookieLifeTime = 0 //set cookie default is the brower life
|
||||
SessionAutoSetCookie = true
|
||||
|
||||
MaxMemory = 1 << 26 //64MB
|
||||
|
||||
HTTPServerTimeOut = 0
|
||||
|
||||
EnableErrorsShow = true
|
||||
|
||||
XSRFKEY = "beegoxsrf"
|
||||
XSRFExpire = 0
|
||||
|
||||
TemplateLeft = "{{"
|
||||
TemplateRight = "}}"
|
||||
|
||||
BeegoServerName = "beegoServer:" + VERSION
|
||||
|
||||
EnableAdmin = false
|
||||
AdminHTTPAddr = "127.0.0.1"
|
||||
AdminHTTPPort = 8088
|
||||
|
||||
FlashName = "BEEGO_FLASH"
|
||||
FlashSeperator = "BEEGOFLASH"
|
||||
|
||||
RouterCaseSensitive = true
|
||||
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
||||
// init BeeLogger
|
||||
BeeLogger = logs.NewLogger(10000)
|
||||
err := BeeLogger.SetLogger("console", "")
|
||||
if err != nil {
|
||||
fmt.Println("init console log error:", err)
|
||||
}
|
||||
SetLogFuncCall(true)
|
||||
|
||||
err = ParseConfig()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// for init if doesn't have app.conf will not panic
|
||||
ac := config.NewFakeConfig()
|
||||
AppConfig = &beegoAppConfig{ac}
|
||||
}
|
||||
Warning(err)
|
||||
}
|
||||
}
|
||||
|
||||
// ParseConfig parsed default config file.
|
||||
// now only support ini, next will support json.
|
||||
func ParseConfig() (err error) {
|
||||
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
envRunMode := os.Getenv("BEEGO_RUNMODE")
|
||||
// set the runmode first
|
||||
if envRunMode != "" {
|
||||
RunMode = envRunMode
|
||||
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
|
||||
RunMode = runmode
|
||||
}
|
||||
|
||||
HTTPAddr = AppConfig.String("HTTPAddr")
|
||||
|
||||
if v, err := AppConfig.Int("HTTPPort"); err == nil {
|
||||
HTTPPort = v
|
||||
}
|
||||
|
||||
if v, err := AppConfig.Bool("ListenTCP4"); err == nil {
|
||||
ListenTCP4 = v
|
||||
}
|
||||
|
||||
if v, err := AppConfig.Bool("EnableHTTPListen"); err == nil {
|
||||
EnableHTTPListen = v
|
||||
}
|
||||
|
||||
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
|
||||
MaxMemory = maxmemory
|
||||
}
|
||||
|
||||
if appname := AppConfig.String("AppName"); appname != "" {
|
||||
AppName = appname
|
||||
}
|
||||
|
||||
if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
|
||||
AutoRender = autorender
|
||||
}
|
||||
|
||||
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
|
||||
RecoverPanic = autorecover
|
||||
}
|
||||
|
||||
if views := AppConfig.String("ViewsPath"); views != "" {
|
||||
ViewsPath = views
|
||||
}
|
||||
|
||||
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
|
||||
SessionOn = sessionon
|
||||
}
|
||||
|
||||
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
|
||||
SessionProvider = sessProvider
|
||||
}
|
||||
|
||||
if sessName := AppConfig.String("SessionName"); sessName != "" {
|
||||
SessionName = sessName
|
||||
}
|
||||
|
||||
if sessProvConfig := AppConfig.String("SessionProviderConfig"); sessProvConfig != "" {
|
||||
SessionProviderConfig = sessProvConfig
|
||||
}
|
||||
|
||||
if sessMaxLifeTime, err := AppConfig.Int64("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
|
||||
SessionGCMaxLifetime = sessMaxLifeTime
|
||||
}
|
||||
|
||||
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
|
||||
SessionCookieLifeTime = sesscookielifetime
|
||||
}
|
||||
|
||||
if enableFcgi, err := AppConfig.Bool("EnableFcgi"); err == nil {
|
||||
EnableFcgi = enableFcgi
|
||||
}
|
||||
|
||||
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
|
||||
EnableGzip = enablegzip
|
||||
}
|
||||
|
||||
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
|
||||
DirectoryIndex = directoryindex
|
||||
}
|
||||
|
||||
if timeout, err := AppConfig.Int64("HTTPServerTimeOut"); err == nil {
|
||||
HTTPServerTimeOut = timeout
|
||||
}
|
||||
|
||||
if errorsshow, err := AppConfig.Bool("EnableErrorsShow"); err == nil {
|
||||
EnableErrorsShow = errorsshow
|
||||
}
|
||||
|
||||
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
|
||||
CopyRequestBody = copyrequestbody
|
||||
}
|
||||
|
||||
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
|
||||
XSRFKEY = xsrfkey
|
||||
}
|
||||
|
||||
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
|
||||
EnableXSRF = enablexsrf
|
||||
}
|
||||
|
||||
if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
|
||||
XSRFExpire = expire
|
||||
}
|
||||
|
||||
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
|
||||
TemplateLeft = tplleft
|
||||
}
|
||||
|
||||
if tplright := AppConfig.String("TemplateRight"); tplright != "" {
|
||||
TemplateRight = tplright
|
||||
}
|
||||
|
||||
if httptls, err := AppConfig.Bool("EnableHTTPTLS"); err == nil {
|
||||
EnableHTTPTLS = httptls
|
||||
}
|
||||
|
||||
if httpsport, err := AppConfig.Int("HTTPSPort"); err == nil {
|
||||
HTTPSPort = httpsport
|
||||
}
|
||||
|
||||
if certfile := AppConfig.String("HTTPCertFile"); certfile != "" {
|
||||
HTTPCertFile = certfile
|
||||
}
|
||||
|
||||
if keyfile := AppConfig.String("HTTPKeyFile"); keyfile != "" {
|
||||
HTTPKeyFile = keyfile
|
||||
}
|
||||
|
||||
if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
|
||||
BeegoServerName = serverName
|
||||
}
|
||||
|
||||
if flashname := AppConfig.String("FlashName"); flashname != "" {
|
||||
FlashName = flashname
|
||||
}
|
||||
|
||||
if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" {
|
||||
FlashSeperator = flashseperator
|
||||
}
|
||||
|
||||
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["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
|
||||
} else {
|
||||
StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
|
||||
extensions := strings.Split(sgz, ",")
|
||||
fileExts := []string{}
|
||||
for _, ext := range extensions {
|
||||
ext = strings.TrimSpace(ext)
|
||||
if ext == "" {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(ext, ".") {
|
||||
ext = "." + ext
|
||||
}
|
||||
fileExts = append(fileExts, ext)
|
||||
}
|
||||
if len(fileExts) > 0 {
|
||||
StaticExtensionsToGzip = fileExts
|
||||
}
|
||||
}
|
||||
|
||||
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
|
||||
EnableAdmin = enableadmin
|
||||
}
|
||||
|
||||
if adminhttpaddr := AppConfig.String("AdminHTTPAddr"); adminhttpaddr != "" {
|
||||
AdminHTTPAddr = adminhttpaddr
|
||||
}
|
||||
|
||||
if adminhttpport, err := AppConfig.Int("AdminHTTPPort"); err == nil {
|
||||
AdminHTTPPort = adminhttpport
|
||||
}
|
||||
|
||||
if enabledocs, err := AppConfig.Bool("EnableDocs"); err == nil {
|
||||
EnableDocs = enabledocs
|
||||
}
|
||||
|
||||
if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
|
||||
RouterCaseSensitive = casesensitive
|
||||
}
|
||||
if graceful, err := AppConfig.Bool("Graceful"); err == nil {
|
||||
Graceful = graceful
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user