From cbc7f43e88984a145d7ba85ff2ab7fb0dfc9e6e9 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Sat, 23 Jan 2016 02:32:09 +0800 Subject: [PATCH 01/12] fix issue #1601 --- beego.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/beego.go b/beego.go index 04f02071..7aa6b221 100644 --- a/beego.go +++ b/beego.go @@ -69,10 +69,13 @@ func Run(params ...string) { func initBeforeHTTPRun() { // if AppConfigPath is setted or conf/app.conf exist + /* err := ParseConfig() if err != nil { panic(err) } + */ + var err error //init log for adaptor, config := range BConfig.Log.Outputs { err = BeeLogger.SetLogger(adaptor, config) From d9e6250d08aa12e1f84e4236b1994e2ecbae7679 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 00:10:21 +0800 Subject: [PATCH 02/12] fix config logic --- beego.go | 12 +-------- config.go | 80 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/beego.go b/beego.go index 7aa6b221..68a76e52 100644 --- a/beego.go +++ b/beego.go @@ -15,7 +15,6 @@ package beego import ( - "fmt" "os" "path/filepath" "strconv" @@ -76,15 +75,6 @@ func initBeforeHTTPRun() { } */ 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 AddAPPStartHook(registerMime) @@ -95,7 +85,7 @@ func initBeforeHTTPRun() { AddAPPStartHook(registerAdmin) for _, hk := range hooks { - if err := hk(); err != nil { + if err = hk(); err != nil { panic(err) } } diff --git a/config.go b/config.go index e91ca28b..246c53f0 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "strings" + "fmt" "github.com/astaxie/beego/config" "github.com/astaxie/beego/session" @@ -103,14 +104,15 @@ var ( BConfig *Config // AppConfig is the instance of Config, store the config information from file 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 map[string]*template.Template // GlobalSessions is the instance for the 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() { @@ -173,22 +175,26 @@ func init() { 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. -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) { - AppConfig = &beegoAppConfig{config.NewFakeConfig()} - return - } - } - AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) +func parseConfig(appConfigPath string) (err error) { + AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath) if err != nil { return err } @@ -242,6 +248,8 @@ func ParseConfig() (err error) { 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.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 != "" { for k := range BConfig.WebConfig.StaticDir { @@ -274,9 +282,47 @@ func ParseConfig() (err error) { 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 + } + + AppConfigPath = absConfigPath + AppConfigProvider = adapterName + + return parseConfig(AppConfigPath) +} + type beegoAppConfig struct { innerConfig config.Configer } From 330b3b193118b58cd442e8004532d48033035cea Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 00:17:56 +0800 Subject: [PATCH 03/12] enhancement code --- config.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config.go b/config.go index 246c53f0..a86ad34c 100644 --- a/config.go +++ b/config.go @@ -20,6 +20,7 @@ import ( "path/filepath" "strings" "fmt" + "errors" "github.com/astaxie/beego/config" "github.com/astaxie/beego/session" @@ -313,6 +314,11 @@ func LoadAppConfig(configPath string, adapterName string) error { if err != nil { return err } + + if !utils.FileExists(absConfigPath) { + return errors.New(fmt.Sprintf("the target config file: %s don't exist!", configPath)) + } + if absConfigPath == AppConfigPath { return nil } From 20efd5236e7e5f6a99a44d53e5ac7a54adb53b9e Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 00:42:07 +0800 Subject: [PATCH 04/12] fix bug --- logs/log.go | 1 + 1 file changed, 1 insertion(+) diff --git a/logs/log.go b/logs/log.go index ccaaa3ad..14757929 100644 --- a/logs/log.go +++ b/logs/log.go @@ -361,4 +361,5 @@ func (bl *BeeLogger) Close() { l.Flush() l.Destroy() } + bl.outputs = nil } From e549d0fd9c6675e11a08af96fdb4e04511389ef1 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 12:13:26 +0800 Subject: [PATCH 05/12] move some code piece --- config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 570faacb..e72f770c 100644 --- a/config.go +++ b/config.go @@ -124,6 +124,10 @@ func init() { workPath, _ = os.Getwd() workPath, _ = filepath.Abs(workPath) + if workPath != AppPath { + os.Chdir(AppPath) + } + BConfig = &Config{ AppName: "beego", RunMode: DEV, @@ -195,10 +199,6 @@ func init() { // now only support ini, next will support json. func parseConfig(appConfigPath string) (err error) { - if workPath != AppPath { - os.Chdir(AppPath) - } - AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath) if err != nil { return err From 321bcc606af089c74a7b980ba4962bb81cf2f862 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 12:26:37 +0800 Subject: [PATCH 06/12] fix bug of test case --- staticfile_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/staticfile_test.go b/staticfile_test.go index d3333570..e7003366 100644 --- a/staticfile_test.go +++ b/staticfile_test.go @@ -8,9 +8,11 @@ import ( "io/ioutil" "os" "testing" + "path/filepath" ) -const licenseFile = "./LICENSE" +var currentWorkDir, _ = os.Getwd() +var licenseFile = filepath.Join(currentWorkDir, "LICENSE") func testOpenFile(encoding string, content []byte, t *testing.T) { fi, _ := os.Stat(licenseFile) From e357f6846bd5f07ef3baead76658d4dbb926d5fc Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 13:30:34 +0800 Subject: [PATCH 07/12] accept @ysqi suggestion --- beego.go | 11 +---------- config.go | 3 +-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/beego.go b/beego.go index 68a76e52..65368295 100644 --- a/beego.go +++ b/beego.go @@ -67,15 +67,6 @@ func Run(params ...string) { } func initBeforeHTTPRun() { - // if AppConfigPath is setted or conf/app.conf exist - /* - err := ParseConfig() - if err != nil { - panic(err) - } - */ - var err error - //init hooks AddAPPStartHook(registerMime) AddAPPStartHook(registerDefaultErrorHandler) @@ -85,7 +76,7 @@ func initBeforeHTTPRun() { AddAPPStartHook(registerAdmin) for _, hk := range hooks { - if err = hk(); err != nil { + if err := hk(); err != nil { panic(err) } } diff --git a/config.go b/config.go index e72f770c..ebc054d6 100644 --- a/config.go +++ b/config.go @@ -20,7 +20,6 @@ import ( "path/filepath" "strings" "fmt" - "errors" "github.com/astaxie/beego/config" "github.com/astaxie/beego/session" @@ -320,7 +319,7 @@ func LoadAppConfig(configPath string, adapterName string) error { } if !utils.FileExists(absConfigPath) { - return errors.New(fmt.Sprintf("the target config file: %s don't exist!", configPath)) + return fmt.Errorf("the target config file: %s don't exist!", configPath) } if absConfigPath == AppConfigPath { From ccce566ba7a41589fb31814b34d6ad50c81dcf9d Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 16:19:10 +0800 Subject: [PATCH 08/12] accept @astaxie suggestion: change the sequence adapterName and configPath --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index ebc054d6..e0439084 100644 --- a/config.go +++ b/config.go @@ -312,7 +312,7 @@ func parseConfig(appConfigPath string) (err error) { } // LoadAppConfig allow developer to apply a config file -func LoadAppConfig(configPath string, adapterName string) error { +func LoadAppConfig(adapterName string, configPath string) error { absConfigPath, err := filepath.Abs(configPath) if err != nil { return err From 31ef4ae507cf3395cb61dc2c96d5416b7695b3df Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 16:43:01 +0800 Subject: [PATCH 09/12] rename AppConfigPath, AppConfigProvider to appConfigPath, appConfigProvider, make public to private --- admin.go | 4 ++-- beego.go | 2 +- config.go | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/admin.go b/admin.go index 3effc582..031e6421 100644 --- a/admin.go +++ b/admin.go @@ -90,8 +90,8 @@ func listConf(rw http.ResponseWriter, r *http.Request) { switch command { case "conf": m := make(map[string]interface{}) - m["AppConfigPath"] = AppConfigPath - m["AppConfigProvider"] = AppConfigProvider + m["AppConfigPath"] = appConfigPath + m["AppConfigProvider"] = appConfigProvider m["BConfig.AppName"] = BConfig.AppName m["BConfig.RunMode"] = BConfig.RunMode m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive diff --git a/beego.go b/beego.go index 65368295..fb628e5f 100644 --- a/beego.go +++ b/beego.go @@ -85,7 +85,7 @@ func initBeforeHTTPRun() { // TestBeegoInit is for test package init func TestBeegoInit(ap string) { os.Setenv("BEEGO_RUNMODE", "test") - AppConfigPath = filepath.Join(ap, "conf", "app.conf") + appConfigPath = filepath.Join(ap, "conf", "app.conf") os.Chdir(ap) initBeforeHTTPRun() } diff --git a/config.go b/config.go index e0439084..7e2b3ee7 100644 --- a/config.go +++ b/config.go @@ -106,16 +106,16 @@ var ( AppConfig *beegoAppConfig // AppPath is the absolute path to the app AppPath string - // 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 map[string]*template.Template // GlobalSessions is the instance for the session manager GlobalSessions *session.Manager workPath string + // appConfigPath is the path to the config files + appConfigPath string + // appConfigProvider is the provider for the config, default is ini + appConfigProvider = "ini" ) func init() { @@ -187,18 +187,18 @@ func init() { }, } - AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") - if !utils.FileExists(AppConfigPath) { + appConfigPath = filepath.Join(AppPath, "conf", "app.conf") + if !utils.FileExists(appConfigPath) { AppConfig = &beegoAppConfig{config.NewFakeConfig()} return } - parseConfig(AppConfigPath) + parseConfig(appConfigPath) } // now only support ini, next will support json. func parseConfig(appConfigPath string) (err error) { - AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath) + AppConfig, err = newAppConfig(appConfigProvider, appConfigPath) if err != nil { return err } @@ -312,7 +312,7 @@ func parseConfig(appConfigPath string) (err error) { } // LoadAppConfig allow developer to apply a config file -func LoadAppConfig(adapterName string, configPath string) error { +func LoadAppConfig(adapterName, configPath string) error { absConfigPath, err := filepath.Abs(configPath) if err != nil { return err @@ -322,22 +322,22 @@ func LoadAppConfig(adapterName string, configPath string) error { return fmt.Errorf("the target config file: %s don't exist!", configPath) } - if absConfigPath == AppConfigPath { + if absConfigPath == appConfigPath { return nil } - AppConfigPath = absConfigPath - AppConfigProvider = adapterName + appConfigPath = absConfigPath + appConfigProvider = adapterName - return parseConfig(AppConfigPath) + return parseConfig(appConfigPath) } type beegoAppConfig struct { innerConfig config.Configer } -func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) { - ac, err := config.NewConfig(AppConfigProvider, AppConfigPath) +func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, error) { + ac, err := config.NewConfig(appConfigProvider, appConfigPath) if err != nil { return nil, err } From 453d744db92323180861705d1d6c35fbb75a4190 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Wed, 27 Jan 2016 14:58:50 +0100 Subject: [PATCH 10/12] Fix Context.Input.SetParam not overwriting existing value - Also added tests for Context.Input.Param handling --- context/input.go | 7 ++++++ context/input_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/context/input.go b/context/input.go index c37204bd..edfdf530 100644 --- a/context/input.go +++ b/context/input.go @@ -287,6 +287,13 @@ func (input *BeegoInput) Params() map[string]string { // SetParam will set the param with key and value func (input *BeegoInput) SetParam(key, val string) { + // check if already exists + for i, v := range input.pnames { + if v == key && i <= len(input.pvalues) { + input.pvalues[i] = val + return + } + } input.pvalues = append(input.pvalues, val) input.pnames = append(input.pnames, key) } diff --git a/context/input_test.go b/context/input_test.go index 618e1254..24f6fd99 100644 --- a/context/input_test.go +++ b/context/input_test.go @@ -18,6 +18,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "reflect" "testing" ) @@ -117,3 +118,56 @@ func TestSubDomain(t *testing.T) { t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains()) } } + +func TestParams(t *testing.T) { + inp := NewInput() + + inp.SetParam("p1", "val1_ver1") + inp.SetParam("p2", "val2_ver1") + inp.SetParam("p3", "val3_ver1") + if l := inp.ParamsLen(); l != 3 { + t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3) + } + + if val := inp.Param("p1"); val != "val1_ver1" { + t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1") + } + if val := inp.Param("p3"); val != "val3_ver1" { + t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1") + } + vals := inp.Params() + expected := map[string]string{ + "p1": "val1_ver1", + "p2": "val2_ver1", + "p3": "val3_ver1", + } + if !reflect.DeepEqual(vals, expected) { + t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected) + } + + // overwriting existing params + inp.SetParam("p1", "val1_ver2") + inp.SetParam("p2", "val2_ver2") + expected = map[string]string{ + "p1": "val1_ver2", + "p2": "val2_ver2", + "p3": "val3_ver1", + } + vals = inp.Params() + if !reflect.DeepEqual(vals, expected) { + t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected) + } + + if l := inp.ParamsLen(); l != 3 { + t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3) + } + + if val := inp.Param("p1"); val != "val1_ver2" { + t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2") + } + + if val := inp.Param("p2"); val != "val2_ver2" { + t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2") + } + +} From 7f888e3d18ff735eea10b2b3c6dab91ce90134d1 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Wed, 27 Jan 2016 19:20:58 +0100 Subject: [PATCH 11/12] Add noColor option for console logger - Also added simple test --- logs/console.go | 7 ++++--- logs/console_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/logs/console.go b/logs/console.go index 10d8e8b2..871e7972 100644 --- a/logs/console.go +++ b/logs/console.go @@ -47,8 +47,9 @@ var colors = []brush{ // consoleWriter implements LoggerInterface and writes messages to terminal. type consoleWriter struct { - lg *log.Logger - Level int `json:"level"` + lg *log.Logger + Level int `json:"level"` + NoColor bool `json:"noColor"` } // NewConsole create ConsoleWriter returning as LoggerInterface. @@ -75,7 +76,7 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error { return nil } msg = formatLogTime(when) + msg - if runtime.GOOS == "windows" { + if runtime.GOOS == "windows" || c.NoColor { c.lg.Println(msg) return nil } diff --git a/logs/console_test.go b/logs/console_test.go index ce8937d4..45211588 100644 --- a/logs/console_test.go +++ b/logs/console_test.go @@ -42,3 +42,10 @@ func TestConsole(t *testing.T) { log2.SetLogger("console", `{"level":3}`) testConsoleCalls(log2) } + +// Test console without color +func TestConsoleNoColor(t *testing.T) { + log := NewLogger(100) + log.SetLogger("console", `{"noColor":true}`) + testConsoleCalls(log) +} From a0434323982ac73a406177bec245b3286e484378 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Thu, 28 Jan 2016 07:50:07 +0100 Subject: [PATCH 12/12] Change option name from noColor to color Still same default behavior (with color) --- logs/console.go | 9 +++++---- logs/console_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/logs/console.go b/logs/console.go index 871e7972..d7ed8d8e 100644 --- a/logs/console.go +++ b/logs/console.go @@ -47,9 +47,9 @@ var colors = []brush{ // consoleWriter implements LoggerInterface and writes messages to terminal. type consoleWriter struct { - lg *log.Logger - Level int `json:"level"` - NoColor bool `json:"noColor"` + lg *log.Logger + Level int `json:"level"` + Color bool `json:"color"` } // NewConsole create ConsoleWriter returning as LoggerInterface. @@ -57,6 +57,7 @@ func NewConsole() Logger { cw := &consoleWriter{ lg: log.New(os.Stdout, "", 0), Level: LevelDebug, + Color: true, } return cw } @@ -76,7 +77,7 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error { return nil } msg = formatLogTime(when) + msg - if runtime.GOOS == "windows" || c.NoColor { + if runtime.GOOS == "windows" || !c.Color { c.lg.Println(msg) return nil } diff --git a/logs/console_test.go b/logs/console_test.go index 45211588..04f2bd7e 100644 --- a/logs/console_test.go +++ b/logs/console_test.go @@ -46,6 +46,6 @@ func TestConsole(t *testing.T) { // Test console without color func TestConsoleNoColor(t *testing.T) { log := NewLogger(100) - log.SetLogger("console", `{"noColor":true}`) + log.SetLogger("console", `{"color":false}`) testConsoleCalls(log) }