diff --git a/app.go b/app.go index e6ca4bc7..bcfa0564 100644 --- a/app.go +++ b/app.go @@ -22,12 +22,13 @@ type FilterFunc func(*context.Context) // App defines beego application with a new PatternServeMux. type App struct { Handlers *ControllerRegistor + Server *http.Server } // NewApp returns a new beego application. func NewApp() *App { cr := NewControllerRegistor() - app := &App{Handlers: cr} + app := &App{Handlers: cr, Server: &http.Server{}} return app } @@ -58,18 +59,17 @@ func (app *App) Run() { } err = fcgi.Serve(l, app.Handlers) } else { - s := &http.Server{ - Addr: addr, - Handler: app.Handlers, - ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second, - WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second, - } + app.Server.Addr = addr + app.Server.Handler = app.Handlers + app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second + app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second + if EnableHttpTLS { go func() { 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 { BeeLogger.Critical("ListenAndServeTLS: ", err) time.Sleep(100 * time.Microsecond) @@ -80,7 +80,7 @@ func (app *App) Run() { if EnableHttpListen { go func() { - err := s.ListenAndServe() + err := app.Server.ListenAndServe() if err != nil { BeeLogger.Critical("ListenAndServe: ", err) time.Sleep(100 * time.Microsecond) @@ -92,141 +92,3 @@ func (app *App) Run() { <-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 -} diff --git a/beego.go b/beego.go index f8916231..60f87c21 100644 --- a/beego.go +++ b/beego.go @@ -80,11 +80,11 @@ func (gr *GroupRouters) AddAuto(c ControllerInterface) { func AddGroupRouter(prefix string, groups GroupRouters) *App { for _, v := range groups { if v.pattern == "" { - BeeApp.AutoRouterWithPrefix(prefix, v.controller) + BeeApp.Handlers.AddAutoPrefix(prefix, v.controller) } else if v.mappingMethods != "" { - BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods) + BeeApp.Handlers.Add(prefix+v.pattern, v.controller, v.mappingMethods) } 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 { // Router adds a patterned controller handler to BeeApp. // 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 { - BeeApp.Router(rootpath, c, mappingMethods...) + BeeApp.Handlers.Add(rootpath, c, mappingMethods...) return BeeApp } @@ -109,69 +123,73 @@ func RESTRouter(rootpath string, c ControllerInterface) *App { // AutoRouter adds defined controller handler to BeeApp. // 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 { - BeeApp.AutoRouter(c) + BeeApp.Handlers.AddAuto(c) return BeeApp } // AutoPrefix adds controller handler to BeeApp with prefix. // 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 { - BeeApp.AutoRouterWithPrefix(prefix, c) + BeeApp.Handlers.AddAutoPrefix(prefix, c) return BeeApp } // register router for Get method func Get(rootpath string, f FilterFunc) *App { - BeeApp.Get(rootpath, f) + BeeApp.Handlers.Get(rootpath, f) return BeeApp } // register router for Post method func Post(rootpath string, f FilterFunc) *App { - BeeApp.Post(rootpath, f) + BeeApp.Handlers.Post(rootpath, f) return BeeApp } // register router for Delete method func Delete(rootpath string, f FilterFunc) *App { - BeeApp.Delete(rootpath, f) + BeeApp.Handlers.Delete(rootpath, f) return BeeApp } // register router for Put method func Put(rootpath string, f FilterFunc) *App { - BeeApp.Put(rootpath, f) + BeeApp.Handlers.Put(rootpath, f) return BeeApp } // register router for Head method func Head(rootpath string, f FilterFunc) *App { - BeeApp.Head(rootpath, f) + BeeApp.Handlers.Head(rootpath, f) return BeeApp } // register router for Options method func Options(rootpath string, f FilterFunc) *App { - BeeApp.Options(rootpath, f) + BeeApp.Handlers.Options(rootpath, f) return BeeApp } // register router for Patch method func Patch(rootpath string, f FilterFunc) *App { - BeeApp.Patch(rootpath, f) + BeeApp.Handlers.Patch(rootpath, f) return BeeApp } // register router for all method func Any(rootpath string, f FilterFunc) *App { - BeeApp.Any(rootpath, f) + BeeApp.Handlers.Any(rootpath, f) return BeeApp } // register router for own Handler func Handler(rootpath string, h http.Handler, options ...interface{}) *App { - BeeApp.Handler(rootpath, h, options...) + BeeApp.Handlers.Handler(rootpath, h, options...) return BeeApp } @@ -184,15 +202,14 @@ func Errorhandler(err string, h http.HandlerFunc) *App { return BeeApp } -// SetViewsPath sets view directory to BeeApp. -// it's alias of App.SetViewsPath. +// SetViewsPath sets view directory path in beego application. func SetViewsPath(path string) *App { - BeeApp.SetViewsPath(path) + ViewsPath = path return BeeApp } -// SetStaticPath sets static directory and url prefix to BeeApp. -// it's alias of App.SetStaticPath. +// 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". func SetStaticPath(url string, path string) *App { if !strings.HasPrefix(url, "/") { url = "/" + url @@ -203,7 +220,6 @@ func SetStaticPath(url string, path string) *App { } // DelStaticPath removes the static folder setting in this url pattern in beego application. -// it's alias of App.DelStaticPath. func DelStaticPath(url string) *App { delete(StaticDir, url) return BeeApp @@ -212,18 +228,16 @@ func DelStaticPath(url string) *App { // [Deprecated] use InsertFilter. // Filter adds a FilterFunc under pattern condition and named action. // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter. -// it's alias of App.Filter. func AddFilter(pattern, action string, filter FilterFunc) *App { - BeeApp.Filter(pattern, action, filter) + BeeApp.Handlers.AddFilter(pattern, action, filter) return BeeApp } // 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. -// it's alias of App.InsertFilter. func InsertFilter(pattern string, pos int, filter FilterFunc) *App { - BeeApp.InsertFilter(pattern, pos, filter) + BeeApp.Handlers.InsertFilter(pattern, pos, filter) return BeeApp } diff --git a/templatefunc.go b/templatefunc.go index 62584781..3611adbc 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -202,7 +202,7 @@ func Htmlunquote(src string) string { // // more detail http://beego.me/docs/mvc/controller/urlbuilding.md func UrlFor(endpoint string, values ...string) string { - return BeeApp.UrlFor(endpoint, values...) + return BeeApp.Handlers.UrlFor(endpoint, values...) } // returns script tag with src string.