beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
Showing
4 changed files
with
154 additions
and
0 deletions
| ... | @@ -136,6 +136,60 @@ func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App | ... | @@ -136,6 +136,60 @@ func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App |
| 136 | return app | 136 | return app |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | // add router for Get method | ||
| 140 | func (app *App) Get(rootpath string, f FilterFunc) *App { | ||
| 141 | app.Handlers.Get(rootpath, f) | ||
| 142 | return app | ||
| 143 | } | ||
| 144 | |||
| 145 | // add router for Post method | ||
| 146 | func (app *App) Post(rootpath string, f FilterFunc) *App { | ||
| 147 | app.Handlers.Post(rootpath, f) | ||
| 148 | return app | ||
| 149 | } | ||
| 150 | |||
| 151 | // add router for Put method | ||
| 152 | func (app *App) Put(rootpath string, f FilterFunc) *App { | ||
| 153 | app.Handlers.Put(rootpath, f) | ||
| 154 | return app | ||
| 155 | } | ||
| 156 | |||
| 157 | // add router for Delete method | ||
| 158 | func (app *App) Delete(rootpath string, f FilterFunc) *App { | ||
| 159 | app.Handlers.Delete(rootpath, f) | ||
| 160 | return app | ||
| 161 | } | ||
| 162 | |||
| 163 | // add router for Options method | ||
| 164 | func (app *App) Options(rootpath string, f FilterFunc) *App { | ||
| 165 | app.Handlers.Options(rootpath, f) | ||
| 166 | return app | ||
| 167 | } | ||
| 168 | |||
| 169 | // add router for Head method | ||
| 170 | func (app *App) Head(rootpath string, f FilterFunc) *App { | ||
| 171 | app.Handlers.Head(rootpath, f) | ||
| 172 | return app | ||
| 173 | } | ||
| 174 | |||
| 175 | // add router for Patch method | ||
| 176 | func (app *App) Patch(rootpath string, f FilterFunc) *App { | ||
| 177 | app.Handlers.Patch(rootpath, f) | ||
| 178 | return app | ||
| 179 | } | ||
| 180 | |||
| 181 | // add router for Patch method | ||
| 182 | func (app *App) Any(rootpath string, f FilterFunc) *App { | ||
| 183 | app.Handlers.Any(rootpath, f) | ||
| 184 | return app | ||
| 185 | } | ||
| 186 | |||
| 187 | // add router for http.Handler | ||
| 188 | func (app *App) Handler(rootpath string, h http.Handler) *App { | ||
| 189 | app.Handlers.Handler(rootpath, h) | ||
| 190 | return app | ||
| 191 | } | ||
| 192 | |||
| 139 | // UrlFor creates a url with another registered controller handler with params. | 193 | // UrlFor creates a url with another registered controller handler with params. |
| 140 | // The endpoint is formed as path.controller.name to defined the controller method which will run. | 194 | // The endpoint is formed as path.controller.name to defined the controller method which will run. |
| 141 | // The values need key-pair data to assign into controller method. | 195 | // The values need key-pair data to assign into controller method. | ... | ... |
| ... | @@ -121,6 +121,60 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { | ... | @@ -121,6 +121,60 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { |
| 121 | return BeeApp | 121 | return BeeApp |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | // register router for Get method | ||
| 125 | func Get(rootpath string, f FilterFunc) *App { | ||
| 126 | BeeApp.Get(rootpath, f) | ||
| 127 | return BeeApp | ||
| 128 | } | ||
| 129 | |||
| 130 | // register router for Post method | ||
| 131 | func Post(rootpath string, f FilterFunc) *App { | ||
| 132 | BeeApp.Post(rootpath, f) | ||
| 133 | return BeeApp | ||
| 134 | } | ||
| 135 | |||
| 136 | // register router for Delete method | ||
| 137 | func Delete(rootpath string, f FilterFunc) *App { | ||
| 138 | BeeApp.Delete(rootpath, f) | ||
| 139 | return BeeApp | ||
| 140 | } | ||
| 141 | |||
| 142 | // register router for Put method | ||
| 143 | func Put(rootpath string, f FilterFunc) *App { | ||
| 144 | BeeApp.Put(rootpath, f) | ||
| 145 | return BeeApp | ||
| 146 | } | ||
| 147 | |||
| 148 | // register router for Head method | ||
| 149 | func Head(rootpath string, f FilterFunc) *App { | ||
| 150 | BeeApp.Head(rootpath, f) | ||
| 151 | return BeeApp | ||
| 152 | } | ||
| 153 | |||
| 154 | // register router for Options method | ||
| 155 | func Options(rootpath string, f FilterFunc) *App { | ||
| 156 | BeeApp.Options(rootpath, f) | ||
| 157 | return BeeApp | ||
| 158 | } | ||
| 159 | |||
| 160 | // register router for Patch method | ||
| 161 | func Patch(rootpath string, f FilterFunc) *App { | ||
| 162 | BeeApp.Patch(rootpath, f) | ||
| 163 | return BeeApp | ||
| 164 | } | ||
| 165 | |||
| 166 | // register router for all method | ||
| 167 | func Any(rootpath string, f FilterFunc) *App { | ||
| 168 | BeeApp.Any(rootpath, f) | ||
| 169 | return BeeApp | ||
| 170 | } | ||
| 171 | |||
| 172 | // register router for own Handler | ||
| 173 | func Handler(rootpath string, h http.Handler) *App { | ||
| 174 | BeeApp.Handler(rootpath, h) | ||
| 175 | return BeeApp | ||
| 176 | } | ||
| 177 | |||
| 124 | // ErrorHandler registers http.HandlerFunc to each http err code string. | 178 | // ErrorHandler registers http.HandlerFunc to each http err code string. |
| 125 | // usage: | 179 | // usage: |
| 126 | // beego.ErrorHandler("404",NotFound) | 180 | // beego.ErrorHandler("404",NotFound) | ... | ... |
This diff is collapsed.
Click to expand it.
| ... | @@ -10,6 +10,8 @@ import ( | ... | @@ -10,6 +10,8 @@ import ( |
| 10 | "net/http" | 10 | "net/http" |
| 11 | "net/http/httptest" | 11 | "net/http/httptest" |
| 12 | "testing" | 12 | "testing" |
| 13 | |||
| 14 | "github.com/astaxie/beego/context" | ||
| 13 | ) | 15 | ) |
| 14 | 16 | ||
| 15 | type TestController struct { | 17 | type TestController struct { |
| ... | @@ -232,3 +234,47 @@ func TestAutoPrefix(t *testing.T) { | ... | @@ -232,3 +234,47 @@ func TestAutoPrefix(t *testing.T) { |
| 232 | t.Errorf("TestAutoPrefix can't run") | 234 | t.Errorf("TestAutoPrefix can't run") |
| 233 | } | 235 | } |
| 234 | } | 236 | } |
| 237 | |||
| 238 | func TestRouterGet(t *testing.T) { | ||
| 239 | r, _ := http.NewRequest("GET", "/user", nil) | ||
| 240 | w := httptest.NewRecorder() | ||
| 241 | |||
| 242 | handler := NewControllerRegistor() | ||
| 243 | handler.Get("/user", func(ctx *context.Context) { | ||
| 244 | ctx.Output.Body([]byte("Get userlist")) | ||
| 245 | }) | ||
| 246 | handler.ServeHTTP(w, r) | ||
| 247 | if w.Body.String() != "Get userlist" { | ||
| 248 | t.Errorf("TestRouterGet can't run") | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | func TestRouterPost(t *testing.T) { | ||
| 253 | r, _ := http.NewRequest("POST", "/user/123", nil) | ||
| 254 | w := httptest.NewRecorder() | ||
| 255 | |||
| 256 | handler := NewControllerRegistor() | ||
| 257 | handler.Post("/user/:id", func(ctx *context.Context) { | ||
| 258 | ctx.Output.Body([]byte(ctx.Input.Param(":id"))) | ||
| 259 | }) | ||
| 260 | handler.ServeHTTP(w, r) | ||
| 261 | if w.Body.String() != "123" { | ||
| 262 | t.Errorf("TestRouterPost can't run") | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | func sayhello(w http.ResponseWriter, r *http.Request) { | ||
| 267 | w.Write([]byte("sayhello")) | ||
| 268 | } | ||
| 269 | |||
| 270 | func TestRouterHandler(t *testing.T) { | ||
| 271 | r, _ := http.NewRequest("POST", "/sayhi", nil) | ||
| 272 | w := httptest.NewRecorder() | ||
| 273 | |||
| 274 | handler := NewControllerRegistor() | ||
| 275 | handler.Handler("/sayhi", http.HandlerFunc(sayhello)) | ||
| 276 | handler.ServeHTTP(w, r) | ||
| 277 | if w.Body.String() != "sayhello" { | ||
| 278 | t.Errorf("TestRouterHandler can't run") | ||
| 279 | } | ||
| 280 | } | ... | ... |
-
Please register or sign in to post a comment