From 93babc578023aeddf321573a96b3090ac69494d0 Mon Sep 17 00:00:00 2001 From: astaxie Date: Sun, 5 May 2013 23:03:59 +0800 Subject: [PATCH] fix #48 --- beego.go | 136 ++++++++++-------------------------------- config.go | 54 +++++++++++++++++ docs/zh/Quickstart.md | 4 ++ 3 files changed, 89 insertions(+), 105 deletions(-) diff --git a/beego.go b/beego.go index 04a583b2..e21f50cf 100644 --- a/beego.go +++ b/beego.go @@ -10,7 +10,6 @@ import ( "os" "path" "runtime" - "strconv" ) const VERSION = "0.5.0" @@ -19,6 +18,7 @@ var ( BeeApp *App AppName string AppPath string + AppConfigPath string StaticDir map[string]string TemplateCache map[string]*template.Template HttpAddr string @@ -30,16 +30,15 @@ var ( RunMode string //"dev" or "prod" AppConfig *Config //related to session - SessionOn bool // wheather auto start session,default is false - SessionProvider string // default session provider memory mysql redis - SessionName string // sessionName cookie's name - SessionGCMaxLifetime int64 // session's gc maxlifetime - SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo + GlobalSessions *session.Manager //GlobalSessions + SessionOn bool // wheather auto start session,default is false + SessionProvider string // default session provider memory mysql redis + SessionName string // sessionName cookie's name + SessionGCMaxLifetime int64 // session's gc maxlifetime + SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo UseFcgi bool MaxMemory int64 EnableGzip bool // enable gzip - - GlobalSessions *session.Manager //GlobalSessions ) func init() { @@ -48,103 +47,24 @@ func init() { AppPath, _ = os.Getwd() StaticDir = make(map[string]string) TemplateCache = make(map[string]*template.Template) - var err error - AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf")) - if err != nil { - //Trace("open Config err:", err) - HttpAddr = "" - HttpPort = 8080 - AppName = "beego" - RunMode = "dev" //default runmod - AutoRender = true - RecoverPanic = true - PprofOn = false - ViewsPath = "views" - SessionOn = false - SessionProvider = "memory" - SessionName = "beegosessionID" - SessionGCMaxLifetime = 3600 - SessionSavePath = "" - UseFcgi = false - MaxMemory = 1 << 26 //64MB - EnableGzip = false - } else { - HttpAddr = AppConfig.String("httpaddr") - if v, err := AppConfig.Int("httpport"); err != nil { - HttpPort = 8080 - } else { - HttpPort = v - } - if v, err := AppConfig.Int64("maxmemory"); err != nil { - MaxMemory = 1 << 26 - } else { - MaxMemory = v - } - AppName = AppConfig.String("appname") - if runmode := AppConfig.String("runmode"); runmode != "" { - RunMode = runmode - } else { - RunMode = "dev" - } - if ar, err := AppConfig.Bool("autorender"); err != nil { - AutoRender = true - } else { - AutoRender = ar - } - if ar, err := AppConfig.Bool("autorecover"); err != nil { - RecoverPanic = true - } else { - RecoverPanic = ar - } - if ar, err := AppConfig.Bool("pprofon"); err != nil { - PprofOn = false - } else { - PprofOn = ar - } - if views := AppConfig.String("viewspath"); views == "" { - ViewsPath = "views" - } else { - ViewsPath = views - } - if ar, err := AppConfig.Bool("sessionon"); err != nil { - SessionOn = false - } else { - SessionOn = ar - } - if ar := AppConfig.String("sessionprovider"); ar == "" { - SessionProvider = "memory" - } else { - SessionProvider = ar - } - if ar := AppConfig.String("sessionname"); ar == "" { - SessionName = "beegosessionID" - } else { - SessionName = ar - } - if ar := AppConfig.String("sessionsavepath"); ar == "" { - SessionSavePath = "" - } else { - SessionSavePath = ar - } - if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && ar != 0 { - int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64) - SessionGCMaxLifetime = int64val - } else { - SessionGCMaxLifetime = 3600 - } - if ar, err := AppConfig.Bool("usefcgi"); err != nil { - UseFcgi = false - } else { - UseFcgi = ar - } - if ar, err := AppConfig.Bool("enablegzip"); err != nil { - EnableGzip = false - } else { - EnableGzip = ar - } - } + HttpAddr = "" + HttpPort = 8080 + AppName = "beego" + RunMode = "dev" //default runmod + AutoRender = true + RecoverPanic = true + PprofOn = false + ViewsPath = "views" + SessionOn = false + SessionProvider = "memory" + SessionName = "beegosessionID" + SessionGCMaxLifetime = 3600 + SessionSavePath = "" + UseFcgi = false + MaxMemory = 1 << 26 //64MB + EnableGzip = false StaticDir["/static"] = "static" - + AppConfigPath = path.Join(AppPath, "conf", "app.conf") } type App struct { @@ -254,6 +174,12 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App { } func Run() { + err := ParseConfig() + if err != nil { + if RunMode == "dev" { + Warn(err) + } + } if PprofOn { BeeApp.Router(`/debug/pprof`, &ProfController{}) BeeApp.Router(`/debug/pprof/:pp([\w]+)`, &ProfController{}) @@ -262,7 +188,7 @@ func Run() { GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime, SessionSavePath) go GlobalSessions.GC() } - err := BuildTemplate(ViewsPath) + err = BuildTemplate(ViewsPath) if err != nil { if RunMode == "dev" { Warn(err) diff --git a/config.go b/config.go index ce7c3a62..f4eca072 100644 --- a/config.go +++ b/config.go @@ -123,3 +123,57 @@ func (c *Config) SetValue(key, value string) error { c.data[key] = value return nil } + +func ParseConfig() (err error) { + AppConfig, err = LoadConfig(AppConfigPath) + if err != nil { + return err + } else { + HttpAddr = AppConfig.String("httpaddr") + if v, err := AppConfig.Int("httpport"); err == nil { + HttpPort = v + } + if v, err := AppConfig.Int64("maxmemory"); err == nil { + MaxMemory = v + } + AppName = AppConfig.String("appname") + if runmode := AppConfig.String("runmode"); runmode != "" { + RunMode = runmode + } + if ar, err := AppConfig.Bool("autorender"); err == nil { + AutoRender = ar + } + if ar, err := AppConfig.Bool("autorecover"); err == nil { + RecoverPanic = ar + } + if ar, err := AppConfig.Bool("pprofon"); err == nil { + PprofOn = ar + } + if views := AppConfig.String("viewspath"); views != "" { + ViewsPath = views + } + if ar, err := AppConfig.Bool("sessionon"); err == nil { + SessionOn = ar + } + if ar := AppConfig.String("sessionprovider"); ar != "" { + SessionProvider = ar + } + if ar := AppConfig.String("sessionname"); ar != "" { + SessionName = ar + } + if ar := AppConfig.String("sessionsavepath"); ar != "" { + SessionSavePath = ar + } + if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && ar != 0 { + int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64) + SessionGCMaxLifetime = int64val + } + if ar, err := AppConfig.Bool("usefcgi"); err == nil { + UseFcgi = ar + } + if ar, err := AppConfig.Bool("enablegzip"); err == nil { + EnableGzip = ar + } + } + return nil +} diff --git a/docs/zh/Quickstart.md b/docs/zh/Quickstart.md index af0170ca..380828e3 100644 --- a/docs/zh/Quickstart.md +++ b/docs/zh/Quickstart.md @@ -834,6 +834,10 @@ beego中带有很多可配置的参数,我们来一一认识一下它们,这 beego的配置文件解析之后的对象,也是在init的时候初始化的,里面保存有解析`conf/app.conf`下面所有的参数数据 +* AppConfigPath + + 配置文件所在的路径,默认是应用程序对应的目录下的`conf/app.conf`,用户可以修改该值配置自己的配置文件 + * HttpAddr 应用监听地址,默认为空,监听所有的网卡IP