diff --git a/router.go b/router.go index 5432322..326f840 100644 --- a/router.go +++ b/router.go @@ -41,15 +41,25 @@ type APIGRouter struct { request *events.APIGatewayProxyRequest endpoints map[string]*radix.Tree params map[string]string - svcprefix string + prefix string + headers map[string]string +} + +// APIGRouterConfig is used as the input to NewAPIGRouter, request is your incoming +// apig request and prefix will be stripped of all incoming request paths. Headers +// will be sent with all responses. +type APIGRouterConfig struct { + Request *events.APIGatewayProxyRequest + Prefix string + Headers map[string]string } // NOTE: Begin router methods. -// NewAPIGRouter creates a new router using the request and a prefix to strip from your incoming requests. -func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRouter { +// NewAPIGRouter creates a new router using the given router config. +func NewAPIGRouter(cfg *APIGRouterConfig) *APIGRouter { return &APIGRouter{ - request: r, + request: cfg.Request, endpoints: map[string]*radix.Tree{ post: radix.New(), get: radix.New(), @@ -57,8 +67,9 @@ func NewAPIGRouter(r *events.APIGatewayProxyRequest, svcprefix string) *APIGRout patch: radix.New(), delete: radix.New(), }, - params: map[string]string{}, - svcprefix: svcprefix, + params: map[string]string{}, + prefix: cfg.Prefix, + headers: cfg.Headers, } } @@ -97,7 +108,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { err error endpointTree = r.endpoints[r.request.HTTPMethod] - path = strings.TrimPrefix(r.request.Path, "/"+r.svcprefix) + path = strings.TrimPrefix(r.request.Path, r.prefix) response = events.APIGatewayProxyResponse{} splitPath = stripSlashesAndSplit(path) ) @@ -158,6 +169,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { response.StatusCode = status response.Body = string(respbody) + response.Headers = r.headers return response } diff --git a/router_test.go b/router_test.go index bd8102d..f9cc168 100644 --- a/router_test.go +++ b/router_test.go @@ -13,7 +13,14 @@ func TestRouterSpec(t *testing.T) { Convey("Given an instantiated router", t, func() { request := events.APIGatewayProxyRequest{} - rtr := NewAPIGRouter(&request, "shipping") + rtr := NewAPIGRouter(&APIGRouterConfig{ + Request: &request, + Prefix: "/shipping", + Headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "true", + }, + }) Convey("When the handler func does NOT return an error", func() { hdlrfunc := func(ctx *APIGContext) { @@ -42,11 +49,15 @@ func TestRouterSpec(t *testing.T) { }, } - Convey("The router will return the expected status and body", func() { + Convey("The router will return the expected status, body, and headers", func() { response := rtr.Respond() So(response.StatusCode, ShouldEqual, http.StatusOK) So(response.Body, ShouldEqual, "hello") + So(response.Headers, ShouldResemble, map[string]string{ + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "true", + }) }) })