@@ -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.
9494func (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
0 commit comments