Commit 55ad951b authored by astaxie's avatar 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)
}
parent ef815bf5
......@@ -136,6 +136,60 @@ func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App
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) *App {
app.Handlers.Handler(rootpath, h)
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.
......
......@@ -121,6 +121,60 @@ func AutoPrefix(prefix string, c ControllerInterface) *App {
return BeeApp
}
// register router for Get method
func Get(rootpath string, f FilterFunc) *App {
BeeApp.Get(rootpath, f)
return BeeApp
}
// register router for Post method
func Post(rootpath string, f FilterFunc) *App {
BeeApp.Post(rootpath, f)
return BeeApp
}
// register router for Delete method
func Delete(rootpath string, f FilterFunc) *App {
BeeApp.Delete(rootpath, f)
return BeeApp
}
// register router for Put method
func Put(rootpath string, f FilterFunc) *App {
BeeApp.Put(rootpath, f)
return BeeApp
}
// register router for Head method
func Head(rootpath string, f FilterFunc) *App {
BeeApp.Head(rootpath, f)
return BeeApp
}
// register router for Options method
func Options(rootpath string, f FilterFunc) *App {
BeeApp.Options(rootpath, f)
return BeeApp
}
// register router for Patch method
func Patch(rootpath string, f FilterFunc) *App {
BeeApp.Patch(rootpath, f)
return BeeApp
}
// register router for all method
func Any(rootpath string, f FilterFunc) *App {
BeeApp.Any(rootpath, f)
return BeeApp
}
// register router for own Handler
func Handler(rootpath string, h http.Handler) *App {
BeeApp.Handler(rootpath, h)
return BeeApp
}
// ErrorHandler registers http.HandlerFunc to each http err code string.
// usage:
// beego.ErrorHandler("404",NotFound)
......
This diff is collapsed.
......@@ -10,6 +10,8 @@ import (
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/beego/context"
)
type TestController struct {
......@@ -232,3 +234,47 @@ func TestAutoPrefix(t *testing.T) {
t.Errorf("TestAutoPrefix can't run")
}
}
func TestRouterGet(t *testing.T) {
r, _ := http.NewRequest("GET", "/user", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
handler.ServeHTTP(w, r)
if w.Body.String() != "Get userlist" {
t.Errorf("TestRouterGet can't run")
}
}
func TestRouterPost(t *testing.T) {
r, _ := http.NewRequest("POST", "/user/123", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.Post("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte(ctx.Input.Param(":id")))
})
handler.ServeHTTP(w, r)
if w.Body.String() != "123" {
t.Errorf("TestRouterPost can't run")
}
}
func sayhello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("sayhello"))
}
func TestRouterHandler(t *testing.T) {
r, _ := http.NewRequest("POST", "/sayhi", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.Handler("/sayhi", http.HandlerFunc(sayhello))
handler.ServeHTTP(w, r)
if w.Body.String() != "sayhello" {
t.Errorf("TestRouterHandler can't run")
}
}
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