deb553be by astaxie

beego:confgi support difference run mode section

runmode = dev
appname = doraemon
[dev]
httpport = 8880
sessionon = true

[prod]
httpport = 8888
sessionon = true

[test]
httpport = 8080
sessionon = false
1 parent 3db9633e
Showing 1 changed file with 124 additions and 84 deletions
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
7 package beego 7 package beego
8 8
9 import ( 9 import (
10 "errors"
10 "fmt" 11 "fmt"
11 "html/template" 12 "html/template"
12 "os" 13 "os"
13 "path/filepath" 14 "path/filepath"
14 "runtime" 15 "runtime"
15 "strconv"
16 "strings" 16 "strings"
17 17
18 "github.com/astaxie/beego/config" 18 "github.com/astaxie/beego/config"
...@@ -178,154 +178,152 @@ func ParseConfig() (err error) { ...@@ -178,154 +178,152 @@ func ParseConfig() (err error) {
178 AppConfig = config.NewFakeConfig() 178 AppConfig = config.NewFakeConfig()
179 return err 179 return err
180 } else { 180 } else {
181 if v := AppConfig.String(RunMode + "::HttpAddr"); v != "" { 181
182 HttpAddr = v 182 if v, err := getConfig("string", "HttpAddr"); err == nil {
183 } else { 183 HttpAddr = v.(string)
184 HttpAddr = AppConfig.String("HttpAddr")
185 } 184 }
186 185
187 if v, err := AppConfig.Int("HttpPort"); err == nil { 186 if v, err := getConfig("int", "HttpPort"); err == nil {
188 HttpPort = v 187 HttpPort = v.(int)
189 } 188 }
190 189
191 if v, err := AppConfig.Bool("EnableHttpListen"); err == nil { 190 if v, err := getConfig("bool", "EnableHttpListen"); err == nil {
192 EnableHttpListen = v 191 EnableHttpListen = v.(bool)
193 } 192 }
194 193
195 if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { 194 if maxmemory, err := getConfig("int64", "MaxMemory"); err == nil {
196 MaxMemory = maxmemory 195 MaxMemory = maxmemory.(int64)
197 } 196 }
198 197
199 if appname := AppConfig.String("AppName"); appname != "" { 198 if appname, _ := getConfig("string", "AppName"); appname != "" {
200 AppName = appname 199 AppName = appname.(string)
201 } 200 }
202 201
203 if runmode := AppConfig.String("RunMode"); runmode != "" { 202 if runmode, _ := getConfig("string", "RunMode"); runmode != "" {
204 RunMode = runmode 203 RunMode = runmode.(string)
205 } 204 }
206 205
207 if autorender, err := AppConfig.Bool("AutoRender"); err == nil { 206 if autorender, err := getConfig("bool", "AutoRender"); err == nil {
208 AutoRender = autorender 207 AutoRender = autorender.(bool)
209 } 208 }
210 209
211 if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil { 210 if autorecover, err := getConfig("bool", "RecoverPanic"); err == nil {
212 RecoverPanic = autorecover 211 RecoverPanic = autorecover.(bool)
213 } 212 }
214 213
215 if views := AppConfig.String("ViewsPath"); views != "" { 214 if views, _ := getConfig("string", "ViewsPath"); views != "" {
216 ViewsPath = views 215 ViewsPath = views.(string)
217 } 216 }
218 217
219 if sessionon, err := AppConfig.Bool("SessionOn"); err == nil { 218 if sessionon, err := getConfig("bool", "SessionOn"); err == nil {
220 SessionOn = sessionon 219 SessionOn = sessionon.(bool)
221 } 220 }
222 221
223 if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" { 222 if sessProvider, _ := getConfig("string", "SessionProvider"); sessProvider != "" {
224 SessionProvider = sessProvider 223 SessionProvider = sessProvider.(string)
225 } 224 }
226 225
227 if sessName := AppConfig.String("SessionName"); sessName != "" { 226 if sessName, _ := getConfig("string", "SessionName"); sessName != "" {
228 SessionName = sessName 227 SessionName = sessName.(string)
229 } 228 }
230 229
231 if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" { 230 if sesssavepath, _ := getConfig("string", "SessionSavePath"); sesssavepath != "" {
232 SessionSavePath = sesssavepath 231 SessionSavePath = sesssavepath.(string)
233 } 232 }
234 233
235 if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" { 234 if sesshashfunc, _ := getConfig("string", "SessionHashFunc"); sesshashfunc != "" {
236 SessionHashFunc = sesshashfunc 235 SessionHashFunc = sesshashfunc.(string)
237 } 236 }
238 237
239 if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" { 238 if sesshashkey, _ := getConfig("string", "SessionHashKey"); sesshashkey != "" {
240 SessionHashKey = sesshashkey 239 SessionHashKey = sesshashkey.(string)
241 } 240 }
242 241
243 if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { 242 if sessMaxLifeTime, err := getConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
244 int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64) 243 SessionGCMaxLifetime = sessMaxLifeTime.(int64)
245 SessionGCMaxLifetime = int64val
246 } 244 }
247 245
248 if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { 246 if sesscookielifetime, err := getConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
249 SessionCookieLifeTime = sesscookielifetime 247 SessionCookieLifeTime = sesscookielifetime.(int)
250 } 248 }
251 249
252 if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil { 250 if usefcgi, err := getConfig("bool", "UseFcgi"); err == nil {
253 UseFcgi = usefcgi 251 UseFcgi = usefcgi.(bool)
254 } 252 }
255 253
256 if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { 254 if enablegzip, err := getConfig("bool", "EnableGzip"); err == nil {
257 EnableGzip = enablegzip 255 EnableGzip = enablegzip.(bool)
258 } 256 }
259 257
260 if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil { 258 if directoryindex, err := getConfig("bool", "DirectoryIndex"); err == nil {
261 DirectoryIndex = directoryindex 259 DirectoryIndex = directoryindex.(bool)
262 } 260 }
263 261
264 if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil { 262 if timeout, err := getConfig("int64", "HttpServerTimeOut"); err == nil {
265 HttpServerTimeOut = timeout 263 HttpServerTimeOut = timeout.(int64)
266 } 264 }
267 265
268 if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil { 266 if errorsshow, err := getConfig("bool", "ErrorsShow"); err == nil {
269 ErrorsShow = errorsshow 267 ErrorsShow = errorsshow.(bool)
270 } 268 }
271 269
272 if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil { 270 if copyrequestbody, err := getConfig("bool", "CopyRequestBody"); err == nil {
273 CopyRequestBody = copyrequestbody 271 CopyRequestBody = copyrequestbody.(bool)
274 } 272 }
275 273
276 if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" { 274 if xsrfkey, _ := getConfig("string", "XSRFKEY"); xsrfkey != "" {
277 XSRFKEY = xsrfkey 275 XSRFKEY = xsrfkey.(string)
278 } 276 }
279 277
280 if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil { 278 if enablexsrf, err := getConfig("bool", "EnableXSRF"); err == nil {
281 EnableXSRF = enablexsrf 279 EnableXSRF = enablexsrf.(bool)
282 } 280 }
283 281
284 if expire, err := AppConfig.Int("XSRFExpire"); err == nil { 282 if expire, err := getConfig("int", "XSRFExpire"); err == nil {
285 XSRFExpire = expire 283 XSRFExpire = expire.(int)
286 } 284 }
287 285
288 if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" { 286 if tplleft, _ := getConfig("string", "TemplateLeft"); tplleft != "" {
289 TemplateLeft = tplleft 287 TemplateLeft = tplleft.(string)
290 } 288 }
291 289
292 if tplright := AppConfig.String("TemplateRight"); tplright != "" { 290 if tplright, _ := getConfig("string", "TemplateRight"); tplright != "" {
293 TemplateRight = tplright 291 TemplateRight = tplright.(string)
294 } 292 }
295 293
296 if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil { 294 if httptls, err := getConfig("bool", "EnableHttpTLS"); err == nil {
297 EnableHttpTLS = httptls 295 EnableHttpTLS = httptls.(bool)
298 } 296 }
299 297
300 if httpsport, err := AppConfig.Int("HttpsPort"); err == nil { 298 if httpsport, err := getConfig("int", "HttpsPort"); err == nil {
301 HttpsPort = httpsport 299 HttpsPort = httpsport.(int)
302 } 300 }
303 301
304 if certfile := AppConfig.String("HttpCertFile"); certfile != "" { 302 if certfile, _ := getConfig("string", "HttpCertFile"); certfile != "" {
305 HttpCertFile = certfile 303 HttpCertFile = certfile.(string)
306 } 304 }
307 305
308 if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" { 306 if keyfile, _ := getConfig("string", "HttpKeyFile"); keyfile != "" {
309 HttpKeyFile = keyfile 307 HttpKeyFile = keyfile.(string)
310 } 308 }
311 309
312 if serverName := AppConfig.String("BeegoServerName"); serverName != "" { 310 if serverName, _ := getConfig("string", "BeegoServerName"); serverName != "" {
313 BeegoServerName = serverName 311 BeegoServerName = serverName.(string)
314 } 312 }
315 313
316 if flashname := AppConfig.String("FlashName"); flashname != "" { 314 if flashname, _ := getConfig("string", "FlashName"); flashname != "" {
317 FlashName = flashname 315 FlashName = flashname.(string)
318 } 316 }
319 317
320 if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" { 318 if flashseperator, _ := getConfig("string", "FlashSeperator"); flashseperator != "" {
321 FlashSeperator = flashseperator 319 FlashSeperator = flashseperator.(string)
322 } 320 }
323 321
324 if sd := AppConfig.String("StaticDir"); sd != "" { 322 if sd, _ := getConfig("string", "StaticDir"); sd != "" {
325 for k := range StaticDir { 323 for k := range StaticDir {
326 delete(StaticDir, k) 324 delete(StaticDir, k)
327 } 325 }
328 sds := strings.Fields(sd) 326 sds := strings.Fields(sd.(string))
329 for _, v := range sds { 327 for _, v := range sds {
330 if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 { 328 if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
331 StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1] 329 StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
...@@ -335,8 +333,8 @@ func ParseConfig() (err error) { ...@@ -335,8 +333,8 @@ func ParseConfig() (err error) {
335 } 333 }
336 } 334 }
337 335
338 if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" { 336 if sgz, _ := getConfig("string", "StaticExtensionsToGzip"); sgz != "" {
339 extensions := strings.Split(sgz, ",") 337 extensions := strings.Split(sgz.(string), ",")
340 if len(extensions) > 0 { 338 if len(extensions) > 0 {
341 StaticExtensionsToGzip = []string{} 339 StaticExtensionsToGzip = []string{}
342 for _, ext := range extensions { 340 for _, ext := range extensions {
...@@ -352,17 +350,59 @@ func ParseConfig() (err error) { ...@@ -352,17 +350,59 @@ func ParseConfig() (err error) {
352 } 350 }
353 } 351 }
354 352
355 if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil { 353 if enableadmin, err := getConfig("bool", "EnableAdmin"); err == nil {
356 EnableAdmin = enableadmin 354 EnableAdmin = enableadmin.(bool)
357 } 355 }
358 356
359 if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" { 357 if adminhttpaddr, _ := getConfig("string", "AdminHttpAddr"); adminhttpaddr != "" {
360 AdminHttpAddr = adminhttpaddr 358 AdminHttpAddr = adminhttpaddr.(string)
361 } 359 }
362 360
363 if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil { 361 if adminhttpport, err := getConfig("int", "AdminHttpPort"); err == nil {
364 AdminHttpPort = adminhttpport 362 AdminHttpPort = adminhttpport.(int)
365 } 363 }
366 } 364 }
367 return nil 365 return nil
368 } 366 }
367
368 func getConfig(typ, key string) (interface{}, error) {
369 switch typ {
370 case "string":
371 v := AppConfig.String(RunMode + "::" + key)
372 if v == "" {
373 v = AppConfig.String(key)
374 }
375 return v, nil
376 case "strings":
377 v := AppConfig.Strings(RunMode + "::" + key)
378 if len(v) == 0 {
379 v = AppConfig.Strings(key)
380 }
381 return v, nil
382 case "int":
383 v, err := AppConfig.Int(RunMode + "::" + key)
384 if err != nil || v == 0 {
385 return AppConfig.Int(key)
386 }
387 return v, nil
388 case "bool":
389 v, err := AppConfig.Bool(RunMode + "::" + key)
390 if err != nil {
391 return AppConfig.Bool(key)
392 }
393 return v, nil
394 case "int64":
395 v, err := AppConfig.Int64(RunMode + "::" + key)
396 if err != nil || v == 0 {
397 return AppConfig.Int64(key)
398 }
399 return v, nil
400 case "float":
401 v, err := AppConfig.Float(RunMode + "::" + key)
402 if err != nil || v == 0 {
403 return AppConfig.Float(key)
404 }
405 return v, nil
406 }
407 return "", errors.New("not support type")
408 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!