Skip to content

Commit 0a73e3f

Browse files
committed
Changed handlers to veriadic parameter inputs, allowing for multiple
middleware functions
1 parent 2005f45 commit 0a73e3f

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

router.go

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,38 @@ func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRout
6666
}
6767

6868
// Get creates a new get endpoint.
69-
func (r *APIGRouter) Get(route string, handler APIGHandler) {
70-
r.addEndpoint(get, route, handler)
69+
func (r *APIGRouter) Get(route string, handlers ...APIGHandler) {
70+
r.addEndpoint(get, route, handlers)
7171
}
7272

7373
// Post creates a new post endpoint.
74-
func (r *APIGRouter) Post(route string, handler APIGHandler) {
75-
r.addEndpoint(post, route, handler)
74+
func (r *APIGRouter) Post(route string, handlers ...APIGHandler) {
75+
r.addEndpoint(post, route, handlers)
7676
}
7777

7878
// Put creates a new put endpoint.
79-
func (r *APIGRouter) Put(route string, handler APIGHandler) {
80-
r.addEndpoint(put, route, handler)
79+
func (r *APIGRouter) Put(route string, handlers ...APIGHandler) {
80+
r.addEndpoint(put, route, handlers)
8181
}
8282

8383
// Patch creates a new patch endpoint
84-
func (r *APIGRouter) Patch(route string, handler APIGHandler) {
85-
r.addEndpoint(patch, route, handler)
84+
func (r *APIGRouter) Patch(route string, handlers ...APIGHandler) {
85+
r.addEndpoint(patch, route, handlers)
8686
}
8787

8888
// Delete creates a new delete endpoint.
89-
func (r *APIGRouter) Delete(route string, handler APIGHandler) {
90-
r.addEndpoint(delete, route, handler)
89+
func (r *APIGRouter) Delete(route string, handlers ...APIGHandler) {
90+
r.addEndpoint(delete, route, handlers)
9191
}
9292

9393
// Respond returns an APIGatewayProxyResponse to respond to the lambda request.
9494
func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
9595
var (
96-
handlerInterface interface{}
97-
ok bool
96+
handlersInterface interface{}
97+
ok bool
98+
status int
99+
respbody []byte
100+
err error
98101

99102
endpointTree = r.endpoints[r.request.HTTPMethod]
100103
path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix)
@@ -109,40 +112,42 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
109112
}
110113
}
111114

112-
if handlerInterface, ok = endpointTree.Get(path); !ok {
115+
if handlersInterface, ok = endpointTree.Get(path); !ok {
113116
respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"})
114117

115118
response.StatusCode = http.StatusNotFound
116119
response.Body = string(respbody)
117120
return response
118121
}
119122

120-
handler := handlerInterface.(APIGHandler)
123+
handlers := handlersInterface.([]APIGHandler)
121124

122-
req := &APIGRequest{
123-
Path: r.request.PathParameters,
124-
QryStr: r.request.QueryStringParameters,
125-
Request: r.request,
126-
}
127-
if r.request.RequestContext.Authorizer["claims"] != nil {
128-
req.Claims = r.request.RequestContext.Authorizer["claims"].(map[string]interface{})
129-
}
130-
res := &APIGResponse{}
131-
132-
handler(req, res)
133-
status, respbody, err := res.deconstruct()
134-
135-
if err != nil {
136-
respbody, _ := json.Marshal(map[string]string{"error": err.Error()})
137-
if strings.Contains(err.Error(), "record not found") {
138-
status = 204
139-
} else if status < 400 {
140-
status = 400
125+
for _, handler := range handlers {
126+
req := &APIGRequest{
127+
Path: r.request.PathParameters,
128+
QryStr: r.request.QueryStringParameters,
129+
Request: r.request,
130+
}
131+
if r.request.RequestContext.Authorizer["claims"] != nil {
132+
req.Claims = r.request.RequestContext.Authorizer["claims"].(map[string]interface{})
133+
}
134+
res := &APIGResponse{}
135+
136+
handler(req, res)
137+
status, respbody, err = res.deconstruct()
138+
139+
if err != nil {
140+
respbody, _ := json.Marshal(map[string]string{"error": err.Error()})
141+
if strings.Contains(err.Error(), "record not found") {
142+
status = 204
143+
} else if status < 400 {
144+
status = 400
145+
}
146+
147+
response.StatusCode = status
148+
response.Body = string(respbody)
149+
return response
141150
}
142-
143-
response.StatusCode = status
144-
response.Body = string(respbody)
145-
return response
146151
}
147152

148153
response.StatusCode = status
@@ -161,8 +166,8 @@ func (res *APIGResponse) deconstruct() (int, []byte, error) {
161166
return res.Status, res.Body, res.Err
162167
}
163168

164-
func (r *APIGRouter) addEndpoint(method string, route string, handler APIGHandler) {
165-
if _, overwrite := r.endpoints[method].Insert(route, handler); overwrite {
169+
func (r *APIGRouter) addEndpoint(method string, route string, handlers []APIGHandler) {
170+
if _, overwrite := r.endpoints[method].Insert(route, handlers); overwrite {
166171
panic("endpoint already existent")
167172
}
168173

router_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func TestRouterSpec(t *testing.T) {
2020
res.Status = http.StatusOK
2121
res.Body = []byte("hello")
2222
res.Err = nil
23-
2423
}
2524

2625
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) {
128127
})
129128

130129
Convey("When the handler func does return a status < 400", func() {
131-
hdlrfunc := func(req *APIGRequest, res *APIGResponse) {
130+
middlefunc1 := func(req *APIGRequest, res *APIGResponse) {
131+
res.Status = http.StatusOK
132+
res.Body = []byte("hello")
133+
res.Err = nil
134+
}
135+
middlefunc2 := func(req *APIGRequest, res *APIGResponse) {
132136
res.Status = http.StatusOK
133137
res.Body = []byte("hello")
134138
res.Err = errors.New("bad request")
135-
139+
}
140+
hdlrfunc := func(req *APIGRequest, res *APIGResponse) {
141+
res.Status = http.StatusOK
142+
res.Body = []byte("hello")
143+
res.Err = nil
136144
}
137145

138146
Convey("And a Get handler expecting the pattern /orders/filter/by_user/{id} is defined", func() {
139-
rtr.Get("/orders/filter/by_user/{id}", hdlrfunc)
147+
rtr.Get("/orders/filter/by_user/{id}", middlefunc1, middlefunc2, hdlrfunc)
140148

141149
Convey("And the request matches the pattern and the path params are filled", func() {
142150
request.HTTPMethod = http.MethodGet

0 commit comments

Comments
 (0)