Refactored path preparation for http event adding

This commit is contained in:
Mitchell 2019-06-12 00:38:03 -07:00
parent 6cbf672635
commit 3c33e95efd
2 changed files with 42 additions and 19 deletions

View File

@ -18,13 +18,15 @@ type Router struct {
prefix string prefix string
} }
// New initializes an empty router. // New initializes an empty router. The prefix parameter may be of any length.
func New(prefix string) Router { func New(prefix string) Router {
if prefix[0] != '/' { if len(prefix) > 0 {
prefix = "/" + prefix if prefix[0] != '/' {
} prefix = "/" + prefix
if prefix[len(prefix)-1] != '/' { }
prefix += "/" if prefix[len(prefix)-1] != '/' {
prefix += "/"
}
} }
return Router{ return Router{
@ -37,40 +39,35 @@ func New(prefix string) Router {
// define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the // define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the
// route. // route.
func (r *Router) Get(path string, handler lambda.Handler) { func (r *Router) Get(path string, handler lambda.Handler) {
path = r.prefix + path r.addEvent(prepPath(http.MethodGet, r.prefix, path), event{h: handler})
r.addEvent(http.MethodGet+path, event{h: handler})
} }
// Post adds a new POST method route to the router. The path parameter is the route path you wish to // Post adds a new POST method route to the router. The path parameter is the route path you wish to
// define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the // define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the
// route. // route.
func (r *Router) Post(path string, handler lambda.Handler) { func (r *Router) Post(path string, handler lambda.Handler) {
path = r.prefix + path r.addEvent(prepPath(http.MethodPost, r.prefix, path), event{h: handler})
r.addEvent(http.MethodPost+path, event{h: handler})
} }
// Put adds a new PUT method route to the router. The path parameter is the route path you wish to // Put adds a new PUT method route to the router. The path parameter is the route path you wish to
// define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the // define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the
// route. // route.
func (r *Router) Put(path string, handler lambda.Handler) { func (r *Router) Put(path string, handler lambda.Handler) {
path = r.prefix + path r.addEvent(prepPath(http.MethodPut, r.prefix, path), event{h: handler})
r.addEvent(http.MethodPut+path, event{h: handler})
} }
// Patch adds a new PATCH method route to the router. The path parameter is the route path you wish // Patch adds a new PATCH method route to the router. The path parameter is the route path you wish
// to define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the // to define. The handler parameter is a lambda.Handler to invoke if an incoming path matches the
// route. // route.
func (r *Router) Patch(path string, handler lambda.Handler) { func (r *Router) Patch(path string, handler lambda.Handler) {
path = r.prefix + path r.addEvent(prepPath(http.MethodPatch, r.prefix, path), event{h: handler})
r.addEvent(http.MethodPatch+path, event{h: handler})
} }
// Delete adds a new DELETE method route to the router. The path parameter is the route path you // Delete adds a new DELETE method route to the router. The path parameter is the route path you
// wish to define. The handler parameter is a lambda.Handler to invoke if an incoming path matches // wish to define. The handler parameter is a lambda.Handler to invoke if an incoming path matches
// the route. // the route.
func (r *Router) Delete(path string, handler lambda.Handler) { func (r *Router) Delete(path string, handler lambda.Handler) {
path = r.prefix + path r.addEvent(prepPath(http.MethodDelete, r.prefix, path), event{h: handler})
r.addEvent(http.MethodDelete+path, event{h: handler})
} }
// Invoke implements the lambda.Handler interface for the Router type. // Invoke implements the lambda.Handler interface for the Router type.
@ -100,10 +97,12 @@ func (r Router) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
return e.h.Invoke(ctx, payload) return e.h.Invoke(ctx, payload)
} }
// Group allows you to define many routes with the same prefix. The prefix parameter will apply the // Group allows you to define many routes with the same prefix. The prefix parameter will be applied
// prefix to all routes defined in the function. The fn parmater is a function in which the grouped // to all routes defined in the function. The fn parameter is a function in which the grouped
// routes should be defined. // routes should be defined.
func (r *Router) Group(prefix string, fn func(r *Router)) { func (r *Router) Group(prefix string, fn func(r *Router)) {
validatePathPart(prefix)
if prefix[0] == '/' { if prefix[0] == '/' {
prefix = prefix[1:] prefix = prefix[1:]
} }
@ -134,3 +133,22 @@ func (r *Router) addEvent(key string, e event) {
r.events = routes r.events = routes
} }
func prepPath(method, prefix, path string) string {
validatePathPart(path)
if path[0] == '/' {
path = path[1:]
}
if path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
return method + prefix + path
}
func validatePathPart(part string) {
if len(part) == 0 {
panic("path was empty")
}
}

View File

@ -25,7 +25,7 @@ func TestRouter(t *testing.T) {
desc(t, 4, "insert a new route succesfully") desc(t, 4, "insert a new route succesfully")
a.NotPanics(func() { a.NotPanics(func() {
r.Get("thing/{id}", handler) r.Get("thing/{id}", handler)
r.Delete("thing/{id}", handler) r.Delete("/thing/{id}/", handler)
r.Put("thing", handler) r.Put("thing", handler)
}) })
@ -39,6 +39,11 @@ func TestRouter(t *testing.T) {
a.Panics(func() { a.Panics(func() {
r2.Patch("panic", handler) r2.Patch("panic", handler)
}) })
desc(t, 4, "panic when when given an empty path")
a.Panics(func() {
r.Post("", handler)
})
} }
desc(t, 2, "PrefixGroup method should") desc(t, 2, "PrefixGroup method should")