From 561e7115f37e454600f5ef842a0f65c08a0e88cb Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Tue, 29 Mar 2016 18:16:38 +0800 Subject: [PATCH 1/2] To support `go run` --- config.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 72045fd3..c1ee7ae6 100644 --- a/config.go +++ b/config.go @@ -106,6 +106,8 @@ var ( AppConfig *beegoAppConfig // AppPath is the absolute path to the app AppPath string + // workPath is the absolute path of the current dir + workPath string // GlobalSessions is the instance for the session manager GlobalSessions *session.Manager @@ -118,7 +120,8 @@ var ( func init() { AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) - os.Chdir(AppPath) + workPath, _ = os.Getwd() + workPath, _ = filepath.Abs(workPath) BConfig = &Config{ AppName: "beego", @@ -180,10 +183,13 @@ func init() { }, } - appConfigPath = filepath.Join(AppPath, "conf", "app.conf") + appConfigPath = filepath.Join(workPath, "conf", "app.conf") if !utils.FileExists(appConfigPath) { - AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()} - return + appConfigPath = filepath.Join(AppPath, "conf", "app.conf") + if !utils.FileExists(appConfigPath) { + AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()} + return + } } if err := parseConfig(appConfigPath); err != nil { From e281b6e82a49be5bfe5e0137f1d5c5101ca8ef0d Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 30 Mar 2016 10:49:39 +0800 Subject: [PATCH 2/2] improve --- config.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index c1ee7ae6..22074bd7 100644 --- a/config.go +++ b/config.go @@ -106,8 +106,6 @@ var ( AppConfig *beegoAppConfig // AppPath is the absolute path to the app AppPath string - // workPath is the absolute path of the current dir - workPath string // GlobalSessions is the instance for the session manager GlobalSessions *session.Manager @@ -118,11 +116,6 @@ var ( ) func init() { - AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) - - workPath, _ = os.Getwd() - workPath, _ = filepath.Abs(workPath) - BConfig = &Config{ AppName: "beego", RunMode: DEV, @@ -183,6 +176,16 @@ func init() { }, } + var err error + + if AppPath, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil { + panic(err) + } + workPath, err := os.Getwd() + if err != nil { + panic(err) + } + appConfigPath = filepath.Join(workPath, "conf", "app.conf") if !utils.FileExists(appConfigPath) { appConfigPath = filepath.Join(AppPath, "conf", "app.conf") @@ -192,7 +195,7 @@ func init() { } } - if err := parseConfig(appConfigPath); err != nil { + if err = parseConfig(appConfigPath); err != nil { panic(err) } }