1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:00:54 +00:00
This commit is contained in:
astaxie 2013-05-05 23:03:59 +08:00
parent 429f44562c
commit 93babc5780
3 changed files with 89 additions and 105 deletions

136
beego.go
View File

@ -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)

View File

@ -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
}

View File

@ -834,6 +834,10 @@ beego中带有很多可配置的参数我们来一一认识一下它们
beego的配置文件解析之后的对象也是在init的时候初始化的里面保存有解析`conf/app.conf`下面所有的参数数据
* AppConfigPath
配置文件所在的路径,默认是应用程序对应的目录下的`conf/app.conf`,用户可以修改该值配置自己的配置文件
* HttpAddr
应用监听地址默认为空监听所有的网卡IP