Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit ce34a43

Browse files
committed
Change the document for PR#33
1 parent f6f827b commit ce34a43

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,22 @@ $ aws cloudformation deploy --template-file output-sam.yaml --stack-name YOUR_ST
108108
Using the CloudFormation console, you can find the URL for the newly created API endpoint in the `Outputs` tab of the sample stack - it looks sample like this: `https://xxxxxxxxx.execute-api.xx-xxxx-x.amazonaws.com/Prod/pets`. Open a browser window and try to call the URL.
109109

110110
## API Gateway context and stage variables
111-
The `RequestAccessor` object, and therefore `GinLambda`, automatically marshals the API Gateway request context and stage variables objects and stores them in custom headers in the request: `X-GinLambda-ApiGw-Context` and `X-GinLambda-ApiGw-StageVars`. While you could manually unmarshal the json content into the `events.APIGatewayProxyRequestContext` and `map[string]string` objects, the library exports two utility methods to give you easy access to the data.
111+
~~The `RequestAccessor` object, and therefore `GinLambda`, automatically marshals the API Gateway request context and stage variables objects and stores them in custom headers in the request: `X-GinLambda-ApiGw-Context` and `X-GinLambda-ApiGw-StageVars`. While you could manually unmarshal the json content into the `events.APIGatewayProxyRequestContext` and `map[string]string` objects, the library exports two utility methods to give you easy access to the data.~~
112+
113+
The gateway context, stage variables and lambda runtime variables are automatically populate to the context.
112114

113115
```go
114116
// the methods are available in your instance of the GinLambda
115-
// object and receive the http.Request object
116-
apiGwContext := ginLambda.GetAPIGatewayContext(c.Request)
117-
apiGwStageVars := ginLambda.GetAPIGatewayStageVars(c.Request)
117+
// object and receive the context
118+
apiGwContext := ginLambda.GetAPIGatewayContextFromContext(ctx)
119+
apiGwStageVars := ginLambda.GetStageVarsFromContext(ctx)
120+
runtimeContext := ginLambda.GetRuntimeContextFromContext(ctx)
118121

119122
// you can access the properties of the context directly
120123
log.Println(apiGwContext.RequestID)
121124
log.Println(apiGwContext.Stage)
125+
log.Println(runtimeContext.InvokedFunctionArn)
126+
122127

123128
// stage variables are stored in a map[string]string
124129
stageVarValue := apiGwStageVars["MyStageVar"]

core/requestv2_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ var _ = Describe("RequestAccessorV2 tests", func() {
247247
})
248248

249249
It("Populates stage variables correctly", func() {
250-
varsRequest := getProxyRequest("orders", "GET")
250+
varsRequest := getProxyRequestV2("orders", "GET")
251251
varsRequest.StageVariables = getStageVariables()
252252

253-
accessor := core.RequestAccessor{}
253+
accessor := core.RequestAccessorV2{}
254254
httpReq, err := accessor.ProxyEventToHTTPRequest(varsRequest)
255255
Expect(err).To(BeNil())
256256

@@ -262,7 +262,7 @@ var _ = Describe("RequestAccessorV2 tests", func() {
262262
Expect("value1").To(Equal(stageVars["var1"]))
263263
Expect("value2").To(Equal(stageVars["var2"]))
264264

265-
stageVars, ok := core.GetStageVarsFromContext(httpReq.Context())
265+
stageVars, ok := core.GetStageVarsFromContextV2(httpReq.Context())
266266
// not present in context
267267
Expect(ok).To(BeFalse())
268268

@@ -273,7 +273,7 @@ var _ = Describe("RequestAccessorV2 tests", func() {
273273
// should not be in headers
274274
Expect(err).ToNot(BeNil())
275275

276-
stageVars, ok = core.GetStageVarsFromContext(httpReq.Context())
276+
stageVars, ok = core.GetStageVarsFromContextV2(httpReq.Context())
277277
Expect(ok).To(BeTrue())
278278
Expect(2).To(Equal(len(stageVars)))
279279
Expect(stageVars["var1"]).ToNot(BeNil())
@@ -284,9 +284,9 @@ var _ = Describe("RequestAccessorV2 tests", func() {
284284

285285
It("Populates the default hostname correctly", func() {
286286

287-
basicRequest := getProxyRequest("orders", "GET")
288-
basicRequest.RequestContext = getRequestContext()
289-
accessor := core.RequestAccessor{}
287+
basicRequest := getProxyRequestV2("orders", "GET")
288+
basicRequest.RequestContext = getRequestContextV2()
289+
accessor := core.RequestAccessorV2{}
290290
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
291291
Expect(err).To(BeNil())
292292

@@ -297,8 +297,8 @@ var _ = Describe("RequestAccessorV2 tests", func() {
297297
It("Uses a custom hostname", func() {
298298
myCustomHost := "http://my-custom-host.com"
299299
os.Setenv(core.CustomHostVariable, myCustomHost)
300-
basicRequest := getProxyRequest("orders", "GET")
301-
accessor := core.RequestAccessor{}
300+
basicRequest := getProxyRequestV2("orders", "GET")
301+
accessor := core.RequestAccessorV2{}
302302
httpReq, err := accessor.EventToRequestWithContext(context.Background(), basicRequest)
303303
Expect(err).To(BeNil())
304304

@@ -310,8 +310,8 @@ var _ = Describe("RequestAccessorV2 tests", func() {
310310
It("Strips terminating / from hostname", func() {
311311
myCustomHost := "http://my-custom-host.com"
312312
os.Setenv(core.CustomHostVariable, myCustomHost+"/")
313-
basicRequest := getProxyRequest("orders", "GET")
314-
accessor := core.RequestAccessor{}
313+
basicRequest := getProxyRequestV2("orders", "GET")
314+
accessor := core.RequestAccessorV2{}
315315
httpReq, err := accessor.EventToRequestWithContext(context.Background(), basicRequest)
316316
Expect(err).To(BeNil())
317317

0 commit comments

Comments
 (0)