33ad6c13 by astaxie

beego: remove app funciont & fix #590

config := tls.Config{
    ClientAuth: tls.RequireAndVerifyClientCert,
    Certificates: []tls.Certificate{cert},
    ClientCAs: pool,
}
config.Rand = rand.Reader

beego.BeeApp.Server. TLSConfig = &config
1 parent 04290dfc
...@@ -22,12 +22,13 @@ type FilterFunc func(*context.Context) ...@@ -22,12 +22,13 @@ type FilterFunc func(*context.Context)
22 // App defines beego application with a new PatternServeMux. 22 // App defines beego application with a new PatternServeMux.
23 type App struct { 23 type App struct {
24 Handlers *ControllerRegistor 24 Handlers *ControllerRegistor
25 Server *http.Server
25 } 26 }
26 27
27 // NewApp returns a new beego application. 28 // NewApp returns a new beego application.
28 func NewApp() *App { 29 func NewApp() *App {
29 cr := NewControllerRegistor() 30 cr := NewControllerRegistor()
30 app := &App{Handlers: cr} 31 app := &App{Handlers: cr, Server: &http.Server{}}
31 return app 32 return app
32 } 33 }
33 34
...@@ -58,18 +59,17 @@ func (app *App) Run() { ...@@ -58,18 +59,17 @@ func (app *App) Run() {
58 } 59 }
59 err = fcgi.Serve(l, app.Handlers) 60 err = fcgi.Serve(l, app.Handlers)
60 } else { 61 } else {
61 s := &http.Server{ 62 app.Server.Addr = addr
62 Addr: addr, 63 app.Server.Handler = app.Handlers
63 Handler: app.Handlers, 64 app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
64 ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second, 65 app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
65 WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second, 66
66 }
67 if EnableHttpTLS { 67 if EnableHttpTLS {
68 go func() { 68 go func() {
69 if HttpsPort != 0 { 69 if HttpsPort != 0 {
70 s.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort) 70 app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
71 } 71 }
72 err := s.ListenAndServeTLS(HttpCertFile, HttpKeyFile) 72 err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
73 if err != nil { 73 if err != nil {
74 BeeLogger.Critical("ListenAndServeTLS: ", err) 74 BeeLogger.Critical("ListenAndServeTLS: ", err)
75 time.Sleep(100 * time.Microsecond) 75 time.Sleep(100 * time.Microsecond)
...@@ -80,7 +80,7 @@ func (app *App) Run() { ...@@ -80,7 +80,7 @@ func (app *App) Run() {
80 80
81 if EnableHttpListen { 81 if EnableHttpListen {
82 go func() { 82 go func() {
83 err := s.ListenAndServe() 83 err := app.Server.ListenAndServe()
84 if err != nil { 84 if err != nil {
85 BeeLogger.Critical("ListenAndServe: ", err) 85 BeeLogger.Critical("ListenAndServe: ", err)
86 time.Sleep(100 * time.Microsecond) 86 time.Sleep(100 * time.Microsecond)
...@@ -92,141 +92,3 @@ func (app *App) Run() { ...@@ -92,141 +92,3 @@ func (app *App) Run() {
92 92
93 <-endRunning 93 <-endRunning
94 } 94 }
95
96 // Router adds a url-patterned controller handler.
97 // The path argument supports regex rules and specific placeholders.
98 // The c argument needs a controller handler implemented beego.ControllerInterface.
99 // The mapping methods argument only need one string to define custom router rules.
100 // usage:
101 // simple router
102 // beego.Router("/admin", &admin.UserController{})
103 // beego.Router("/admin/index", &admin.ArticleController{})
104 //
105 // regex router
106 //
107 // beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
108 //
109 // custom rules
110 // beego.Router("/api/list",&RestController{},"*:ListFood")
111 // beego.Router("/api/create",&RestController{},"post:CreateFood")
112 // beego.Router("/api/update",&RestController{},"put:UpdateFood")
113 // beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
114 func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App {
115 app.Handlers.Add(path, c, mappingMethods...)
116 return app
117 }
118
119 // AutoRouter adds beego-defined controller handler.
120 // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
121 // visit the url /main/list to exec List function or /main/page to exec Page function.
122 func (app *App) AutoRouter(c ControllerInterface) *App {
123 app.Handlers.AddAuto(c)
124 return app
125 }
126
127 // AutoRouterWithPrefix adds beego-defined controller handler with prefix.
128 // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
129 // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
130 func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App {
131 app.Handlers.AddAutoPrefix(prefix, c)
132 return app
133 }
134
135 // add router for Get method
136 func (app *App) Get(rootpath string, f FilterFunc) *App {
137 app.Handlers.Get(rootpath, f)
138 return app
139 }
140
141 // add router for Post method
142 func (app *App) Post(rootpath string, f FilterFunc) *App {
143 app.Handlers.Post(rootpath, f)
144 return app
145 }
146
147 // add router for Put method
148 func (app *App) Put(rootpath string, f FilterFunc) *App {
149 app.Handlers.Put(rootpath, f)
150 return app
151 }
152
153 // add router for Delete method
154 func (app *App) Delete(rootpath string, f FilterFunc) *App {
155 app.Handlers.Delete(rootpath, f)
156 return app
157 }
158
159 // add router for Options method
160 func (app *App) Options(rootpath string, f FilterFunc) *App {
161 app.Handlers.Options(rootpath, f)
162 return app
163 }
164
165 // add router for Head method
166 func (app *App) Head(rootpath string, f FilterFunc) *App {
167 app.Handlers.Head(rootpath, f)
168 return app
169 }
170
171 // add router for Patch method
172 func (app *App) Patch(rootpath string, f FilterFunc) *App {
173 app.Handlers.Patch(rootpath, f)
174 return app
175 }
176
177 // add router for Patch method
178 func (app *App) Any(rootpath string, f FilterFunc) *App {
179 app.Handlers.Any(rootpath, f)
180 return app
181 }
182
183 // add router for http.Handler
184 func (app *App) Handler(rootpath string, h http.Handler, options ...interface{}) *App {
185 app.Handlers.Handler(rootpath, h, options...)
186 return app
187 }
188
189 // UrlFor creates a url with another registered controller handler with params.
190 // The endpoint is formed as path.controller.name to defined the controller method which will run.
191 // The values need key-pair data to assign into controller method.
192 func (app *App) UrlFor(endpoint string, values ...string) string {
193 return app.Handlers.UrlFor(endpoint, values...)
194 }
195
196 // [Deprecated] use InsertFilter.
197 // Filter adds a FilterFunc under pattern condition and named action.
198 // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
199 func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
200 app.Handlers.AddFilter(pattern, action, filter)
201 return app
202 }
203
204 // InsertFilter adds a FilterFunc with pattern condition and action constant.
205 // The pos means action constant including
206 // beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
207 func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App {
208 app.Handlers.InsertFilter(pattern, pos, filter)
209 return app
210 }
211
212 // SetViewsPath sets view directory path in beego application.
213 // it returns beego application self.
214 func (app *App) SetViewsPath(path string) *App {
215 ViewsPath = path
216 return app
217 }
218
219 // SetStaticPath sets static directory path and proper url pattern in beego application.
220 // if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
221 // it returns beego application self.
222 func (app *App) SetStaticPath(url string, path string) *App {
223 StaticDir[url] = path
224 return app
225 }
226
227 // DelStaticPath removes the static folder setting in this url pattern in beego application.
228 // it returns beego application self.
229 func (app *App) DelStaticPath(url string) *App {
230 delete(StaticDir, url)
231 return app
232 }
......
...@@ -80,11 +80,11 @@ func (gr *GroupRouters) AddAuto(c ControllerInterface) { ...@@ -80,11 +80,11 @@ func (gr *GroupRouters) AddAuto(c ControllerInterface) {
80 func AddGroupRouter(prefix string, groups GroupRouters) *App { 80 func AddGroupRouter(prefix string, groups GroupRouters) *App {
81 for _, v := range groups { 81 for _, v := range groups {
82 if v.pattern == "" { 82 if v.pattern == "" {
83 BeeApp.AutoRouterWithPrefix(prefix, v.controller) 83 BeeApp.Handlers.AddAutoPrefix(prefix, v.controller)
84 } else if v.mappingMethods != "" { 84 } else if v.mappingMethods != "" {
85 BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods) 85 BeeApp.Handlers.Add(prefix+v.pattern, v.controller, v.mappingMethods)
86 } else { 86 } else {
87 BeeApp.Router(prefix+v.pattern, v.controller) 87 BeeApp.Handlers.Add(prefix+v.pattern, v.controller)
88 } 88 }
89 89
90 } 90 }
...@@ -93,8 +93,22 @@ func AddGroupRouter(prefix string, groups GroupRouters) *App { ...@@ -93,8 +93,22 @@ func AddGroupRouter(prefix string, groups GroupRouters) *App {
93 93
94 // Router adds a patterned controller handler to BeeApp. 94 // Router adds a patterned controller handler to BeeApp.
95 // it's an alias method of App.Router. 95 // it's an alias method of App.Router.
96 // usage:
97 // simple router
98 // beego.Router("/admin", &admin.UserController{})
99 // beego.Router("/admin/index", &admin.ArticleController{})
100 //
101 // regex router
102 //
103 // beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
104 //
105 // custom rules
106 // beego.Router("/api/list",&RestController{},"*:ListFood")
107 // beego.Router("/api/create",&RestController{},"post:CreateFood")
108 // beego.Router("/api/update",&RestController{},"put:UpdateFood")
109 // beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
96 func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App { 110 func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
97 BeeApp.Router(rootpath, c, mappingMethods...) 111 BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
98 return BeeApp 112 return BeeApp
99 } 113 }
100 114
...@@ -109,69 +123,73 @@ func RESTRouter(rootpath string, c ControllerInterface) *App { ...@@ -109,69 +123,73 @@ func RESTRouter(rootpath string, c ControllerInterface) *App {
109 123
110 // AutoRouter adds defined controller handler to BeeApp. 124 // AutoRouter adds defined controller handler to BeeApp.
111 // it's same to App.AutoRouter. 125 // it's same to App.AutoRouter.
126 // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
127 // visit the url /main/list to exec List function or /main/page to exec Page function.
112 func AutoRouter(c ControllerInterface) *App { 128 func AutoRouter(c ControllerInterface) *App {
113 BeeApp.AutoRouter(c) 129 BeeApp.Handlers.AddAuto(c)
114 return BeeApp 130 return BeeApp
115 } 131 }
116 132
117 // AutoPrefix adds controller handler to BeeApp with prefix. 133 // AutoPrefix adds controller handler to BeeApp with prefix.
118 // it's same to App.AutoRouterWithPrefix. 134 // it's same to App.AutoRouterWithPrefix.
135 // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
136 // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
119 func AutoPrefix(prefix string, c ControllerInterface) *App { 137 func AutoPrefix(prefix string, c ControllerInterface) *App {
120 BeeApp.AutoRouterWithPrefix(prefix, c) 138 BeeApp.Handlers.AddAutoPrefix(prefix, c)
121 return BeeApp 139 return BeeApp
122 } 140 }
123 141
124 // register router for Get method 142 // register router for Get method
125 func Get(rootpath string, f FilterFunc) *App { 143 func Get(rootpath string, f FilterFunc) *App {
126 BeeApp.Get(rootpath, f) 144 BeeApp.Handlers.Get(rootpath, f)
127 return BeeApp 145 return BeeApp
128 } 146 }
129 147
130 // register router for Post method 148 // register router for Post method
131 func Post(rootpath string, f FilterFunc) *App { 149 func Post(rootpath string, f FilterFunc) *App {
132 BeeApp.Post(rootpath, f) 150 BeeApp.Handlers.Post(rootpath, f)
133 return BeeApp 151 return BeeApp
134 } 152 }
135 153
136 // register router for Delete method 154 // register router for Delete method
137 func Delete(rootpath string, f FilterFunc) *App { 155 func Delete(rootpath string, f FilterFunc) *App {
138 BeeApp.Delete(rootpath, f) 156 BeeApp.Handlers.Delete(rootpath, f)
139 return BeeApp 157 return BeeApp
140 } 158 }
141 159
142 // register router for Put method 160 // register router for Put method
143 func Put(rootpath string, f FilterFunc) *App { 161 func Put(rootpath string, f FilterFunc) *App {
144 BeeApp.Put(rootpath, f) 162 BeeApp.Handlers.Put(rootpath, f)
145 return BeeApp 163 return BeeApp
146 } 164 }
147 165
148 // register router for Head method 166 // register router for Head method
149 func Head(rootpath string, f FilterFunc) *App { 167 func Head(rootpath string, f FilterFunc) *App {
150 BeeApp.Head(rootpath, f) 168 BeeApp.Handlers.Head(rootpath, f)
151 return BeeApp 169 return BeeApp
152 } 170 }
153 171
154 // register router for Options method 172 // register router for Options method
155 func Options(rootpath string, f FilterFunc) *App { 173 func Options(rootpath string, f FilterFunc) *App {
156 BeeApp.Options(rootpath, f) 174 BeeApp.Handlers.Options(rootpath, f)
157 return BeeApp 175 return BeeApp
158 } 176 }
159 177
160 // register router for Patch method 178 // register router for Patch method
161 func Patch(rootpath string, f FilterFunc) *App { 179 func Patch(rootpath string, f FilterFunc) *App {
162 BeeApp.Patch(rootpath, f) 180 BeeApp.Handlers.Patch(rootpath, f)
163 return BeeApp 181 return BeeApp
164 } 182 }
165 183
166 // register router for all method 184 // register router for all method
167 func Any(rootpath string, f FilterFunc) *App { 185 func Any(rootpath string, f FilterFunc) *App {
168 BeeApp.Any(rootpath, f) 186 BeeApp.Handlers.Any(rootpath, f)
169 return BeeApp 187 return BeeApp
170 } 188 }
171 189
172 // register router for own Handler 190 // register router for own Handler
173 func Handler(rootpath string, h http.Handler, options ...interface{}) *App { 191 func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
174 BeeApp.Handler(rootpath, h, options...) 192 BeeApp.Handlers.Handler(rootpath, h, options...)
175 return BeeApp 193 return BeeApp
176 } 194 }
177 195
...@@ -184,15 +202,14 @@ func Errorhandler(err string, h http.HandlerFunc) *App { ...@@ -184,15 +202,14 @@ func Errorhandler(err string, h http.HandlerFunc) *App {
184 return BeeApp 202 return BeeApp
185 } 203 }
186 204
187 // SetViewsPath sets view directory to BeeApp. 205 // SetViewsPath sets view directory path in beego application.
188 // it's alias of App.SetViewsPath.
189 func SetViewsPath(path string) *App { 206 func SetViewsPath(path string) *App {
190 BeeApp.SetViewsPath(path) 207 ViewsPath = path
191 return BeeApp 208 return BeeApp
192 } 209 }
193 210
194 // SetStaticPath sets static directory and url prefix to BeeApp. 211 // SetStaticPath sets static directory path and proper url pattern in beego application.
195 // it's alias of App.SetStaticPath. 212 // if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
196 func SetStaticPath(url string, path string) *App { 213 func SetStaticPath(url string, path string) *App {
197 if !strings.HasPrefix(url, "/") { 214 if !strings.HasPrefix(url, "/") {
198 url = "/" + url 215 url = "/" + url
...@@ -203,7 +220,6 @@ func SetStaticPath(url string, path string) *App { ...@@ -203,7 +220,6 @@ func SetStaticPath(url string, path string) *App {
203 } 220 }
204 221
205 // DelStaticPath removes the static folder setting in this url pattern in beego application. 222 // DelStaticPath removes the static folder setting in this url pattern in beego application.
206 // it's alias of App.DelStaticPath.
207 func DelStaticPath(url string) *App { 223 func DelStaticPath(url string) *App {
208 delete(StaticDir, url) 224 delete(StaticDir, url)
209 return BeeApp 225 return BeeApp
...@@ -212,18 +228,16 @@ func DelStaticPath(url string) *App { ...@@ -212,18 +228,16 @@ func DelStaticPath(url string) *App {
212 // [Deprecated] use InsertFilter. 228 // [Deprecated] use InsertFilter.
213 // Filter adds a FilterFunc under pattern condition and named action. 229 // Filter adds a FilterFunc under pattern condition and named action.
214 // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter. 230 // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
215 // it's alias of App.Filter.
216 func AddFilter(pattern, action string, filter FilterFunc) *App { 231 func AddFilter(pattern, action string, filter FilterFunc) *App {
217 BeeApp.Filter(pattern, action, filter) 232 BeeApp.Handlers.AddFilter(pattern, action, filter)
218 return BeeApp 233 return BeeApp
219 } 234 }
220 235
221 // InsertFilter adds a FilterFunc with pattern condition and action constant. 236 // InsertFilter adds a FilterFunc with pattern condition and action constant.
222 // The pos means action constant including 237 // The pos means action constant including
223 // beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter. 238 // beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
224 // it's alias of App.InsertFilter.
225 func InsertFilter(pattern string, pos int, filter FilterFunc) *App { 239 func InsertFilter(pattern string, pos int, filter FilterFunc) *App {
226 BeeApp.InsertFilter(pattern, pos, filter) 240 BeeApp.Handlers.InsertFilter(pattern, pos, filter)
227 return BeeApp 241 return BeeApp
228 } 242 }
229 243
......
...@@ -202,7 +202,7 @@ func Htmlunquote(src string) string { ...@@ -202,7 +202,7 @@ func Htmlunquote(src string) string {
202 // 202 //
203 // more detail http://beego.me/docs/mvc/controller/urlbuilding.md 203 // more detail http://beego.me/docs/mvc/controller/urlbuilding.md
204 func UrlFor(endpoint string, values ...string) string { 204 func UrlFor(endpoint string, values ...string) string {
205 return BeeApp.UrlFor(endpoint, values...) 205 return BeeApp.Handlers.UrlFor(endpoint, values...)
206 } 206 }
207 207
208 // returns script tag with src string. 208 // returns script tag with src string.
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!