Commit 33ad6c13 authored by astaxie's avatar 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
parent 04290dfc
...@@ -22,12 +22,13 @@ type FilterFunc func(*context.Context) ...@@ -22,12 +22,13 @@ type FilterFunc func(*context.Context)
// App defines beego application with a new PatternServeMux. // App defines beego application with a new PatternServeMux.
type App struct { type App struct {
Handlers *ControllerRegistor Handlers *ControllerRegistor
Server *http.Server
} }
// NewApp returns a new beego application. // NewApp returns a new beego application.
func NewApp() *App { func NewApp() *App {
cr := NewControllerRegistor() cr := NewControllerRegistor()
app := &App{Handlers: cr} app := &App{Handlers: cr, Server: &http.Server{}}
return app return app
} }
...@@ -58,18 +59,17 @@ func (app *App) Run() { ...@@ -58,18 +59,17 @@ func (app *App) Run() {
} }
err = fcgi.Serve(l, app.Handlers) err = fcgi.Serve(l, app.Handlers)
} else { } else {
s := &http.Server{ app.Server.Addr = addr
Addr: addr, app.Server.Handler = app.Handlers
Handler: app.Handlers, app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second, app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second,
}
if EnableHttpTLS { if EnableHttpTLS {
go func() { go func() {
if HttpsPort != 0 { if HttpsPort != 0 {
s.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort) app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
} }
err := s.ListenAndServeTLS(HttpCertFile, HttpKeyFile) err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
if err != nil { if err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err) BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
...@@ -80,7 +80,7 @@ func (app *App) Run() { ...@@ -80,7 +80,7 @@ func (app *App) Run() {
if EnableHttpListen { if EnableHttpListen {
go func() { go func() {
err := s.ListenAndServe() err := app.Server.ListenAndServe()
if err != nil { if err != nil {
BeeLogger.Critical("ListenAndServe: ", err) BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
...@@ -92,141 +92,3 @@ func (app *App) Run() { ...@@ -92,141 +92,3 @@ func (app *App) Run() {
<-endRunning <-endRunning
} }
// Router adds a url-patterned controller handler.
// The path argument supports regex rules and specific placeholders.
// The c argument needs a controller handler implemented beego.ControllerInterface.
// The mapping methods argument only need one string to define custom router rules.
// usage:
// simple router
// beego.Router("/admin", &admin.UserController{})
// beego.Router("/admin/index", &admin.ArticleController{})
//
// regex router
//
// beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
//
// custom rules
// beego.Router("/api/list",&RestController{},"*:ListFood")
// beego.Router("/api/create",&RestController{},"post:CreateFood")
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App {
app.Handlers.Add(path, c, mappingMethods...)
return app
}
// AutoRouter adds beego-defined controller handler.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function.
func (app *App) AutoRouter(c ControllerInterface) *App {
app.Handlers.AddAuto(c)
return app
}
// AutoRouterWithPrefix adds beego-defined controller handler with prefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App {
app.Handlers.AddAutoPrefix(prefix, c)
return app
}
// add router for Get method
func (app *App) Get(rootpath string, f FilterFunc) *App {
app.Handlers.Get(rootpath, f)
return app
}
// add router for Post method
func (app *App) Post(rootpath string, f FilterFunc) *App {
app.Handlers.Post(rootpath, f)
return app
}
// add router for Put method
func (app *App) Put(rootpath string, f FilterFunc) *App {
app.Handlers.Put(rootpath, f)
return app
}
// add router for Delete method
func (app *App) Delete(rootpath string, f FilterFunc) *App {
app.Handlers.Delete(rootpath, f)
return app
}
// add router for Options method
func (app *App) Options(rootpath string, f FilterFunc) *App {
app.Handlers.Options(rootpath, f)
return app
}
// add router for Head method
func (app *App) Head(rootpath string, f FilterFunc) *App {
app.Handlers.Head(rootpath, f)
return app
}
// add router for Patch method
func (app *App) Patch(rootpath string, f FilterFunc) *App {
app.Handlers.Patch(rootpath, f)
return app
}
// add router for Patch method
func (app *App) Any(rootpath string, f FilterFunc) *App {
app.Handlers.Any(rootpath, f)
return app
}
// add router for http.Handler
func (app *App) Handler(rootpath string, h http.Handler, options ...interface{}) *App {
app.Handlers.Handler(rootpath, h, options...)
return app
}
// UrlFor creates a url with another registered controller handler with params.
// The endpoint is formed as path.controller.name to defined the controller method which will run.
// The values need key-pair data to assign into controller method.
func (app *App) UrlFor(endpoint string, values ...string) string {
return app.Handlers.UrlFor(endpoint, values...)
}
// [Deprecated] use InsertFilter.
// Filter adds a FilterFunc under pattern condition and named action.
// The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
app.Handlers.AddFilter(pattern, action, filter)
return app
}
// InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including
// beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App {
app.Handlers.InsertFilter(pattern, pos, filter)
return app
}
// SetViewsPath sets view directory path in beego application.
// it returns beego application self.
func (app *App) SetViewsPath(path string) *App {
ViewsPath = path
return app
}
// SetStaticPath sets static directory path and proper url pattern in beego application.
// if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
// it returns beego application self.
func (app *App) SetStaticPath(url string, path string) *App {
StaticDir[url] = path
return app
}
// DelStaticPath removes the static folder setting in this url pattern in beego application.
// it returns beego application self.
func (app *App) DelStaticPath(url string) *App {
delete(StaticDir, url)
return app
}
...@@ -80,11 +80,11 @@ func (gr *GroupRouters) AddAuto(c ControllerInterface) { ...@@ -80,11 +80,11 @@ func (gr *GroupRouters) AddAuto(c ControllerInterface) {
func AddGroupRouter(prefix string, groups GroupRouters) *App { func AddGroupRouter(prefix string, groups GroupRouters) *App {
for _, v := range groups { for _, v := range groups {
if v.pattern == "" { if v.pattern == "" {
BeeApp.AutoRouterWithPrefix(prefix, v.controller) BeeApp.Handlers.AddAutoPrefix(prefix, v.controller)
} else if v.mappingMethods != "" { } else if v.mappingMethods != "" {
BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods) BeeApp.Handlers.Add(prefix+v.pattern, v.controller, v.mappingMethods)
} else { } else {
BeeApp.Router(prefix+v.pattern, v.controller) BeeApp.Handlers.Add(prefix+v.pattern, v.controller)
} }
} }
...@@ -93,8 +93,22 @@ func AddGroupRouter(prefix string, groups GroupRouters) *App { ...@@ -93,8 +93,22 @@ func AddGroupRouter(prefix string, groups GroupRouters) *App {
// Router adds a patterned controller handler to BeeApp. // Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router. // it's an alias method of App.Router.
// usage:
// simple router
// beego.Router("/admin", &admin.UserController{})
// beego.Router("/admin/index", &admin.ArticleController{})
//
// regex router
//
// beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
//
// custom rules
// beego.Router("/api/list",&RestController{},"*:ListFood")
// beego.Router("/api/create",&RestController{},"post:CreateFood")
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App { func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Router(rootpath, c, mappingMethods...) BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
return BeeApp return BeeApp
} }
...@@ -109,69 +123,73 @@ func RESTRouter(rootpath string, c ControllerInterface) *App { ...@@ -109,69 +123,73 @@ func RESTRouter(rootpath string, c ControllerInterface) *App {
// AutoRouter adds defined controller handler to BeeApp. // AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter. // it's same to App.AutoRouter.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function.
func AutoRouter(c ControllerInterface) *App { func AutoRouter(c ControllerInterface) *App {
BeeApp.AutoRouter(c) BeeApp.Handlers.AddAuto(c)
return BeeApp return BeeApp
} }
// AutoPrefix adds controller handler to BeeApp with prefix. // AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix. // it's same to App.AutoRouterWithPrefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func AutoPrefix(prefix string, c ControllerInterface) *App { func AutoPrefix(prefix string, c ControllerInterface) *App {
BeeApp.AutoRouterWithPrefix(prefix, c) BeeApp.Handlers.AddAutoPrefix(prefix, c)
return BeeApp return BeeApp
} }
// register router for Get method // register router for Get method
func Get(rootpath string, f FilterFunc) *App { func Get(rootpath string, f FilterFunc) *App {
BeeApp.Get(rootpath, f) BeeApp.Handlers.Get(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Post method // register router for Post method
func Post(rootpath string, f FilterFunc) *App { func Post(rootpath string, f FilterFunc) *App {
BeeApp.Post(rootpath, f) BeeApp.Handlers.Post(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Delete method // register router for Delete method
func Delete(rootpath string, f FilterFunc) *App { func Delete(rootpath string, f FilterFunc) *App {
BeeApp.Delete(rootpath, f) BeeApp.Handlers.Delete(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Put method // register router for Put method
func Put(rootpath string, f FilterFunc) *App { func Put(rootpath string, f FilterFunc) *App {
BeeApp.Put(rootpath, f) BeeApp.Handlers.Put(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Head method // register router for Head method
func Head(rootpath string, f FilterFunc) *App { func Head(rootpath string, f FilterFunc) *App {
BeeApp.Head(rootpath, f) BeeApp.Handlers.Head(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Options method // register router for Options method
func Options(rootpath string, f FilterFunc) *App { func Options(rootpath string, f FilterFunc) *App {
BeeApp.Options(rootpath, f) BeeApp.Handlers.Options(rootpath, f)
return BeeApp return BeeApp
} }
// register router for Patch method // register router for Patch method
func Patch(rootpath string, f FilterFunc) *App { func Patch(rootpath string, f FilterFunc) *App {
BeeApp.Patch(rootpath, f) BeeApp.Handlers.Patch(rootpath, f)
return BeeApp return BeeApp
} }
// register router for all method // register router for all method
func Any(rootpath string, f FilterFunc) *App { func Any(rootpath string, f FilterFunc) *App {
BeeApp.Any(rootpath, f) BeeApp.Handlers.Any(rootpath, f)
return BeeApp return BeeApp
} }
// register router for own Handler // register router for own Handler
func Handler(rootpath string, h http.Handler, options ...interface{}) *App { func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
BeeApp.Handler(rootpath, h, options...) BeeApp.Handlers.Handler(rootpath, h, options...)
return BeeApp return BeeApp
} }
...@@ -184,15 +202,14 @@ func Errorhandler(err string, h http.HandlerFunc) *App { ...@@ -184,15 +202,14 @@ func Errorhandler(err string, h http.HandlerFunc) *App {
return BeeApp return BeeApp
} }
// SetViewsPath sets view directory to BeeApp. // SetViewsPath sets view directory path in beego application.
// it's alias of App.SetViewsPath.
func SetViewsPath(path string) *App { func SetViewsPath(path string) *App {
BeeApp.SetViewsPath(path) ViewsPath = path
return BeeApp return BeeApp
} }
// SetStaticPath sets static directory and url prefix to BeeApp. // SetStaticPath sets static directory path and proper url pattern in beego application.
// it's alias of App.SetStaticPath. // if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
func SetStaticPath(url string, path string) *App { func SetStaticPath(url string, path string) *App {
if !strings.HasPrefix(url, "/") { if !strings.HasPrefix(url, "/") {
url = "/" + url url = "/" + url
...@@ -203,7 +220,6 @@ func SetStaticPath(url string, path string) *App { ...@@ -203,7 +220,6 @@ func SetStaticPath(url string, path string) *App {
} }
// DelStaticPath removes the static folder setting in this url pattern in beego application. // DelStaticPath removes the static folder setting in this url pattern in beego application.
// it's alias of App.DelStaticPath.
func DelStaticPath(url string) *App { func DelStaticPath(url string) *App {
delete(StaticDir, url) delete(StaticDir, url)
return BeeApp return BeeApp
...@@ -212,18 +228,16 @@ func DelStaticPath(url string) *App { ...@@ -212,18 +228,16 @@ func DelStaticPath(url string) *App {
// [Deprecated] use InsertFilter. // [Deprecated] use InsertFilter.
// Filter adds a FilterFunc under pattern condition and named action. // Filter adds a FilterFunc under pattern condition and named action.
// The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter. // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
// it's alias of App.Filter.
func AddFilter(pattern, action string, filter FilterFunc) *App { func AddFilter(pattern, action string, filter FilterFunc) *App {
BeeApp.Filter(pattern, action, filter) BeeApp.Handlers.AddFilter(pattern, action, filter)
return BeeApp return BeeApp
} }
// InsertFilter adds a FilterFunc with pattern condition and action constant. // InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including // The pos means action constant including
// beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter. // beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
// it's alias of App.InsertFilter.
func InsertFilter(pattern string, pos int, filter FilterFunc) *App { func InsertFilter(pattern string, pos int, filter FilterFunc) *App {
BeeApp.InsertFilter(pattern, pos, filter) BeeApp.Handlers.InsertFilter(pattern, pos, filter)
return BeeApp return BeeApp
} }
......
...@@ -202,7 +202,7 @@ func Htmlunquote(src string) string { ...@@ -202,7 +202,7 @@ func Htmlunquote(src string) string {
// //
// more detail http://beego.me/docs/mvc/controller/urlbuilding.md // more detail http://beego.me/docs/mvc/controller/urlbuilding.md
func UrlFor(endpoint string, values ...string) string { func UrlFor(endpoint string, values ...string) string {
return BeeApp.UrlFor(endpoint, values...) return BeeApp.Handlers.UrlFor(endpoint, values...)
} }
// returns script tag with src string. // returns script tag with src string.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment