1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-16 17:20:55 +00:00
Beego/config.go

485 lines
13 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package beego
import (
2014-04-04 01:49:55 +00:00
"fmt"
"html/template"
"os"
"path/filepath"
"runtime"
"strings"
2013-12-03 11:26:51 +00:00
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/logs"
2013-12-03 11:26:51 +00:00
"github.com/astaxie/beego/session"
"github.com/astaxie/beego/utils"
)
var (
2013-12-20 15:15:00 +00:00
BeeApp *App // beego application
AppName string
AppPath string
workPath string
2013-12-20 15:15:00 +00:00
AppConfigPath string
StaticDir map[string]string
TemplateCache map[string]*template.Template // template caching map
StaticExtensionsToGzip []string // files with should be compressed with gzip (.js,.css,etc)
2014-05-20 07:30:17 +00:00
EnableHttpListen bool
2013-12-20 15:15:00 +00:00
HttpAddr string
HttpPort int
ListenTCP4 bool
2014-05-20 07:30:17 +00:00
EnableHttpTLS bool
HttpsPort int
2013-12-20 15:15:00 +00:00
HttpCertFile string
HttpKeyFile string
RecoverPanic bool // flag of auto recover panic
AutoRender bool // flag of render template automatically
ViewsPath string
2014-10-01 14:10:33 +00:00
AppConfig *beegoAppConfig
RunMode string // run mode, "dev" or "prod"
2013-12-20 15:15:00 +00:00
GlobalSessions *session.Manager // global session mananger
SessionOn bool // flag of starting session auto. default is false.
SessionProvider string // default session provider, memory, mysql , redis ,etc.
SessionName string // the cookie name when saving session id into cookie.
SessionGCMaxLifetime int64 // session gc time for auto cleaning expired session.
SessionSavePath string // if use mysql/redis/file provider, define save path to connection info.
SessionCookieLifeTime int // the life time of session id in cookie.
2014-01-05 06:59:39 +00:00
SessionAutoSetCookie bool // auto setcookie
2014-08-04 08:21:06 +00:00
SessionDomain string // the cookie domain default is empty
2013-12-20 15:15:00 +00:00
UseFcgi bool
2014-10-13 11:47:44 +00:00
UseStdIo bool
2013-12-20 15:15:00 +00:00
MaxMemory int64
EnableGzip bool // flag of enable gzip
DirectoryIndex bool // flag of display directory index. default is false.
HttpServerTimeOut int64
ErrorsShow bool // flag of show errors in page. if true, show error and trace info in page rendered with error template.
XSRFKEY string // xsrf hash salt string.
EnableXSRF bool // flag of enable xsrf.
XSRFExpire int // the expiry of xsrf value.
CopyRequestBody bool // flag of copy raw request body in context.
TemplateLeft string
TemplateRight string
BeegoServerName string // beego server name exported in response header.
EnableAdmin bool // flag of enable admin module to log every request info.
AdminHttpAddr string // http server configurations for admin module.
AdminHttpPort int
FlashName string // name of the flash variable found in response header and cookie
FlashSeperator string // used to seperate flash key:value
2014-05-25 14:37:38 +00:00
AppConfigProvider string // config provider
2014-06-16 08:05:15 +00:00
EnableDocs bool // enable generate docs & server docs API Swagger
2014-09-28 14:10:43 +00:00
RouterCaseSensitive bool // router case sensitive default is true
)
2014-10-09 13:17:10 +00:00
type beegoAppConfig struct {
innerConfig config.ConfigContainer
}
2014-10-01 14:10:33 +00:00
2014-10-24 11:03:27 +00:00
func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) {
2014-10-01 14:10:33 +00:00
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath)
if err != nil {
2014-10-24 11:03:27 +00:00
return nil, err
2014-10-01 14:10:33 +00:00
}
rac := &beegoAppConfig{ac}
2014-10-24 11:03:27 +00:00
return rac, nil
2014-10-01 14:10:33 +00:00
}
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)
2014-11-20 08:35:04 +00:00
if v[0] == "" {
2014-10-01 14:10:33 +00:00
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)
}
2013-12-04 09:03:49 +00:00
func init() {
// create beego application
2013-12-04 09:03:49 +00:00
BeeApp = NewApp()
2013-12-04 15:53:36 +00:00
workPath, _ = os.Getwd()
workPath, _ = filepath.Abs(workPath)
2013-12-04 15:53:36 +00:00
// 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")
}
}
2013-12-04 15:53:36 +00:00
2014-05-25 14:37:38 +00:00
AppConfigProvider = "ini"
StaticDir = make(map[string]string)
2013-12-04 15:53:36 +00:00
StaticDir["/static"] = "static"
2013-12-14 18:34:27 +00:00
StaticExtensionsToGzip = []string{".css", ".js"}
2013-12-04 15:53:36 +00:00
2013-12-06 05:46:14 +00:00
TemplateCache = make(map[string]*template.Template)
2013-12-04 15:53:36 +00:00
// set this to 0.0.0.0 to make this app available to externally
2014-05-20 07:30:17 +00:00
EnableHttpListen = true //default enable http Listen
2014-05-20 08:41:39 +00:00
2013-12-07 09:22:57 +00:00
HttpAddr = ""
HttpPort = 8080
2013-12-04 15:53:36 +00:00
2014-05-20 07:40:05 +00:00
HttpsPort = 10443
2014-05-20 07:30:17 +00:00
AppName = "beego"
2013-12-04 15:53:36 +00:00
RunMode = "dev" //default runmod
2013-12-04 15:53:36 +00:00
AutoRender = true
2013-12-04 15:53:36 +00:00
RecoverPanic = true
2013-12-04 15:53:36 +00:00
ViewsPath = "views"
2013-12-04 15:53:36 +00:00
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
SessionSavePath = ""
2013-12-12 02:43:13 +00:00
SessionCookieLifeTime = 0 //set cookie default is the brower life
2014-01-05 06:59:39 +00:00
SessionAutoSetCookie = true
2013-12-04 15:53:36 +00:00
UseFcgi = false
2014-10-13 11:47:44 +00:00
UseStdIo = false
2013-12-04 15:53:36 +00:00
2013-12-20 14:35:16 +00:00
MaxMemory = 1 << 26 //64MB
2013-12-04 15:53:36 +00:00
EnableGzip = false
2013-12-04 15:53:36 +00:00
HttpServerTimeOut = 0
2013-12-04 15:53:36 +00:00
ErrorsShow = true
2013-12-04 15:53:36 +00:00
XSRFKEY = "beegoxsrf"
XSRFExpire = 0
2013-12-04 15:53:36 +00:00
TemplateLeft = "{{"
TemplateRight = "}}"
2013-12-04 15:53:36 +00:00
BeegoServerName = "beegoServer:" + VERSION
2013-12-04 15:53:36 +00:00
EnableAdmin = false
2013-12-04 15:53:36 +00:00
AdminHttpAddr = "127.0.0.1"
AdminHttpPort = 8088
2013-12-04 15:53:36 +00:00
FlashName = "BEEGO_FLASH"
FlashSeperator = "BEEGOFLASH"
2014-09-28 14:10:43 +00:00
RouterCaseSensitive = true
runtime.GOMAXPROCS(runtime.NumCPU())
2013-12-04 15:53:36 +00:00
// init BeeLogger
BeeLogger = logs.NewLogger(10000)
2014-04-04 01:49:55 +00:00
err := BeeLogger.SetLogger("console", "")
if err != nil {
fmt.Println("init console log error:", err)
}
2014-10-31 01:02:16 +00:00
SetLogFuncCall(true)
2014-04-04 01:49:55 +00:00
err = ParseConfig()
if err != nil && os.IsNotExist(err) {
// for init if doesn't have app.conf will not panic
2014-10-24 11:03:27 +00:00
ac := config.NewFakeConfig()
AppConfig = &beegoAppConfig{ac}
Warning(err)
2013-12-04 15:53:36 +00:00
}
}
// ParseConfig parsed default config file.
// now only support ini, next will support json.
func ParseConfig() (err error) {
2014-10-24 11:03:27 +00:00
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
if err != nil {
return err
}
2014-10-09 13:17:10 +00:00
envRunMode := os.Getenv("BEEGO_RUNMODE")
2014-10-01 14:10:33 +00:00
// set the runmode first
2014-10-09 13:17:10 +00:00
if envRunMode != "" {
RunMode = envRunMode
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
2014-10-01 14:10:33 +00:00
RunMode = runmode
}
2014-10-01 14:10:33 +00:00
HttpAddr = AppConfig.String("HttpAddr")
2014-10-01 14:10:33 +00:00
if v, err := AppConfig.Int("HttpPort"); err == nil {
HttpPort = v
}
2014-05-20 07:30:17 +00:00
if v, err := AppConfig.Bool("ListenTCP4"); err == nil {
ListenTCP4 = v
}
2014-10-01 14:10:33 +00:00
if v, err := AppConfig.Bool("EnableHttpListen"); err == nil {
EnableHttpListen = v
}
2014-10-01 14:10:33 +00:00
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
MaxMemory = maxmemory
}
2014-10-01 14:10:33 +00:00
if appname := AppConfig.String("AppName"); appname != "" {
AppName = appname
}
2014-10-01 14:10:33 +00:00
if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
AutoRender = autorender
}
2014-10-01 14:10:33 +00:00
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
RecoverPanic = autorecover
}
2014-10-01 14:10:33 +00:00
if views := AppConfig.String("ViewsPath"); views != "" {
ViewsPath = views
}
2014-10-01 14:10:33 +00:00
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
SessionOn = sessionon
}
2014-10-01 14:10:33 +00:00
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
SessionProvider = sessProvider
}
2014-10-01 14:10:33 +00:00
if sessName := AppConfig.String("SessionName"); sessName != "" {
SessionName = sessName
}
2014-10-01 14:10:33 +00:00
if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
SessionSavePath = sesssavepath
}
2014-10-01 14:10:33 +00:00
if sessMaxLifeTime, err := AppConfig.Int64("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
SessionGCMaxLifetime = sessMaxLifeTime
}
2014-10-01 14:10:33 +00:00
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionCookieLifeTime = sesscookielifetime
}
2014-10-01 14:10:33 +00:00
if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
UseFcgi = usefcgi
}
2014-10-01 14:10:33 +00:00
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
EnableGzip = enablegzip
}
2014-10-01 14:10:33 +00:00
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
DirectoryIndex = directoryindex
}
2014-10-01 14:10:33 +00:00
if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
HttpServerTimeOut = timeout
}
2014-10-01 14:10:33 +00:00
if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
ErrorsShow = errorsshow
}
2014-10-01 14:10:33 +00:00
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
CopyRequestBody = copyrequestbody
}
2014-10-01 14:10:33 +00:00
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
XSRFKEY = xsrfkey
}
2014-10-01 14:10:33 +00:00
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
EnableXSRF = enablexsrf
}
2014-10-01 14:10:33 +00:00
if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
XSRFExpire = expire
}
2014-10-01 14:10:33 +00:00
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
TemplateLeft = tplleft
}
2014-10-01 14:10:33 +00:00
if tplright := AppConfig.String("TemplateRight"); tplright != "" {
TemplateRight = tplright
}
2014-10-01 14:10:33 +00:00
if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil {
EnableHttpTLS = httptls
}
2014-05-20 07:30:17 +00:00
2014-10-01 14:10:33 +00:00
if httpsport, err := AppConfig.Int("HttpsPort"); err == nil {
HttpsPort = httpsport
}
2014-10-01 14:10:33 +00:00
if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
HttpCertFile = certfile
}
2014-10-01 14:10:33 +00:00
if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile
}
2014-10-01 14:10:33 +00:00
if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
BeegoServerName = serverName
}
2014-10-01 14:10:33 +00:00
if flashname := AppConfig.String("FlashName"); flashname != "" {
FlashName = flashname
}
2014-10-01 14:10:33 +00:00
if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" {
FlashSeperator = flashseperator
}
2014-10-01 14:10:33 +00:00
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]
}
}
2014-10-01 14:10:33 +00:00
}
2014-10-01 14:10:33 +00:00
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz, ",")
if len(extensions) > 0 {
StaticExtensionsToGzip = []string{}
for _, ext := range extensions {
if len(ext) == 0 {
continue
2013-12-14 18:34:27 +00:00
}
2014-10-01 14:10:33 +00:00
extWithDot := ext
if extWithDot[:1] != "." {
extWithDot = "." + extWithDot
}
StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot)
2013-12-14 18:34:27 +00:00
}
}
2014-10-01 14:10:33 +00:00
}
2014-10-01 14:10:33 +00:00
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
EnableAdmin = enableadmin
}
2014-10-01 14:10:33 +00:00
if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr
}
2014-06-16 08:05:15 +00:00
2014-10-01 14:10:33 +00:00
if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport
}
2014-09-28 14:10:43 +00:00
2014-10-01 14:10:33 +00:00
if enabledocs, err := AppConfig.Bool("EnableDocs"); err == nil {
EnableDocs = enabledocs
}
2014-10-01 14:10:33 +00:00
if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
RouterCaseSensitive = casesensitive
}
2014-10-01 14:10:33 +00:00
return nil
}