增加了文档和router函数
Showing
2 changed files
with
38 additions
and
0 deletions
| ... | @@ -72,5 +72,28 @@ this will serve any files in /static, including files in subdirectories. For exa | ... | @@ -72,5 +72,28 @@ this will serve any files in /static, including files in subdirectories. For exa |
| 72 | 72 | ||
| 73 | ## Filters / Middleware | 73 | ## Filters / Middleware |
| 74 | ============ | 74 | ============ |
| 75 | You can apply filters to routes, which is useful for enforcing security, redirects, etc. | ||
| 75 | 76 | ||
| 77 | You can, for example, filter all request to enforce some type of security: | ||
| 78 | |||
| 79 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { | ||
| 80 | if r.URL.User == nil || r.URL.User.Username() != "admin" { | ||
| 81 | http.Error(w, "", http.StatusUnauthorized) | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | beego.BeeApp.Filter(FilterUser) | ||
| 86 | |||
| 87 | You can also apply filters only when certain REST URL Parameters exist: | ||
| 88 | |||
| 89 | beego.BeeApp.RegisterController("/:id([0-9]+)", &admin.EditController{}) | ||
| 90 | beego.BeeApp.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { | ||
| 91 | ... | ||
| 92 | }) | ||
| 93 | |||
| 94 | also You can apply filters only when certain prefix URL path exist: | ||
| 95 | |||
| 96 | beego.BeeApp.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { | ||
| 97 | … auth | ||
| 98 | }) | ||
| 76 | 99 | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -93,6 +93,21 @@ func (app *App) RegisterController(path string, c ControllerInterface) *App { | ... | @@ -93,6 +93,21 @@ func (app *App) RegisterController(path string, c ControllerInterface) *App { |
| 93 | return app | 93 | return app |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | func (app *App) Filter(filter http.HandlerFunc) *App { | ||
| 97 | app.Handlers.Filter(filter) | ||
| 98 | return app | ||
| 99 | } | ||
| 100 | |||
| 101 | func (app *App) FilterParam(param string, filter http.HandlerFunc) *App { | ||
| 102 | app.Handlers.FilterParam(param, filter) | ||
| 103 | return app | ||
| 104 | } | ||
| 105 | |||
| 106 | func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App { | ||
| 107 | app.Handlers.FilterParam(path, filter) | ||
| 108 | return app | ||
| 109 | } | ||
| 110 | |||
| 96 | func (app *App) SetViewsPath(path string) *App { | 111 | func (app *App) SetViewsPath(path string) *App { |
| 97 | ViewsPath = path | 112 | ViewsPath = path |
| 98 | return app | 113 | return app | ... | ... |
-
Please register or sign in to post a comment