Commit f8422653 by bergquist Committed by Carl Bergquist

add custom route register

parent bf138d18
...@@ -6,18 +6,26 @@ import ( ...@@ -6,18 +6,26 @@ import (
macaron "gopkg.in/macaron.v1" macaron "gopkg.in/macaron.v1"
) )
type Router interface {
Route(pattern, method string, handlers ...macaron.Handler)
}
type RouteRegister interface { type RouteRegister interface {
Get(string, ...macaron.Handler) Get(string, ...macaron.Handler)
Post(string, ...macaron.Handler) Post(string, ...macaron.Handler)
Delete(string, ...macaron.Handler) Delete(string, ...macaron.Handler)
Put(string, ...macaron.Handler) Put(string, ...macaron.Handler)
Group(string, func(RouteRegister), ...macaron.Handler) Group(string, func(RouteRegister), ...macaron.Handler)
Register(Router)
} }
func newRouteRegister(rr *macaron.Router) RouteRegister { func newRouteRegister() RouteRegister {
return &routeRegister{ return &routeRegister{
prefix: "", prefix: "",
routes: []route{}, routes: []route{},
subfixHandlers: []macaron.Handler{},
} }
} }
...@@ -31,56 +39,50 @@ type routeRegister struct { ...@@ -31,56 +39,50 @@ type routeRegister struct {
prefix string prefix string
subfixHandlers []macaron.Handler subfixHandlers []macaron.Handler
routes []route routes []route
groups []*routeRegister
} }
func (rr *routeRegister) Group(pattern string, fn func(rr RouteRegister), handlers ...macaron.Handler) { func (rr *routeRegister) Group(pattern string, fn func(rr RouteRegister), handlers ...macaron.Handler) {
group := &routeRegister{ group := &routeRegister{
prefix: rr.prefix + pattern, prefix: rr.prefix + pattern,
subfixHandlers: handlers, subfixHandlers: append(rr.subfixHandlers, handlers...),
routes: rr.routes, routes: []route{},
} }
fn(group) fn(group)
rr.groups = append(rr.groups, group)
} }
func (rr *routeRegister) Get(pattern string, handlers ...macaron.Handler) { func (rr *routeRegister) Register(router Router) {
rr.routes = append(rr.routes, route{ for _, r := range rr.routes {
method: http.MethodGet, router.Route(r.pattern, r.method, r.handlers...)
pattern: rr.prefix + pattern, }
handlers: handlers,
}) for _, g := range rr.groups {
println("get: get ", len(rr.routes)) g.Register(router)
rr.routes = rr.routes[:len(rr.routes)-1] }
} }
func (rr *routeRegister) Post(pattern string, handlers ...macaron.Handler) { func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{ rr.routes = append(rr.routes, route{
method: http.MethodPost, method: method,
pattern: rr.prefix + pattern, pattern: rr.prefix + pattern,
handlers: handlers, handlers: append(rr.subfixHandlers, handlers...),
}) })
println("get: post ", len(rr.routes)) }
rr.routes = rr.routes[:len(rr.routes)-1] func (rr *routeRegister) Get(pattern string, handlers ...macaron.Handler) {
rr.route(pattern, http.MethodGet, handlers...)
} }
func (rr *routeRegister) Delete(pattern string, handlers ...macaron.Handler) { func (rr *routeRegister) Post(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{ rr.route(pattern, http.MethodPost, handlers...)
method: http.MethodDelete, }
pattern: rr.prefix + pattern,
handlers: handlers,
})
println("get: delete ", len(rr.routes))
rr.routes = rr.routes[:len(rr.routes)-1] func (rr *routeRegister) Delete(pattern string, handlers ...macaron.Handler) {
rr.route(pattern, http.MethodDelete, handlers...)
} }
func (rr *routeRegister) Put(pattern string, handlers ...macaron.Handler) { func (rr *routeRegister) Put(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{ rr.route(pattern, http.MethodPut, handlers...)
method: http.MethodPut,
pattern: rr.prefix + pattern,
handlers: handlers,
})
rr.routes = rr.routes[:len(rr.routes)-1]
} }
package api package api
import "testing" import (
"strconv"
"testing"
func TestRouteRegister(t *testing.T) { macaron "gopkg.in/macaron.v1"
)
rr := &routeRegister{
prefix: "",
routes: []route{},
}
rr.Delete("/admin") type fakeRouter struct {
rr.Get("/down") route []route
}
rr.Group("/user", func(innerRR RouteRegister) { func (fr *fakeRouter) Route(pattern, method string, handlers ...macaron.Handler) {
innerRR.Delete("") fr.route = append(fr.route, route{
innerRR.Get("/friends") pattern: pattern,
method: method,
handlers: handlers,
}) })
}
println("len", len(rr.routes)) func emptyHandlers(n int) []macaron.Handler {
res := []macaron.Handler{}
if rr.routes[0].pattern != "/admin" && rr.routes[0].method != "DELETE" { for i := 1; n >= i; i++ {
t.Errorf("expected first route to be DELETE /admin") res = append(res, emptyHandler(strconv.Itoa(i)))
} }
return res
}
func emptyHandler(name string) macaron.Handler {
return struct{ name string }{name: name}
}
if rr.routes[1].pattern != "/down" && rr.routes[1].method != "GET" { func TestRouteRegister(t *testing.T) {
t.Errorf("expected first route to be GET /down") testTable := []route{
{method: "DELETE", pattern: "/admin", handlers: emptyHandlers(1)},
{method: "GET", pattern: "/down", handlers: emptyHandlers(2)},
{method: "POST", pattern: "/user", handlers: emptyHandlers(1)},
{method: "PUT", pattern: "/user/friends", handlers: emptyHandlers(1)},
{method: "DELETE", pattern: "/user/admin", handlers: emptyHandlers(2)},
{method: "GET", pattern: "/user/admin/all", handlers: emptyHandlers(4)},
} }
println("len", len(rr.routes)) // Setup
rr := newRouteRegister()
rr.Delete("/admin", emptyHandler("1"))
rr.Get("/down", emptyHandler("1"), emptyHandler("2"))
rr.Group("/user", func(user RouteRegister) {
user.Post("", emptyHandler("1"))
user.Put("/friends", emptyHandler("2"))
if rr.routes[2].pattern != "/user" && rr.routes[2].method != "DELETE" { user.Group("/admin", func(admin RouteRegister) {
t.Errorf("expected first route to be DELETE /admin") admin.Delete("", emptyHandler("3"))
admin.Get("/all", emptyHandler("3"), emptyHandler("4"), emptyHandler("5"))
}, emptyHandler("3"))
})
fr := &fakeRouter{}
rr.Register(fr)
// Validation
if len(fr.route) != len(testTable) {
t.Errorf("want %v routes, got %v", len(testTable), len(fr.route))
} }
if rr.routes[3].pattern != "/user/friends" && rr.routes[3].method != "GET" { for i, _ := range testTable {
t.Errorf("expected first route to be GET /down") if testTable[i].method != fr.route[i].method {
t.Errorf("want %s got %v", testTable[i].method, fr.route[i].method)
}
if testTable[i].pattern != fr.route[i].pattern {
t.Errorf("want %s got %v", testTable[i].pattern, fr.route[i].pattern)
}
if len(testTable[i].handlers) != len(fr.route[i].handlers) {
t.Errorf("want %d handlers got %d handlers \ntestcase: %v\nroute: %v\n",
len(testTable[i].handlers),
len(fr.route[i].handlers),
testTable[i],
fr.route[i])
}
} }
} }
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