adds small docs page metrics

This commit is contained in:
bergquist
2017-09-14 14:26:32 +02:00
committed by Carl Bergquist
parent c177a9a77a
commit bf138d1845
3 changed files with 140 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
package api
import (
"net/http"
macaron "gopkg.in/macaron.v1"
)
type RouteRegister interface {
Get(string, ...macaron.Handler)
Post(string, ...macaron.Handler)
Delete(string, ...macaron.Handler)
Put(string, ...macaron.Handler)
Group(string, func(RouteRegister), ...macaron.Handler)
}
func newRouteRegister(rr *macaron.Router) RouteRegister {
return &routeRegister{
prefix: "",
routes: []route{},
}
}
type route struct {
method string
pattern string
handlers []macaron.Handler
}
type routeRegister struct {
prefix string
subfixHandlers []macaron.Handler
routes []route
}
func (rr *routeRegister) Group(pattern string, fn func(rr RouteRegister), handlers ...macaron.Handler) {
group := &routeRegister{
prefix: rr.prefix + pattern,
subfixHandlers: handlers,
routes: rr.routes,
}
fn(group)
}
func (rr *routeRegister) Get(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{
method: http.MethodGet,
pattern: rr.prefix + pattern,
handlers: handlers,
})
println("get: get ", len(rr.routes))
rr.routes = rr.routes[:len(rr.routes)-1]
}
func (rr *routeRegister) Post(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{
method: http.MethodPost,
pattern: rr.prefix + pattern,
handlers: handlers,
})
println("get: post ", len(rr.routes))
rr.routes = rr.routes[:len(rr.routes)-1]
}
func (rr *routeRegister) Delete(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{
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) Put(pattern string, handlers ...macaron.Handler) {
rr.routes = append(rr.routes, route{
method: http.MethodPut,
pattern: rr.prefix + pattern,
handlers: handlers,
})
rr.routes = rr.routes[:len(rr.routes)-1]
}
+39
View File
@@ -0,0 +1,39 @@
package api
import "testing"
func TestRouteRegister(t *testing.T) {
rr := &routeRegister{
prefix: "",
routes: []route{},
}
rr.Delete("/admin")
rr.Get("/down")
rr.Group("/user", func(innerRR RouteRegister) {
innerRR.Delete("")
innerRR.Get("/friends")
})
println("len", len(rr.routes))
if rr.routes[0].pattern != "/admin" && rr.routes[0].method != "DELETE" {
t.Errorf("expected first route to be DELETE /admin")
}
if rr.routes[1].pattern != "/down" && rr.routes[1].method != "GET" {
t.Errorf("expected first route to be GET /down")
}
println("len", len(rr.routes))
if rr.routes[2].pattern != "/user" && rr.routes[2].method != "DELETE" {
t.Errorf("expected first route to be DELETE /admin")
}
if rr.routes[3].pattern != "/user/friends" && rr.routes[3].method != "GET" {
t.Errorf("expected first route to be GET /down")
}
}