2629de28 by astaxie

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)
}
1 parent 10d2c7c3
...@@ -132,6 +132,60 @@ func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App ...@@ -132,6 +132,60 @@ func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App
132 return app 132 return app
133 } 133 }
134 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) *App {
185 app.Handlers.Handler(rootpath, h)
186 return app
187 }
188
135 // UrlFor creates a url with another registered controller handler with params. 189 // UrlFor creates a url with another registered controller handler with params.
136 // The endpoint is formed as path.controller.name to defined the controller method which will run. 190 // The endpoint is formed as path.controller.name to defined the controller method which will run.
137 // The values need key-pair data to assign into controller method. 191 // 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)
......
...@@ -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 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!