ec745693 by astaxie

Merge pull request #327 from pengfei-xue/devel

remove duplicated config initialization
2 parents 9c4414f9 9cac7504
Showing 1 changed file with 261 additions and 340 deletions
1 package beego 1 package beego
2 2
3 import ( 3 import (
4 "github.com/astaxie/beego/config" 4 "github.com/astaxie/beego/config"
5 "github.com/astaxie/beego/session" 5 "github.com/astaxie/beego/session"
6 "html/template" 6 "html/template"
7 "os" 7 "os"
8 "path" 8 "path"
9 "runtime" 9 "runtime"
10 "strconv" 10 "strconv"
11 "strings" 11 "strings"
12 ) 12 )
13 13
14 var ( 14 var (
15 BeeApp *App 15 BeeApp *App
16 AppName string 16 AppName string
17 AppPath string 17 AppPath string
18 AppConfigPath string 18 AppConfigPath string
19 StaticDir map[string]string 19 StaticDir map[string]string
20 TemplateCache map[string]*template.Template 20 TemplateCache map[string]*template.Template
21 HttpAddr string 21 HttpAddr string
22 HttpPort int 22 HttpPort int
23 HttpTLS bool 23 HttpTLS bool
24 HttpCertFile string 24 HttpCertFile string
25 HttpKeyFile string 25 HttpKeyFile string
26 RecoverPanic bool 26 RecoverPanic bool
27 AutoRender bool 27 AutoRender bool
28 PprofOn bool 28 PprofOn bool
29 ViewsPath string 29 ViewsPath string
30 RunMode string //"dev" or "prod" 30 RunMode string //"dev" or "prod"
31 AppConfig config.ConfigContainer 31 AppConfig config.ConfigContainer
32 //related to session 32 //related to session
33 GlobalSessions *session.Manager //GlobalSessions 33 GlobalSessions *session.Manager //GlobalSessions
34 SessionOn bool // whether auto start session,default is false 34 SessionOn bool // whether auto start session,default is false
35 SessionProvider string // default session provider memory mysql redis 35 SessionProvider string // default session provider memory mysql redis
36 SessionName string // sessionName cookie's name 36 SessionName string // sessionName cookie's name
37 SessionGCMaxLifetime int64 // session's gc maxlifetime 37 SessionGCMaxLifetime int64 // session's gc maxlifetime
38 SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo 38 SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo
39 SessionHashFunc string 39 SessionHashFunc string
40 SessionHashKey string 40 SessionHashKey string
41 SessionCookieLifeTime int 41 SessionCookieLifeTime int
42 UseFcgi bool 42 UseFcgi bool
43 MaxMemory int64 43 MaxMemory int64
44 EnableGzip bool // enable gzip 44 EnableGzip bool // enable gzip
45 DirectoryIndex bool //enable DirectoryIndex default is false 45 DirectoryIndex bool //enable DirectoryIndex default is false
46 EnableHotUpdate bool //enable HotUpdate default is false 46 EnableHotUpdate bool //enable HotUpdate default is false
47 HttpServerTimeOut int64 //set httpserver timeout 47 HttpServerTimeOut int64 //set httpserver timeout
48 ErrorsShow bool //set weather show errors 48 ErrorsShow bool //set weather show errors
49 XSRFKEY string //set XSRF 49 XSRFKEY string //set XSRF
50 EnableXSRF bool 50 EnableXSRF bool
51 XSRFExpire int 51 XSRFExpire int
52 CopyRequestBody bool //When in raw application, You want to the reqeustbody 52 CopyRequestBody bool //When in raw application, You want to the reqeustbody
53 TemplateLeft string 53 TemplateLeft string
54 TemplateRight string 54 TemplateRight string
55 BeegoServerName string 55 BeegoServerName string
56 EnableAdmin bool //enable admin module to log api time 56 EnableAdmin bool //enable admin module to log api time
57 AdminHttpAddr string //admin module http addr 57 AdminHttpAddr string //admin module http addr
58 AdminHttpPort int 58 AdminHttpPort int
59 ) 59 )
60 60
61 func init() { 61 func init() {
62 os.Chdir(path.Dir(os.Args[0])) 62 os.Chdir(path.Dir(os.Args[0]))
63 BeeApp = NewApp() 63 BeeApp = NewApp()
64 AppPath = path.Dir(os.Args[0]) 64 AppPath = path.Dir(os.Args[0])
65 StaticDir = make(map[string]string) 65 StaticDir = make(map[string]string)
66 TemplateCache = make(map[string]*template.Template) 66 TemplateCache = make(map[string]*template.Template)
67 HttpAddr = "" 67 HttpAddr = ""
68 HttpPort = 8080 68 HttpPort = 8080
69 AppName = "beego" 69 AppName = "beego"
70 RunMode = "dev" //default runmod 70 RunMode = "dev" //default runmod
71 AutoRender = true 71 AutoRender = true
72 RecoverPanic = true 72 RecoverPanic = true
73 PprofOn = false 73 PprofOn = false
74 ViewsPath = "views" 74 ViewsPath = "views"
75 SessionOn = false 75 SessionOn = false
76 SessionProvider = "memory" 76 SessionProvider = "memory"
77 SessionName = "beegosessionID" 77 SessionName = "beegosessionID"
78 SessionGCMaxLifetime = 3600 78 SessionGCMaxLifetime = 3600
79 SessionSavePath = "" 79 SessionSavePath = ""
80 SessionHashFunc = "sha1" 80 SessionHashFunc = "sha1"
81 SessionHashKey = "beegoserversessionkey" 81 SessionHashKey = "beegoserversessionkey"
82 SessionCookieLifeTime = 3600 82 SessionCookieLifeTime = 3600
83 UseFcgi = false 83 UseFcgi = false
84 MaxMemory = 1 << 26 //64MB 84 MaxMemory = 1 << 26 //64MB
85 EnableGzip = false 85 EnableGzip = false
86 StaticDir["/static"] = "static" 86 StaticDir["/static"] = "static"
87 AppConfigPath = path.Join(AppPath, "conf", "app.conf") 87 AppConfigPath = path.Join(AppPath, "conf", "app.conf")
88 HttpServerTimeOut = 0 88 HttpServerTimeOut = 0
89 ErrorsShow = true 89 ErrorsShow = true
90 XSRFKEY = "beegoxsrf" 90 XSRFKEY = "beegoxsrf"
91 XSRFExpire = 0 91 XSRFExpire = 0
92 TemplateLeft = "{{" 92 TemplateLeft = "{{"
93 TemplateRight = "}}" 93 TemplateRight = "}}"
94 BeegoServerName = "beegoServer" 94 BeegoServerName = "beegoServer"
95 EnableAdmin = true 95 EnableAdmin = true
96 AdminHttpAddr = "localhost" 96 AdminHttpAddr = "localhost"
97 AdminHttpPort = 8088 97 AdminHttpPort = 8088
98 ParseConfig() 98 ParseConfig()
99 runtime.GOMAXPROCS(runtime.NumCPU()) 99 runtime.GOMAXPROCS(runtime.NumCPU())
100 } 100 }
101 101
102 func ParseConfig() (err error) { 102 func ParseConfig() (err error) {
103 AppConfig, err = config.NewConfig("ini", AppConfigPath) 103 AppConfig, err = config.NewConfig("ini", AppConfigPath)
104 if err != nil { 104 if err != nil {
105 return err 105 return err
106 } else { 106 } else {
107 HttpAddr = AppConfig.String("httpaddr") 107 HttpAddr = AppConfig.String("HttpAddr")
108 if v, err := AppConfig.Int("httpport"); err == nil { 108
109 HttpPort = v 109 if v, err := AppConfig.Int("HttpPort"); err == nil {
110 } 110 HttpPort = v
111 if v, err := AppConfig.Int("HttpPort"); err == nil { 111 }
112 HttpPort = v 112
113 } 113 if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
114 if maxmemory, err := AppConfig.Int64("maxmemory"); err == nil { 114 MaxMemory = maxmemory
115 MaxMemory = maxmemory 115 }
116 } 116
117 if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil { 117 if appname := AppConfig.String("AppName"); appname != "" {
118 MaxMemory = maxmemory 118 AppName = appname
119 } 119 }
120 AppName = AppConfig.String("appname") 120
121 if appname := AppConfig.String("AppName"); appname != "" { 121 if runmode := AppConfig.String("RunMode"); runmode != "" {
122 AppName = appname 122 RunMode = runmode
123 } 123 }
124 if runmode := AppConfig.String("runmode"); runmode != "" { 124
125 RunMode = runmode 125 if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
126 } 126 AutoRender = autorender
127 if runmode := AppConfig.String("RunMode"); runmode != "" { 127 }
128 RunMode = runmode 128
129 } 129 if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
130 if autorender, err := AppConfig.Bool("autorender"); err == nil { 130 RecoverPanic = autorecover
131 AutoRender = autorender 131 }
132 } 132
133 if autorender, err := AppConfig.Bool("AutoRender"); err == nil { 133 if views := AppConfig.String("ViewsPath"); views != "" {
134 AutoRender = autorender 134 ViewsPath = views
135 } 135 }
136 if autorecover, err := AppConfig.Bool("autorecover"); err == nil { 136
137 RecoverPanic = autorecover 137 if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
138 } 138 SessionOn = sessionon
139 if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil { 139 }
140 RecoverPanic = autorecover 140
141 } 141 if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
142 if views := AppConfig.String("viewspath"); views != "" { 142 SessionProvider = sessProvider
143 ViewsPath = views 143 }
144 } 144
145 if views := AppConfig.String("ViewsPath"); views != "" { 145 if sessName := AppConfig.String("SessionName"); sessName != "" {
146 ViewsPath = views 146 SessionName = sessName
147 } 147 }
148 if sessionon, err := AppConfig.Bool("sessionon"); err == nil { 148
149 SessionOn = sessionon 149 if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
150 } 150 SessionSavePath = sesssavepath
151 if sessionon, err := AppConfig.Bool("SessionOn"); err == nil { 151 }
152 SessionOn = sessionon 152
153 } 153 if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" {
154 if sessProvider := AppConfig.String("sessionprovider"); sessProvider != "" { 154 SessionHashFunc = sesshashfunc
155 SessionProvider = sessProvider 155 }
156 } 156
157 if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" { 157 if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" {
158 SessionProvider = sessProvider 158 SessionHashKey = sesshashkey
159 } 159 }
160 if sessName := AppConfig.String("sessionname"); sessName != "" { 160
161 SessionName = sessName 161 if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
162 } 162 int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64)
163 if sessName := AppConfig.String("SessionName"); sessName != "" { 163 SessionGCMaxLifetime = int64val
164 SessionName = sessName 164 }
165 } 165
166 if sesssavepath := AppConfig.String("sessionsavepath"); sesssavepath != "" { 166 if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
167 SessionSavePath = sesssavepath 167 SessionCookieLifeTime = sesscookielifetime
168 } 168 }
169 if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" { 169
170 SessionSavePath = sesssavepath 170 if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
171 } 171 UseFcgi = usefcgi
172 if sesshashfunc := AppConfig.String("sessionhashfunc"); sesshashfunc != "" { 172 }
173 SessionHashFunc = sesshashfunc 173
174 } 174 if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
175 if sesshashfunc := AppConfig.String("SessionHashFunc"); sesshashfunc != "" { 175 EnableGzip = enablegzip
176 SessionHashFunc = sesshashfunc 176 }
177 } 177
178 if sesshashkey := AppConfig.String("sessionhashkey"); sesshashkey != "" { 178 if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
179 SessionHashKey = sesshashkey 179 DirectoryIndex = directoryindex
180 } 180 }
181 if sesshashkey := AppConfig.String("SessionHashKey"); sesshashkey != "" { 181
182 SessionHashKey = sesshashkey 182 if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil {
183 } 183 EnableHotUpdate = hotupdate
184 if sessMaxLifeTime, err := AppConfig.Int("sessiongcmaxlifetime"); err == nil && sessMaxLifeTime != 0 { 184 }
185 int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64) 185
186 SessionGCMaxLifetime = int64val 186 if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
187 } 187 HttpServerTimeOut = timeout
188 if sessMaxLifeTime, err := AppConfig.Int("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 { 188 }
189 int64val, _ := strconv.ParseInt(strconv.Itoa(sessMaxLifeTime), 10, 64) 189
190 SessionGCMaxLifetime = int64val 190 if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
191 } 191 ErrorsShow = errorsshow
192 if sesscookielifetime, err := AppConfig.Int("sessioncookielifelime"); err == nil && sesscookielifetime != 0 { 192 }
193 SessionCookieLifeTime = sesscookielifetime 193
194 } 194 if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
195 if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 { 195 CopyRequestBody = copyrequestbody
196 SessionCookieLifeTime = sesscookielifetime 196 }
197 } 197
198 if usefcgi, err := AppConfig.Bool("usefcgi"); err == nil { 198 if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
199 UseFcgi = usefcgi 199 XSRFKEY = xsrfkey
200 } 200 }
201 if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil { 201
202 UseFcgi = usefcgi 202 if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
203 } 203 EnableXSRF = enablexsrf
204 if enablegzip, err := AppConfig.Bool("enablegzip"); err == nil { 204 }
205 EnableGzip = enablegzip 205
206 } 206 if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
207 if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { 207 XSRFExpire = expire
208 EnableGzip = enablegzip 208 }
209 } 209
210 if directoryindex, err := AppConfig.Bool("directoryindex"); err == nil { 210 if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
211 DirectoryIndex = directoryindex 211 TemplateLeft = tplleft
212 } 212 }
213 if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil { 213
214 DirectoryIndex = directoryindex 214 if tplright := AppConfig.String("TemplateRight"); tplright != "" {
215 } 215 TemplateRight = tplright
216 if hotupdate, err := AppConfig.Bool("hotupdate"); err == nil { 216 }
217 EnableHotUpdate = hotupdate 217
218 } 218 if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
219 if hotupdate, err := AppConfig.Bool("HotUpdate"); err == nil { 219 HttpTLS = httptls
220 EnableHotUpdate = hotupdate 220 }
221 } 221
222 if timeout, err := AppConfig.Int64("httpservertimeout"); err == nil { 222 if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
223 HttpServerTimeOut = timeout 223 HttpCertFile = certfile
224 } 224 }
225 if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil { 225
226 HttpServerTimeOut = timeout 226 if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
227 } 227 HttpKeyFile = keyfile
228 if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil { 228 }
229 ErrorsShow = errorsshow 229
230 } 230 if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
231 if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil { 231 BeegoServerName = serverName
232 ErrorsShow = errorsshow 232 }
233 } 233
234 if copyrequestbody, err := AppConfig.Bool("copyrequestbody"); err == nil { 234 if sd := AppConfig.String("StaticDir"); sd != "" {
235 CopyRequestBody = copyrequestbody 235 for k := range StaticDir {
236 } 236 delete(StaticDir, k)
237 if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil { 237 }
238 CopyRequestBody = copyrequestbody 238 sds := strings.Fields(sd)
239 } 239 for _, v := range sds {
240 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" { 240 if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
241 XSRFKEY = xsrfkey 241 StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
242 } 242 } else {
243 if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" { 243 StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
244 XSRFKEY = xsrfkey 244 }
245 } 245 }
246 if enablexsrf, err := AppConfig.Bool("enablexsrf"); err == nil { 246 }
247 EnableXSRF = enablexsrf 247
248 } 248 if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
249 if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil { 249 EnableAdmin = enableadmin
250 EnableXSRF = enablexsrf 250 }
251 } 251
252 if expire, err := AppConfig.Int("xsrfexpire"); err == nil { 252 if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
253 XSRFExpire = expire 253 AdminHttpAddr = adminhttpaddr
254 } 254 }
255 if expire, err := AppConfig.Int("XSRFExpire"); err == nil { 255
256 XSRFExpire = expire 256 if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
257 } 257 AdminHttpPort = adminhttpport
258 if tplleft := AppConfig.String("templateleft"); tplleft != "" { 258 }
259 TemplateLeft = tplleft 259 }
260 } 260 return nil
261 if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" { 261 }
262 TemplateLeft = tplleft
263 }
264 if tplright := AppConfig.String("templateright"); tplright != "" {
265 TemplateRight = tplright
266 }
267 if tplright := AppConfig.String("TemplateRight"); tplright != "" {
268 TemplateRight = tplright
269 }
270 if httptls, err := AppConfig.Bool("httptls"); err == nil {
271 HttpTLS = httptls
272 }
273 if httptls, err := AppConfig.Bool("HttpTLS"); err == nil {
274 HttpTLS = httptls
275 }
276 if certfile := AppConfig.String("httpcertfile"); certfile != "" {
277 HttpCertFile = certfile
278 }
279 if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
280 HttpCertFile = certfile
281 }
282 if keyfile := AppConfig.String("httpkeyfile"); keyfile != "" {
283 HttpKeyFile = keyfile
284 }
285 if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
286 HttpKeyFile = keyfile
287 }
288 if serverName := AppConfig.String("beegoserverName"); serverName != "" {
289 BeegoServerName = serverName
290 }
291 if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
292 BeegoServerName = serverName
293 }
294 if sd := AppConfig.String("staticdir"); sd != "" {
295 for k := range StaticDir {
296 delete(StaticDir, k)
297 }
298 sds := strings.Fields(sd)
299 for _, v := range sds {
300 if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
301 StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
302 } else {
303 StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
304 }
305 }
306 }
307 if sd := AppConfig.String("StaticDir"); sd != "" {
308 for k := range StaticDir {
309 delete(StaticDir, k)
310 }
311 sds := strings.Fields(sd)
312 for _, v := range sds {
313 if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
314 StaticDir["/"+url2fsmap[0]] = url2fsmap[1]
315 } else {
316 StaticDir["/"+url2fsmap[0]] = url2fsmap[0]
317 }
318 }
319 }
320 if enableadmin, err := AppConfig.Bool("enableadmin"); err == nil {
321 EnableAdmin = enableadmin
322 }
323 if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
324 EnableAdmin = enableadmin
325 }
326 if adminhttpaddr := AppConfig.String("admintttpaddr"); adminhttpaddr != "" {
327 AdminHttpAddr = adminhttpaddr
328 }
329 if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
330 AdminHttpAddr = adminhttpaddr
331 }
332 if adminhttpport, err := AppConfig.Int("adminhttpport"); err == nil {
333 AdminHttpPort = adminhttpport
334 }
335 if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
336 AdminHttpPort = adminhttpport
337 }
338 }
339 return nil
340 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!