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

fix config logic

This commit is contained in:
youngsterxyf 2016-01-27 00:10:21 +08:00
parent cbc7f43e88
commit d9e6250d08
2 changed files with 64 additions and 28 deletions

View File

@ -15,7 +15,6 @@
package beego package beego
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -76,15 +75,6 @@ func initBeforeHTTPRun() {
} }
*/ */
var err error var err error
//init log
for adaptor, config := range BConfig.Log.Outputs {
err = BeeLogger.SetLogger(adaptor, config)
if err != nil {
fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err)
}
}
SetLogFuncCall(BConfig.Log.FileLineNum)
//init hooks //init hooks
AddAPPStartHook(registerMime) AddAPPStartHook(registerMime)
@ -95,7 +85,7 @@ func initBeforeHTTPRun() {
AddAPPStartHook(registerAdmin) AddAPPStartHook(registerAdmin)
for _, hk := range hooks { for _, hk := range hooks {
if err := hk(); err != nil { if err = hk(); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -19,6 +19,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"fmt"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
@ -103,14 +104,15 @@ var (
BConfig *Config BConfig *Config
// AppConfig is the instance of Config, store the config information from file // AppConfig is the instance of Config, store the config information from file
AppConfig *beegoAppConfig AppConfig *beegoAppConfig
// AppConfigPath is the path to the config files
AppConfigPath string
// AppConfigProvider is the provider for the config, default is ini
AppConfigProvider = "ini"
// TemplateCache stores template caching // TemplateCache stores template caching
TemplateCache map[string]*template.Template TemplateCache map[string]*template.Template
// GlobalSessions is the instance for the session manager // GlobalSessions is the instance for the session manager
GlobalSessions *session.Manager GlobalSessions *session.Manager
// AppConfigPath is the path to the config files
AppConfigPath string
// AppConfigProvider is the provider for the config, default is ini
AppConfigProvider = "ini"
) )
func init() { func init() {
@ -173,22 +175,26 @@ func init() {
Outputs: map[string]string{"console": ""}, Outputs: map[string]string{"console": ""},
}, },
} }
ParseConfig()
AppConfigPath = getDefaultAppConfigPath()
if !utils.FileExists(AppConfigPath) {
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
return
}
parseConfig(AppConfigPath)
}
func getDefaultAppConfigPath() string {
// default config path
AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
return filepath.Join(AppPath, "conf", "app.conf")
} }
// 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(appConfigPath string) (err error) {
if AppConfigPath == "" { AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath)
// initialize default configurations
AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
if !utils.FileExists(AppConfigPath) {
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
return
}
}
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
if err != nil { if err != nil {
return err return err
} }
@ -242,6 +248,8 @@ func ParseConfig() (err error) {
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime) BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie) BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain) BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs)
BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum)
if sd := AppConfig.String("StaticDir"); sd != "" { if sd := AppConfig.String("StaticDir"); sd != "" {
for k := range BConfig.WebConfig.StaticDir { for k := range BConfig.WebConfig.StaticDir {
@ -274,9 +282,47 @@ func ParseConfig() (err error) {
BConfig.WebConfig.StaticExtensionsToGzip = fileExts BConfig.WebConfig.StaticExtensionsToGzip = fileExts
} }
} }
if lo := AppConfig.String("LogOutputs"); lo != "" {
los := strings.Split(lo, ";")
for _, v := range los {
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
BConfig.Log.Outputs[logType2Config[0]] = logType2Config[1]
} else {
continue
}
}
}
//init log
BeeLogger.Close()
for adaptor, config := range BConfig.Log.Outputs {
err = BeeLogger.SetLogger(adaptor, config)
if err != nil {
fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err)
}
}
SetLogFuncCall(BConfig.Log.FileLineNum)
return nil return nil
} }
// LoadAppConfig allow developer to apply a config file
func LoadAppConfig(configPath string, adapterName string) error {
absConfigPath, err := filepath.Abs(configPath)
if err != nil {
return err
}
if absConfigPath == AppConfigPath {
return nil
}
AppConfigPath = absConfigPath
AppConfigProvider = adapterName
return parseConfig(AppConfigPath)
}
type beegoAppConfig struct { type beegoAppConfig struct {
innerConfig config.Configer innerConfig config.Configer
} }