Skip to content

Commit

Permalink
Added groups
Browse files Browse the repository at this point in the history
  • Loading branch information
José Miguel Molina Arboledas committed Mar 17, 2014
1 parent 7fbb8b2 commit 920dde7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
25 changes: 24 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Params map[string]string
type Router interface {
Routes

// Group adds a group where related routes can be added.
Group(string, func(Router), ...Handler)
// Get adds a route for a HTTP GET request to the specified matching pattern.
Get(string, ...Handler) Route
// Patch adds a route for a HTTP PATCH request to the specified matching pattern.
Expand Down Expand Up @@ -42,6 +44,12 @@ type Router interface {
type router struct {
routes []*route
notFounds []Handler
groups []group
}

type group struct {
pattern string
handlers []Handler
}

// NewRouter creates a new Router instance.
Expand All @@ -54,7 +62,13 @@ type router struct {
//
// If you are using ClassicMartini, then this is done for you.
func NewRouter() Router {
return &router{notFounds: []Handler{http.NotFound}}
return &router{notFounds: []Handler{http.NotFound}, groups: make([]group, 0)}
}

func (r *router) Group(pattern string, fn func(Router), h ...Handler) {
r.groups = append(r.groups, group{pattern, h})
fn(r)
r.groups = r.groups[:len(r.groups)-1]
}

func (r *router) Get(pattern string, h ...Handler) Route {
Expand Down Expand Up @@ -111,6 +125,15 @@ func (r *router) NotFound(handler ...Handler) {
}

func (r *router) addRoute(method string, pattern string, handlers []Handler) *route {
if len(r.groups) > 0 {
group := r.groups[len(r.groups)-1]
pattern = group.pattern + pattern
h := make([]Handler, len(group.handlers)+len(handlers))
copy(h, group.handlers)
copy(h[len(group.handlers):], handlers)
handlers = h
}

route := newRoute(method, pattern, handlers)
route.Validate()
r.routes = append(r.routes, route)
Expand Down
23 changes: 22 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func Test_Routing(t *testing.T) {
req10, _ := http.NewRequest("HEAD", "http://localhost:3000/foo", nil)
context10 := New().createContext(recorder, req10)

req11, _ := http.NewRequest("GET", "http://localhost:3000/bazz/inga", nil)
context11 := New().createContext(recorder, req11)

req12, _ := http.NewRequest("POST", "http://localhost:3000/bazz/inga", nil)
context12 := New().createContext(recorder, req12)

result := ""
router.Get("/foo", func(req *http.Request) {
result += "foo"
Expand Down Expand Up @@ -80,6 +86,19 @@ func Test_Routing(t *testing.T) {
expect(t, params["_1"], "")
result += "wappow"
})
router.Group("/bazz", func(r Router) {
r.Get("/inga", func() {
result += "get"
})

r.Post("/inga", func() {
result += "post"
})
}, func() {
result += "bazz"
}, func() {
result += "inga"
})

router.Handle(recorder, req, context)
router.Handle(recorder, req2, context2)
Expand All @@ -91,7 +110,9 @@ func Test_Routing(t *testing.T) {
router.Handle(recorder, req8, context8)
router.Handle(recorder, req9, context9)
router.Handle(recorder, req10, context10)
expect(t, result, "foobarbatbarfoofezpopbapwappowwappowoptsfoo")
router.Handle(recorder, req11, context11)
router.Handle(recorder, req12, context12)
expect(t, result, "foobarbatbarfoofezpopbapwappowwappowoptsfoobazzingagetbazzingapost")
expect(t, recorder.Code, http.StatusNotFound)
expect(t, recorder.Body.String(), "404 page not found\n")
}
Expand Down

0 comments on commit 920dde7

Please sign in to comment.