Skip to content

Commit

Permalink
Infer domain name from request context (awslabs#67)
Browse files Browse the repository at this point in the history
* Update github.com/aws/aws-lambda-go?!

* Infer serverAddress from RequestContext.DomainName

* Update CustomHostVariable docs

* Remove redundant DefaultServerAddress

* Update vendor.json

* Update github.com/aws/aws-lambda-go

* Dont expect https protocol in host
  • Loading branch information
RaeesBhatti authored Jun 24, 2020
1 parent 9acdb20 commit 54c012a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
9 changes: 3 additions & 6 deletions core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ import (

// CustomHostVariable is the name of the environment variable that contains
// the custom hostname for the request. If this variable is not set the framework
// reverts to `DefaultServerAddress`. The value for a custom host should include
// a protocol: http://my-custom.host.com
// reverts to `RequestContext.DomainName`. The value for a custom host should
// include a protocol: http://my-custom.host.com
const CustomHostVariable = "GO_API_HOST"

// DefaultServerAddress is prepended to the path of each incoming reuqest
const DefaultServerAddress = "https://aws-serverless-go-api.com"

// APIGwContextHeader is the custom header key used to store the
// API Gateway context. To access the Context properties use the
// GetAPIGatewayContext method of the RequestAccessor object.
Expand Down Expand Up @@ -149,7 +146,7 @@ func (r *RequestAccessor) EventToRequest(req events.APIGatewayProxyRequest) (*ht
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
serverAddress := DefaultServerAddress
serverAddress := "https://" + req.RequestContext.DomainName
if customAddress, ok := os.LookupEnv(CustomHostVariable); ok {
serverAddress = customAddress
}
Expand Down
6 changes: 4 additions & 2 deletions core/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,13 @@ var _ = Describe("RequestAccessor tests", func() {
It("Populates the default hostname correctly", func() {

basicRequest := getProxyRequest("orders", "GET")
basicRequest.RequestContext = getRequestContext()
accessor := core.RequestAccessor{}
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
Expect(err).To(BeNil())

Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.Host))
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.URL.Host))
Expect(basicRequest.RequestContext.DomainName).To(Equal(httpReq.Host))
Expect(basicRequest.RequestContext.DomainName).To(Equal(httpReq.URL.Host))
})

It("Uses a custom hostname", func() {
Expand Down Expand Up @@ -336,6 +337,7 @@ func getRequestContext() events.APIGatewayProxyRequestContext {
RequestID: "x",
APIID: "x",
Stage: "prod",
DomainName: "12abcdefgh.execute-api.us-east-2.amazonaws.com",
}
}

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Joker/jade v1.0.0 // indirect
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 // indirect
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6
github.com/aws/aws-lambda-go v1.17.0
github.com/aymerick/raymond v2.0.2+incompatible // indirect
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
Expand Down Expand Up @@ -42,7 +42,6 @@ require (
github.com/pkg/errors v0.8.1 // indirect
github.com/ryanuber/columnize v2.1.0+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862
github.com/valyala/fasttemplate v1.0.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 h1:WDC6ySpJzbxG
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6 h1:YV5tCBu1MJG2XMDkSvDohTQkZZpc2IeZMXuUFCPwE80=
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A=
github.com/aws/aws-lambda-go v1.17.0 h1:Ogihmi8BnpmCNktKAGpNwSiILNNING1MiosnKUfU8m0=
github.com/aws/aws-lambda-go v1.17.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 h1:3T8ZyTDp5QxTx3NU48JVb2u+75xc040fofcBaN+6jPA=
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185/go.mod h1:cFRxtTwTOJkz2x3rQUNCYKWC93yP1VKjR8NUhqFxZNU=
Expand Down Expand Up @@ -84,14 +87,17 @@ github.com/onsi/gomega v1.3.0/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4 h1:euf5tLM++W5h5uyfs6NSMoCGGhw+hRXMLE/DU6hireM=
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862 h1:eg5xqGZGatsyRpVnFJkdeUWSFk46lDgkXLvOryv5ySg=
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
8 changes: 4 additions & 4 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "60T22joBzjxNa8yKzW0V4egimZI=",
"checksumSHA1": "7udeaLOJs0KkZCqN02ndEtOk/+s=",
"path": "github.com/aws/aws-lambda-go/events",
"revision": "dcf76fe64fb68bc66dd3522c904ccf5ff1b2710a",
"revisionTime": "2019-01-29T19:04:57Z"
"revision": "26aa36445a99488623e4b21c0e5c0b4dc684eff9",
"revisionTime": "2020-05-30T01:27:16Z"
},
{
"checksumSHA1": "trfV9u2UzDAypLfWLdFApLNWj0k=",
"checksumSHA1": "SXH3An37BX9zsZA/WmH3MjDqhB4=",
"path": "github.com/aws/aws-lambda-go/lambda",
"revision": "dcf76fe64fb68bc66dd3522c904ccf5ff1b2710a",
"revisionTime": "2019-01-29T19:04:57Z"
Expand Down

0 comments on commit 54c012a

Please sign in to comment.