Changed handlers to veriadic parameter inputs, allowing for multiple

middleware functions
This commit is contained in:
mitchelljfs 2018-07-16 16:44:38 -07:00
parent 2005f453c6
commit 0a73e3f069
2 changed files with 55 additions and 42 deletions

View File

@ -66,35 +66,38 @@ func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRout
}
// Get creates a new get endpoint.
func (r *APIGRouter) Get(route string, handler APIGHandler) {
r.addEndpoint(get, route, handler)
func (r *APIGRouter) Get(route string, handlers ...APIGHandler) {
r.addEndpoint(get, route, handlers)
}
// Post creates a new post endpoint.
func (r *APIGRouter) Post(route string, handler APIGHandler) {
r.addEndpoint(post, route, handler)
func (r *APIGRouter) Post(route string, handlers ...APIGHandler) {
r.addEndpoint(post, route, handlers)
}
// Put creates a new put endpoint.
func (r *APIGRouter) Put(route string, handler APIGHandler) {
r.addEndpoint(put, route, handler)
func (r *APIGRouter) Put(route string, handlers ...APIGHandler) {
r.addEndpoint(put, route, handlers)
}
// Patch creates a new patch endpoint
func (r *APIGRouter) Patch(route string, handler APIGHandler) {
r.addEndpoint(patch, route, handler)
func (r *APIGRouter) Patch(route string, handlers ...APIGHandler) {
r.addEndpoint(patch, route, handlers)
}
// Delete creates a new delete endpoint.
func (r *APIGRouter) Delete(route string, handler APIGHandler) {
r.addEndpoint(delete, route, handler)
func (r *APIGRouter) Delete(route string, handlers ...APIGHandler) {
r.addEndpoint(delete, route, handlers)
}
// Respond returns an APIGatewayProxyResponse to respond to the lambda request.
func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
var (
handlerInterface interface{}
handlersInterface interface{}
ok bool
status int
respbody []byte
err error
endpointTree = r.endpoints[r.request.HTTPMethod]
path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix)
@ -109,7 +112,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
}
}
if handlerInterface, ok = endpointTree.Get(path); !ok {
if handlersInterface, ok = endpointTree.Get(path); !ok {
respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"})
response.StatusCode = http.StatusNotFound
@ -117,8 +120,9 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
return response
}
handler := handlerInterface.(APIGHandler)
handlers := handlersInterface.([]APIGHandler)
for _, handler := range handlers {
req := &APIGRequest{
Path: r.request.PathParameters,
QryStr: r.request.QueryStringParameters,
@ -130,7 +134,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
res := &APIGResponse{}
handler(req, res)
status, respbody, err := res.deconstruct()
status, respbody, err = res.deconstruct()
if err != nil {
respbody, _ := json.Marshal(map[string]string{"error": err.Error()})
@ -144,6 +148,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
response.Body = string(respbody)
return response
}
}
response.StatusCode = status
response.Body = string(respbody)
@ -161,8 +166,8 @@ func (res *APIGResponse) deconstruct() (int, []byte, error) {
return res.Status, res.Body, res.Err
}
func (r *APIGRouter) addEndpoint(method string, route string, handler APIGHandler) {
if _, overwrite := r.endpoints[method].Insert(route, handler); overwrite {
func (r *APIGRouter) addEndpoint(method string, route string, handlers []APIGHandler) {
if _, overwrite := r.endpoints[method].Insert(route, handlers); overwrite {
panic("endpoint already existent")
}

View File

@ -20,7 +20,6 @@ func TestRouterSpec(t *testing.T) {
res.Status = http.StatusOK
res.Body = []byte("hello")
res.Err = nil
}
Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() {
@ -128,15 +127,24 @@ func TestRouterSpec(t *testing.T) {
})
Convey("When the handler func does return a status < 400", func() {
hdlrfunc := func(req *APIGRequest, res *APIGResponse) {
middlefunc1 := func(req *APIGRequest, res *APIGResponse) {
res.Status = http.StatusOK
res.Body = []byte("hello")
res.Err = nil
}
middlefunc2 := func(req *APIGRequest, res *APIGResponse) {
res.Status = http.StatusOK
res.Body = []byte("hello")
res.Err = errors.New("bad request")
}
hdlrfunc := func(req *APIGRequest, res *APIGResponse) {
res.Status = http.StatusOK
res.Body = []byte("hello")
res.Err = nil
}
Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() {
rtr.Get("/orders/filter/by_user/{id}", hdlrfunc)
rtr.Get("/orders/filter/by_user/{id}", middlefunc1, middlefunc2, hdlrfunc)
Convey("And the request matches the pattern and the path params are filled", func() {
request.HTTPMethod = http.MethodGet