Commit d9e6250d authored by youngsterxyf's avatar youngsterxyf

fix config logic

parent cbc7f43e
...@@ -15,7 +15,6 @@ ...@@ -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() { ...@@ -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() { ...@@ -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)
} }
} }
......
...@@ -19,6 +19,7 @@ import ( ...@@ -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 ( ...@@ -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() { ...@@ -173,22 +175,26 @@ func init() {
Outputs: map[string]string{"console": ""}, Outputs: map[string]string{"console": ""},
}, },
} }
ParseConfig()
}
// ParseConfig parsed default config file. AppConfigPath = getDefaultAppConfigPath()
// now only support ini, next will support json.
func ParseConfig() (err error) {
if AppConfigPath == "" {
// initialize default configurations
AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
if !utils.FileExists(AppConfigPath) { if !utils.FileExists(AppConfigPath) {
AppConfig = &beegoAppConfig{config.NewFakeConfig()} AppConfig = &beegoAppConfig{config.NewFakeConfig()}
return return
} }
}
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) parseConfig(AppConfigPath)
}
func getDefaultAppConfigPath() string {
// default config path
AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
return filepath.Join(AppPath, "conf", "app.conf")
}
// now only support ini, next will support json.
func parseConfig(appConfigPath string) (err error) {
AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath)
if err != nil { if err != nil {
return err return err
} }
...@@ -242,6 +248,8 @@ func ParseConfig() (err error) { ...@@ -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,7 +282,45 @@ func ParseConfig() (err error) { ...@@ -274,7 +282,45 @@ 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
}
// 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 return nil
}
AppConfigPath = absConfigPath
AppConfigProvider = adapterName
return parseConfig(AppConfigPath)
} }
type beegoAppConfig struct { type beegoAppConfig struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment