fix #48
Showing
3 changed files
with
89 additions
and
105 deletions
| ... | @@ -10,7 +10,6 @@ import ( | ... | @@ -10,7 +10,6 @@ import ( |
| 10 | "os" | 10 | "os" |
| 11 | "path" | 11 | "path" |
| 12 | "runtime" | 12 | "runtime" |
| 13 | "strconv" | ||
| 14 | ) | 13 | ) |
| 15 | 14 | ||
| 16 | const VERSION = "0.5.0" | 15 | const VERSION = "0.5.0" |
| ... | @@ -19,6 +18,7 @@ var ( | ... | @@ -19,6 +18,7 @@ var ( |
| 19 | BeeApp *App | 18 | BeeApp *App |
| 20 | AppName string | 19 | AppName string |
| 21 | AppPath string | 20 | AppPath string |
| 21 | AppConfigPath string | ||
| 22 | StaticDir map[string]string | 22 | StaticDir map[string]string |
| 23 | TemplateCache map[string]*template.Template | 23 | TemplateCache map[string]*template.Template |
| 24 | HttpAddr string | 24 | HttpAddr string |
| ... | @@ -30,16 +30,15 @@ var ( | ... | @@ -30,16 +30,15 @@ var ( |
| 30 | RunMode string //"dev" or "prod" | 30 | RunMode string //"dev" or "prod" |
| 31 | AppConfig *Config | 31 | AppConfig *Config |
| 32 | //related to session | 32 | //related to session |
| 33 | SessionOn bool // wheather auto start session,default is false | 33 | GlobalSessions *session.Manager //GlobalSessions |
| 34 | SessionProvider string // default session provider memory mysql redis | 34 | SessionOn bool // wheather auto start session,default is false |
| 35 | SessionName string // sessionName cookie's name | 35 | SessionProvider string // default session provider memory mysql redis |
| 36 | SessionGCMaxLifetime int64 // session's gc maxlifetime | 36 | SessionName string // sessionName cookie's name |
| 37 | SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo | 37 | SessionGCMaxLifetime int64 // session's gc maxlifetime |
| 38 | SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo | ||
| 38 | UseFcgi bool | 39 | UseFcgi bool |
| 39 | MaxMemory int64 | 40 | MaxMemory int64 |
| 40 | EnableGzip bool // enable gzip | 41 | EnableGzip bool // enable gzip |
| 41 | |||
| 42 | GlobalSessions *session.Manager //GlobalSessions | ||
| 43 | ) | 42 | ) |
| 44 | 43 | ||
| 45 | func init() { | 44 | func init() { |
| ... | @@ -48,103 +47,24 @@ func init() { | ... | @@ -48,103 +47,24 @@ func init() { |
| 48 | AppPath, _ = os.Getwd() | 47 | AppPath, _ = os.Getwd() |
| 49 | StaticDir = make(map[string]string) | 48 | StaticDir = make(map[string]string) |
| 50 | TemplateCache = make(map[string]*template.Template) | 49 | TemplateCache = make(map[string]*template.Template) |
| 51 | var err error | 50 | HttpAddr = "" |
| 52 | AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf")) | 51 | HttpPort = 8080 |
| 53 | if err != nil { | 52 | AppName = "beego" |
| 54 | //Trace("open Config err:", err) | 53 | RunMode = "dev" //default runmod |
| 55 | HttpAddr = "" | 54 | AutoRender = true |
| 56 | HttpPort = 8080 | 55 | RecoverPanic = true |
| 57 | AppName = "beego" | 56 | PprofOn = false |
| 58 | RunMode = "dev" //default runmod | 57 | ViewsPath = "views" |
| 59 | AutoRender = true | 58 | SessionOn = false |
| 60 | RecoverPanic = true | 59 | SessionProvider = "memory" |
| 61 | PprofOn = false | 60 | SessionName = "beegosessionID" |
| 62 | ViewsPath = "views" | 61 | SessionGCMaxLifetime = 3600 |
| 63 | SessionOn = false | 62 | SessionSavePath = "" |
| 64 | SessionProvider = "memory" | 63 | UseFcgi = false |
| 65 | SessionName = "beegosessionID" | 64 | MaxMemory = 1 << 26 //64MB |
| 66 | SessionGCMaxLifetime = 3600 | 65 | EnableGzip = false |
| 67 | SessionSavePath = "" | ||
| 68 | UseFcgi = false | ||
| 69 | MaxMemory = 1 << 26 //64MB | ||
| 70 | EnableGzip = false | ||
| 71 | } else { | ||
| 72 | HttpAddr = AppConfig.String("httpaddr") | ||
| 73 | if v, err := AppConfig.Int("httpport"); err != nil { | ||
| 74 | HttpPort = 8080 | ||
| 75 | } else { | ||
| 76 | HttpPort = v | ||
| 77 | } | ||
| 78 | if v, err := AppConfig.Int64("maxmemory"); err != nil { | ||
| 79 | MaxMemory = 1 << 26 | ||
| 80 | } else { | ||
| 81 | MaxMemory = v | ||
| 82 | } | ||
| 83 | AppName = AppConfig.String("appname") | ||
| 84 | if runmode := AppConfig.String("runmode"); runmode != "" { | ||
| 85 | RunMode = runmode | ||
| 86 | } else { | ||
| 87 | RunMode = "dev" | ||
| 88 | } | ||
| 89 | if ar, err := AppConfig.Bool("autorender"); err != nil { | ||
| 90 | AutoRender = true | ||
| 91 | } else { | ||
| 92 | AutoRender = ar | ||
| 93 | } | ||
| 94 | if ar, err := AppConfig.Bool("autorecover"); err != nil { | ||
| 95 | RecoverPanic = true | ||
| 96 | } else { | ||
| 97 | RecoverPanic = ar | ||
| 98 | } | ||
| 99 | if ar, err := AppConfig.Bool("pprofon"); err != nil { | ||
| 100 | PprofOn = false | ||
| 101 | } else { | ||
| 102 | PprofOn = ar | ||
| 103 | } | ||
| 104 | if views := AppConfig.String("viewspath"); views == "" { | ||
| 105 | ViewsPath = "views" | ||
| 106 | } else { | ||
| 107 | ViewsPath = views | ||
| 108 | } | ||
| 109 | if ar, err := AppConfig.Bool("sessionon"); err != nil { | ||
| 110 | SessionOn = false | ||
| 111 | } else { | ||
| 112 | SessionOn = ar | ||
| 113 | } | ||
| 114 | if ar := AppConfig.String("sessionprovider"); ar == "" { | ||
| 115 | SessionProvider = "memory" | ||
| 116 | } else { | ||
| 117 | SessionProvider = ar | ||
| 118 | } | ||
| 119 | if ar := AppConfig.String("sessionname"); ar == "" { | ||
| 120 | SessionName = "beegosessionID" | ||
| 121 | } else { | ||
| 122 | SessionName = ar | ||
| 123 | } | ||
| 124 | if ar := AppConfig.String("sessionsavepath"); ar == "" { | ||
| 125 | SessionSavePath = "" | ||
| 126 | } else { | ||
| 127 | SessionSavePath = ar | ||
| 128 | } | ||
| 129 | if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && ar != 0 { | ||
| 130 | int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64) | ||
| 131 | SessionGCMaxLifetime = int64val | ||
| 132 | } else { | ||
| 133 | SessionGCMaxLifetime = 3600 | ||
| 134 | } | ||
| 135 | if ar, err := AppConfig.Bool("usefcgi"); err != nil { | ||
| 136 | UseFcgi = false | ||
| 137 | } else { | ||
| 138 | UseFcgi = ar | ||
| 139 | } | ||
| 140 | if ar, err := AppConfig.Bool("enablegzip"); err != nil { | ||
| 141 | EnableGzip = false | ||
| 142 | } else { | ||
| 143 | EnableGzip = ar | ||
| 144 | } | ||
| 145 | } | ||
| 146 | StaticDir["/static"] = "static" | 66 | StaticDir["/static"] = "static" |
| 147 | 67 | AppConfigPath = path.Join(AppPath, "conf", "app.conf") | |
| 148 | } | 68 | } |
| 149 | 69 | ||
| 150 | type App struct { | 70 | type App struct { |
| ... | @@ -254,6 +174,12 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App { | ... | @@ -254,6 +174,12 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App { |
| 254 | } | 174 | } |
| 255 | 175 | ||
| 256 | func Run() { | 176 | func Run() { |
| 177 | err := ParseConfig() | ||
| 178 | if err != nil { | ||
| 179 | if RunMode == "dev" { | ||
| 180 | Warn(err) | ||
| 181 | } | ||
| 182 | } | ||
| 257 | if PprofOn { | 183 | if PprofOn { |
| 258 | BeeApp.Router(`/debug/pprof`, &ProfController{}) | 184 | BeeApp.Router(`/debug/pprof`, &ProfController{}) |
| 259 | BeeApp.Router(`/debug/pprof/:pp([\w]+)`, &ProfController{}) | 185 | BeeApp.Router(`/debug/pprof/:pp([\w]+)`, &ProfController{}) |
| ... | @@ -262,7 +188,7 @@ func Run() { | ... | @@ -262,7 +188,7 @@ func Run() { |
| 262 | GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime, SessionSavePath) | 188 | GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime, SessionSavePath) |
| 263 | go GlobalSessions.GC() | 189 | go GlobalSessions.GC() |
| 264 | } | 190 | } |
| 265 | err := BuildTemplate(ViewsPath) | 191 | err = BuildTemplate(ViewsPath) |
| 266 | if err != nil { | 192 | if err != nil { |
| 267 | if RunMode == "dev" { | 193 | if RunMode == "dev" { |
| 268 | Warn(err) | 194 | Warn(err) | ... | ... |
| ... | @@ -123,3 +123,57 @@ func (c *Config) SetValue(key, value string) error { | ... | @@ -123,3 +123,57 @@ func (c *Config) SetValue(key, value string) error { |
| 123 | c.data[key] = value | 123 | c.data[key] = value |
| 124 | return nil | 124 | return nil |
| 125 | } | 125 | } |
| 126 | |||
| 127 | func ParseConfig() (err error) { | ||
| 128 | AppConfig, err = LoadConfig(AppConfigPath) | ||
| 129 | if err != nil { | ||
| 130 | return err | ||
| 131 | } else { | ||
| 132 | HttpAddr = AppConfig.String("httpaddr") | ||
| 133 | if v, err := AppConfig.Int("httpport"); err == nil { | ||
| 134 | HttpPort = v | ||
| 135 | } | ||
| 136 | if v, err := AppConfig.Int64("maxmemory"); err == nil { | ||
| 137 | MaxMemory = v | ||
| 138 | } | ||
| 139 | AppName = AppConfig.String("appname") | ||
| 140 | if runmode := AppConfig.String("runmode"); runmode != "" { | ||
| 141 | RunMode = runmode | ||
| 142 | } | ||
| 143 | if ar, err := AppConfig.Bool("autorender"); err == nil { | ||
| 144 | AutoRender = ar | ||
| 145 | } | ||
| 146 | if ar, err := AppConfig.Bool("autorecover"); err == nil { | ||
| 147 | RecoverPanic = ar | ||
| 148 | } | ||
| 149 | if ar, err := AppConfig.Bool("pprofon"); err == nil { | ||
| 150 | PprofOn = ar | ||
| 151 | } | ||
| 152 | if views := AppConfig.String("viewspath"); views != "" { | ||
| 153 | ViewsPath = views | ||
| 154 | } | ||
| 155 | if ar, err := AppConfig.Bool("sessionon"); err == nil { | ||
| 156 | SessionOn = ar | ||
| 157 | } | ||
| 158 | if ar := AppConfig.String("sessionprovider"); ar != "" { | ||
| 159 | SessionProvider = ar | ||
| 160 | } | ||
| 161 | if ar := AppConfig.String("sessionname"); ar != "" { | ||
| 162 | SessionName = ar | ||
| 163 | } | ||
| 164 | if ar := AppConfig.String("sessionsavepath"); ar != "" { | ||
| 165 | SessionSavePath = ar | ||
| 166 | } | ||
| 167 | if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && ar != 0 { | ||
| 168 | int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64) | ||
| 169 | SessionGCMaxLifetime = int64val | ||
| 170 | } | ||
| 171 | if ar, err := AppConfig.Bool("usefcgi"); err == nil { | ||
| 172 | UseFcgi = ar | ||
| 173 | } | ||
| 174 | if ar, err := AppConfig.Bool("enablegzip"); err == nil { | ||
| 175 | EnableGzip = ar | ||
| 176 | } | ||
| 177 | } | ||
| 178 | return nil | ||
| 179 | } | ... | ... |
| ... | @@ -834,6 +834,10 @@ beego中带有很多可配置的参数,我们来一一认识一下它们,这 | ... | @@ -834,6 +834,10 @@ beego中带有很多可配置的参数,我们来一一认识一下它们,这 |
| 834 | 834 | ||
| 835 | beego的配置文件解析之后的对象,也是在init的时候初始化的,里面保存有解析`conf/app.conf`下面所有的参数数据 | 835 | beego的配置文件解析之后的对象,也是在init的时候初始化的,里面保存有解析`conf/app.conf`下面所有的参数数据 |
| 836 | 836 | ||
| 837 | * AppConfigPath | ||
| 838 | |||
| 839 | 配置文件所在的路径,默认是应用程序对应的目录下的`conf/app.conf`,用户可以修改该值配置自己的配置文件 | ||
| 840 | |||
| 837 | * HttpAddr | 841 | * HttpAddr |
| 838 | 842 | ||
| 839 | 应用监听地址,默认为空,监听所有的网卡IP | 843 | 应用监听地址,默认为空,监听所有的网卡IP | ... | ... |
-
Please register or sign in to post a comment