func init() { BConfig = newBConfig() 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") if !utils.FileExists(appConfigPath) { AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()} return } } if err = parseConfig(appConfigPath); err != nil { panic(err) } if err = os.Chdir(AppPath); err != nil { panic(err) } }
func init() { // create beego application BeeApp = NewApp() workPath, _ = os.Getwd() workPath, _ = filepath.Abs(workPath) // initialize default configurations AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") if workPath != AppPath { if utils.FileExists(AppConfigPath) { os.Chdir(AppPath) } else { AppConfigPath = filepath.Join(workPath, "conf", "app.conf") } } AppConfigProvider = "ini" StaticDir = make(map[string]string) StaticDir["/static"] = "static" StaticExtensionsToGzip = []string{".css", ".js"} TemplateCache = make(map[string]*template.Template) // set this to 0.0.0.0 to make this app available to externally EnableHttpListen = true //default enable http Listen HttpAddr = "" HttpPort = 8080 HttpsPort = 10443 AppName = "beego" RunMode = "dev" //default runmod AutoRender = true RecoverPanic = true ViewsPath = "views" SessionOn = false SessionProvider = "memory" SessionName = "beegosessionID" SessionGCMaxLifetime = 3600 SessionSavePath = "" SessionCookieLifeTime = 0 //set cookie default is the brower life SessionAutoSetCookie = true UseFcgi = false UseStdIo = false MaxMemory = 1 << 26 //64MB EnableGzip = false HttpServerTimeOut = 0 ErrorsShow = true XSRFKEY = "beegoxsrf" XSRFExpire = 0 TemplateLeft = "{{" TemplateRight = "}}" BeegoServerName = "beegoServer:" + VERSION EnableAdmin = false AdminHttpAddr = "127.0.0.1" AdminHttpPort = 8088 FlashName = "BEEGO_FLASH" FlashSeperator = "BEEGOFLASH" RouterCaseSensitive = true runtime.GOMAXPROCS(runtime.NumCPU()) // init BeeLogger BeeLogger = logs.NewLogger(10000) err := BeeLogger.SetLogger("console", "") if err != nil { fmt.Println("init console log error:", err) } SetLogFuncCall(true) err = ParseConfig() if err != nil && os.IsNotExist(err) { // for init if doesn't have app.conf will not panic ac := config.NewFakeConfig() AppConfig = &beegoAppConfig{ac} Warning(err) } }
// ParseConfig parsed default config file. // now only support ini, next will support json. func ParseConfig() (err error) { AppConfig, err = config.NewConfig(AppConfigProvider, AppConfigPath) if err != nil { AppConfig = config.NewFakeConfig() return err } else { if v, err := getConfig("string", "HttpAddr"); err == nil { HttpAddr = v.(string) } if v, err := getConfig("int", "HttpPort"); err == nil { HttpPort = v.(int) } if v, err := getConfig("bool", "EnableHttpListen"); err == nil { EnableHttpListen = v.(bool) } if maxmemory, err := getConfig("int64", "MaxMemory"); err == nil { MaxMemory = maxmemory.(int64) } if appname, _ := getConfig("string", "AppName"); appname != "" { AppName = appname.(string) } if runmode, _ := getConfig("string", "RunMode"); runmode != "" { RunMode = runmode.(string) } if autorender, err := getConfig("bool", "AutoRender"); err == nil { AutoRender = autorender.(bool) } if autorecover, err := getConfig("bool", "RecoverPanic"); err == nil { RecoverPanic = autorecover.(bool) } if views, _ := getConfig("string", "ViewsPath"); views != "" { ViewsPath = views.(string) } if sessionon, err := getConfig("bool", "SessionOn"); err == nil { SessionOn = sessionon.(bool) } if sessProvider, _ := getConfig("string", "SessionProvider"); sessProvider != "" { SessionProvider = sessProvider.(string) } if sessName, _ := getConfig("string", "SessionName"); sessName != "" { SessionName = sessName.(string) } if sesssavepath, _ := getConfig("string", "SessionSavePath"); sesssavepath != "" { SessionSavePath = sesssavepath.(string) } if sesshashfunc, _ := getConfig("string", "SessionHashFunc"); sesshashfunc != "" { SessionHashFunc = sesshashfunc.(string) } if sesshashkey, _ := getConfig("string", "SessionHashKey"); sesshashkey != "" { SessionHashKey = sesshashkey.(string) } if sessMaxLifeTime, err := getConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { SessionGCMaxLifetime = sessMaxLifeTime.(int64) } if sesscookielifetime, err := getConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { SessionCookieLifeTime = sesscookielifetime.(int) } if usefcgi, err := getConfig("bool", "UseFcgi"); err == nil { UseFcgi = usefcgi.(bool) } if enablegzip, err := getConfig("bool", "EnableGzip"); err == nil { EnableGzip = enablegzip.(bool) } if directoryindex, err := getConfig("bool", "DirectoryIndex"); err == nil { DirectoryIndex = directoryindex.(bool) } if timeout, err := getConfig("int64", "HttpServerTimeOut"); err == nil { HttpServerTimeOut = timeout.(int64) } if errorsshow, err := getConfig("bool", "ErrorsShow"); err == nil { ErrorsShow = errorsshow.(bool) } if copyrequestbody, err := getConfig("bool", "CopyRequestBody"); err == nil { CopyRequestBody = copyrequestbody.(bool) } if xsrfkey, _ := getConfig("string", "XSRFKEY"); xsrfkey != "" { XSRFKEY = xsrfkey.(string) } if enablexsrf, err := getConfig("bool", "EnableXSRF"); err == nil { EnableXSRF = enablexsrf.(bool) } if expire, err := getConfig("int", "XSRFExpire"); err == nil { XSRFExpire = expire.(int) } if tplleft, _ := getConfig("string", "TemplateLeft"); tplleft != "" { TemplateLeft = tplleft.(string) } if tplright, _ := getConfig("string", "TemplateRight"); tplright != "" { TemplateRight = tplright.(string) } if httptls, err := getConfig("bool", "EnableHttpTLS"); err == nil { EnableHttpTLS = httptls.(bool) } if httpsport, err := getConfig("int", "HttpsPort"); err == nil { HttpsPort = httpsport.(int) } if certfile, _ := getConfig("string", "HttpCertFile"); certfile != "" { HttpCertFile = certfile.(string) } if keyfile, _ := getConfig("string", "HttpKeyFile"); keyfile != "" { HttpKeyFile = keyfile.(string) } if serverName, _ := getConfig("string", "BeegoServerName"); serverName != "" { BeegoServerName = serverName.(string) } if flashname, _ := getConfig("string", "FlashName"); flashname != "" { FlashName = flashname.(string) } if flashseperator, _ := getConfig("string", "FlashSeperator"); flashseperator != "" { FlashSeperator = flashseperator.(string) } if sd, _ := getConfig("string", "StaticDir"); sd != "" { for k := range StaticDir { delete(StaticDir, k) } sds := strings.Fields(sd.(string)) for _, v := range sds { if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 { StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1] } else { StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0] } } } if sgz, _ := getConfig("string", "StaticExtensionsToGzip"); sgz != "" { extensions := strings.Split(sgz.(string), ",") if len(extensions) > 0 { StaticExtensionsToGzip = []string{} for _, ext := range extensions { if len(ext) == 0 { continue } extWithDot := ext if extWithDot[:1] != "." { extWithDot = "." + extWithDot } StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot) } } } if enableadmin, err := getConfig("bool", "EnableAdmin"); err == nil { EnableAdmin = enableadmin.(bool) } if adminhttpaddr, _ := getConfig("string", "AdminHttpAddr"); adminhttpaddr != "" { AdminHttpAddr = adminhttpaddr.(string) } if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil { AdminHttpPort = adminhttpport.(int) } if enabledocs, err := getConfig("bool", "EnableDocs"); err == nil { EnableDocs = enabledocs.(bool) } } return nil }
// ParseConfig parsed default config file. // now only support ini, next will support json. func ParseConfig() (err error) { AppConfig, err = config.NewConfig("ini", AppConfigPath) if err != nil { AppConfig = config.NewFakeConfig() return err } else { HttpAddr = AppConfig.String("HttpAddr") if v, err := AppConfig.Int("HttpPort"); err == nil { HttpPort = v } if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { MaxMemory = maxmemory } if appname := AppConfig.String("AppName"); appname != "" { AppName = appname } if runmode := AppConfig.String("RunMode"); runmode != "" { RunMode = runmode } if autorender, err := AppConfig.Bool("AutoRender"); err == nil { AutoRender = autorender } if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil { RecoverPanic = autorecover } if views := AppConfig.String("ViewsPath"); views != "" { ViewsPath = views } if sessionon, err := AppConfig.Bool("SessionOn"); err == nil { SessionOn = sessionon } if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" { SessionProvider = sessProvider } if sessName := AppConfig.String("SessionName"); sessName != "" { SessionName = sessName } if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" { SessionSavePath = sesssavepath } if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" { SessionHashFunc = sesshashfunc } if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" { SessionHashKey = sesshashkey } if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64) SessionGCMaxLifetime = int64val } if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { SessionCookieLifeTime = sesscookielifetime } if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil { UseFcgi = usefcgi } if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { EnableGzip = enablegzip } if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil { DirectoryIndex = directoryindex } if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil { EnableHotUpdate = hotupdate } if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil { HttpServerTimeOut = timeout } if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil { ErrorsShow = errorsshow } if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil { CopyRequestBody = copyrequestbody } if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" { XSRFKEY = xsrfkey } if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil { EnableXSRF = enablexsrf } if expire, err := AppConfig.Int("XSRFExpire"); err == nil { XSRFExpire = expire } if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" { TemplateLeft = tplleft } if tplright := AppConfig.String("TemplateRight"); tplright != "" { TemplateRight = tplright } if httptls, err := AppConfig.Bool("HttpTLS"); err == nil { HttpTLS = httptls } if certfile := AppConfig.String("HttpCertFile"); certfile != "" { HttpCertFile = certfile } if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" { HttpKeyFile = keyfile } if serverName := AppConfig.String("BeegoServerName"); serverName != "" { BeegoServerName = serverName } if flashname := AppConfig.String("FlashName"); flashname != "" { FlashName = flashname } if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" { FlashSeperator = flashseperator } if sd := AppConfig.String("StaticDir"); sd != "" { for k := range StaticDir { delete(StaticDir, k) } sds := strings.Fields(sd) for _, v := range sds { if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 { StaticDir["/"+url2fsmap[0]] = url2fsmap[1] } else { StaticDir["/"+url2fsmap[0]] = url2fsmap[0] } } } if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" { extensions := strings.Split(sgz, ",") if len(extensions) > 0 { StaticExtensionsToGzip = []string{} for _, ext := range extensions { if len(ext) == 0 { continue } extWithDot := ext if extWithDot[:1] != "." { extWithDot = "." + extWithDot } StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot) } } } if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil { EnableAdmin = enableadmin } if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" { AdminHttpAddr = adminhttpaddr } if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil { AdminHttpPort = adminhttpport } } return nil }
func init() { AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) os.Chdir(AppPath) BConfig = &Config{ AppName: "beego", RunMode: DEV, RouterCaseSensitive: true, ServerName: "beegoServer:" + VERSION, RecoverPanic: true, CopyRequestBody: false, EnableGzip: false, MaxMemory: 1 << 26, //64MB EnableErrorsShow: true, Listen: Listen{ Graceful: false, ServerTimeOut: 0, ListenTCP4: false, EnableHTTP: true, HTTPAddr: "", HTTPPort: 8080, EnableHTTPS: false, HTTPSAddr: "", HTTPSPort: 10443, HTTPSCertFile: "", HTTPSKeyFile: "", EnableAdmin: false, AdminAddr: "", AdminPort: 8088, EnableFcgi: false, EnableStdIo: false, }, WebConfig: WebConfig{ AutoRender: true, EnableDocs: false, FlashName: "BEEGO_FLASH", FlashSeparator: "BEEGOFLASH", DirectoryIndex: false, StaticDir: map[string]string{"/static": "static"}, StaticExtensionsToGzip: []string{".css", ".js"}, TemplateLeft: "{{", TemplateRight: "}}", ViewsPath: "views", EnableXSRF: false, XSRFKey: "beegoxsrf", XSRFExpire: 0, Session: SessionConfig{ SessionOn: false, SessionProvider: "memory", SessionName: "beegosessionID", SessionGCMaxLifetime: 3600, SessionProviderConfig: "", SessionCookieLifeTime: 0, //set cookie default is the browser life SessionAutoSetCookie: true, SessionDomain: "", }, }, Log: LogConfig{ AccessLogs: false, FileLineNum: true, Outputs: map[string]string{"console": ""}, }, } appConfigPath = filepath.Join(AppPath, "conf", "app.conf") if !utils.FileExists(appConfigPath) { AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()} return } if err := parseConfig(appConfigPath); err != nil { panic(err) } }
// ParseConfig parsed default config file. // now only support ini, next will support json. func ParseConfig() (err error) { if AppConfigPath == "" { if utils.FileExists(filepath.Join("conf", "app.conf")) { AppConfigPath = filepath.Join("conf", "app.conf") } else { AppConfig = &beegoAppConfig{config.NewFakeConfig()} return } } AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) if err != nil { return err } // set the runmode first if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" { BConfig.RunMode = envRunMode } else if runmode := AppConfig.String("RunMode"); runmode != "" { BConfig.RunMode = runmode } BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName) BConfig.RecoverPanic = AppConfig.DefaultBool("RecoverPanic", BConfig.RecoverPanic) BConfig.RouterCaseSensitive = AppConfig.DefaultBool("RouterCaseSensitive", BConfig.RouterCaseSensitive) BConfig.ServerName = AppConfig.DefaultString("ServerName", BConfig.ServerName) BConfig.EnableGzip = AppConfig.DefaultBool("EnableGzip", BConfig.EnableGzip) BConfig.EnableErrorsShow = AppConfig.DefaultBool("EnableErrorsShow", BConfig.EnableErrorsShow) BConfig.CopyRequestBody = AppConfig.DefaultBool("CopyRequestBody", BConfig.CopyRequestBody) BConfig.MaxMemory = AppConfig.DefaultInt64("MaxMemory", BConfig.MaxMemory) BConfig.Listen.Graceful = AppConfig.DefaultBool("Graceful", BConfig.Listen.Graceful) BConfig.Listen.HTTPAddr = AppConfig.String("HTTPAddr") BConfig.Listen.HTTPPort = AppConfig.DefaultInt("HTTPPort", BConfig.Listen.HTTPPort) BConfig.Listen.ListenTCP4 = AppConfig.DefaultBool("ListenTCP4", BConfig.Listen.ListenTCP4) BConfig.Listen.EnableHTTP = AppConfig.DefaultBool("EnableHTTP", BConfig.Listen.EnableHTTP) BConfig.Listen.EnableHTTPS = AppConfig.DefaultBool("EnableHTTPS", BConfig.Listen.EnableHTTPS) BConfig.Listen.HTTPSAddr = AppConfig.DefaultString("HTTPSAddr", BConfig.Listen.HTTPSAddr) BConfig.Listen.HTTPSPort = AppConfig.DefaultInt("HTTPSPort", BConfig.Listen.HTTPSPort) BConfig.Listen.HTTPSCertFile = AppConfig.DefaultString("HTTPSCertFile", BConfig.Listen.HTTPSCertFile) BConfig.Listen.HTTPSKeyFile = AppConfig.DefaultString("HTTPSKeyFile", BConfig.Listen.HTTPSKeyFile) BConfig.Listen.EnableAdmin = AppConfig.DefaultBool("EnableAdmin", BConfig.Listen.EnableAdmin) BConfig.Listen.AdminAddr = AppConfig.DefaultString("AdminAddr", BConfig.Listen.AdminAddr) BConfig.Listen.AdminPort = AppConfig.DefaultInt("AdminPort", BConfig.Listen.AdminPort) BConfig.Listen.EnableFcgi = AppConfig.DefaultBool("EnableFcgi", BConfig.Listen.EnableFcgi) BConfig.Listen.EnableStdIo = AppConfig.DefaultBool("EnableStdIo", BConfig.Listen.EnableStdIo) BConfig.Listen.ServerTimeOut = AppConfig.DefaultInt64("ServerTimeOut", BConfig.Listen.ServerTimeOut) BConfig.WebConfig.AutoRender = AppConfig.DefaultBool("AutoRender", BConfig.WebConfig.AutoRender) BConfig.WebConfig.ViewsPath = AppConfig.DefaultString("ViewsPath", BConfig.WebConfig.ViewsPath) BConfig.WebConfig.DirectoryIndex = AppConfig.DefaultBool("DirectoryIndex", BConfig.WebConfig.DirectoryIndex) BConfig.WebConfig.FlashName = AppConfig.DefaultString("FlashName", BConfig.WebConfig.FlashName) BConfig.WebConfig.FlashSeparator = AppConfig.DefaultString("FlashSeparator", BConfig.WebConfig.FlashSeparator) BConfig.WebConfig.EnableDocs = AppConfig.DefaultBool("EnableDocs", BConfig.WebConfig.EnableDocs) BConfig.WebConfig.XSRFKey = AppConfig.DefaultString("XSRFKEY", BConfig.WebConfig.XSRFKey) BConfig.WebConfig.EnableXSRF = AppConfig.DefaultBool("EnableXSRF", BConfig.WebConfig.EnableXSRF) BConfig.WebConfig.XSRFExpire = AppConfig.DefaultInt("XSRFExpire", BConfig.WebConfig.XSRFExpire) BConfig.WebConfig.TemplateLeft = AppConfig.DefaultString("TemplateLeft", BConfig.WebConfig.TemplateLeft) BConfig.WebConfig.TemplateRight = AppConfig.DefaultString("TemplateRight", BConfig.WebConfig.TemplateRight) BConfig.WebConfig.Session.SessionOn = AppConfig.DefaultBool("SessionOn", BConfig.WebConfig.Session.SessionOn) BConfig.WebConfig.Session.SessionProvider = AppConfig.DefaultString("SessionProvider", BConfig.WebConfig.Session.SessionProvider) BConfig.WebConfig.Session.SessionName = AppConfig.DefaultString("SessionName", BConfig.WebConfig.Session.SessionName) BConfig.WebConfig.Session.SessionProviderConfig = AppConfig.DefaultString("SessionProviderConfig", BConfig.WebConfig.Session.SessionProviderConfig) BConfig.WebConfig.Session.SessionGCMaxLifetime = AppConfig.DefaultInt64("SessionGCMaxLifetime", BConfig.WebConfig.Session.SessionGCMaxLifetime) 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) if sd := AppConfig.String("StaticDir"); sd != "" { for k := range BConfig.WebConfig.StaticDir { delete(BConfig.WebConfig.StaticDir, k) } sds := strings.Fields(sd) for _, v := range sds { if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 { BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1] } else { BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0] } } } if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" { extensions := strings.Split(sgz, ",") fileExts := []string{} for _, ext := range extensions { ext = strings.TrimSpace(ext) if ext == "" { continue } if !strings.HasPrefix(ext, ".") { ext = "." + ext } fileExts = append(fileExts, ext) } if len(fileExts) > 0 { BConfig.WebConfig.StaticExtensionsToGzip = fileExts } } return nil }