Skip to content

Commit 54c012a

Browse files
authored
Infer domain name from request context (awslabs#67)
* 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
1 parent 9acdb20 commit 54c012a

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

core/request.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ import (
2121

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

28-
// DefaultServerAddress is prepended to the path of each incoming reuqest
29-
const DefaultServerAddress = "https://aws-serverless-go-api.com"
30-
3128
// APIGwContextHeader is the custom header key used to store the
3229
// API Gateway context. To access the Context properties use the
3330
// GetAPIGatewayContext method of the RequestAccessor object.
@@ -149,7 +146,7 @@ func (r *RequestAccessor) EventToRequest(req events.APIGatewayProxyRequest) (*ht
149146
if !strings.HasPrefix(path, "/") {
150147
path = "/" + path
151148
}
152-
serverAddress := DefaultServerAddress
149+
serverAddress := "https://" + req.RequestContext.DomainName
153150
if customAddress, ok := os.LookupEnv(CustomHostVariable); ok {
154151
serverAddress = customAddress
155152
}

core/request_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ var _ = Describe("RequestAccessor tests", func() {
287287
It("Populates the default hostname correctly", func() {
288288

289289
basicRequest := getProxyRequest("orders", "GET")
290+
basicRequest.RequestContext = getRequestContext()
290291
accessor := core.RequestAccessor{}
291292
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
292293
Expect(err).To(BeNil())
293294

294-
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.Host))
295-
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.URL.Host))
295+
Expect(basicRequest.RequestContext.DomainName).To(Equal(httpReq.Host))
296+
Expect(basicRequest.RequestContext.DomainName).To(Equal(httpReq.URL.Host))
296297
})
297298

298299
It("Uses a custom hostname", func() {
@@ -336,6 +337,7 @@ func getRequestContext() events.APIGatewayProxyRequestContext {
336337
RequestID: "x",
337338
APIID: "x",
338339
Stage: "prod",
340+
DomainName: "12abcdefgh.execute-api.us-east-2.amazonaws.com",
339341
}
340342
}
341343

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/BurntSushi/toml v0.3.1 // indirect
88
github.com/Joker/jade v1.0.0 // indirect
99
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 // indirect
10-
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6
10+
github.com/aws/aws-lambda-go v1.17.0
1111
github.com/aymerick/raymond v2.0.2+incompatible // indirect
1212
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
1313
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
@@ -42,7 +42,6 @@ require (
4242
github.com/pkg/errors v0.8.1 // indirect
4343
github.com/ryanuber/columnize v2.1.0+incompatible // indirect
4444
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
45-
github.com/stretchr/testify v1.3.0 // indirect
4645
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4
4746
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862
4847
github.com/valyala/fasttemplate v1.0.1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 h1:WDC6ySpJzbxG
99
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
1010
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6 h1:YV5tCBu1MJG2XMDkSvDohTQkZZpc2IeZMXuUFCPwE80=
1111
github.com/aws/aws-lambda-go v0.0.0-20190129190457-dcf76fe64fb6/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A=
12+
github.com/aws/aws-lambda-go v1.17.0 h1:Ogihmi8BnpmCNktKAGpNwSiILNNING1MiosnKUfU8m0=
13+
github.com/aws/aws-lambda-go v1.17.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
1214
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
1315
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
16+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
1417
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1518
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 h1:3T8ZyTDp5QxTx3NU48JVb2u+75xc040fofcBaN+6jPA=
1619
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185/go.mod h1:cFRxtTwTOJkz2x3rQUNCYKWC93yP1VKjR8NUhqFxZNU=
@@ -84,14 +87,17 @@ github.com/onsi/gomega v1.3.0/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5
8487
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
8588
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8689
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
90+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8791
github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s=
8892
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
8993
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
9094
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
9195
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9296
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
97+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
9398
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4 h1:euf5tLM++W5h5uyfs6NSMoCGGhw+hRXMLE/DU6hireM=
9499
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
100+
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
95101
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862 h1:eg5xqGZGatsyRpVnFJkdeUWSFk46lDgkXLvOryv5ySg=
96102
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
97103
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

vendor/vendor.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"ignore": "test",
44
"package": [
55
{
6-
"checksumSHA1": "60T22joBzjxNa8yKzW0V4egimZI=",
6+
"checksumSHA1": "7udeaLOJs0KkZCqN02ndEtOk/+s=",
77
"path": "github.com/aws/aws-lambda-go/events",
8-
"revision": "dcf76fe64fb68bc66dd3522c904ccf5ff1b2710a",
9-
"revisionTime": "2019-01-29T19:04:57Z"
8+
"revision": "26aa36445a99488623e4b21c0e5c0b4dc684eff9",
9+
"revisionTime": "2020-05-30T01:27:16Z"
1010
},
1111
{
12-
"checksumSHA1": "trfV9u2UzDAypLfWLdFApLNWj0k=",
12+
"checksumSHA1": "SXH3An37BX9zsZA/WmH3MjDqhB4=",
1313
"path": "github.com/aws/aws-lambda-go/lambda",
1414
"revision": "dcf76fe64fb68bc66dd3522c904ccf5ff1b2710a",
1515
"revisionTime": "2019-01-29T19:04:57Z"

0 commit comments

Comments
 (0)