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

Change the document for PR#33 #114

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ $ aws cloudformation deploy --template-file output-sam.yaml --stack-name YOUR_ST
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.

## API Gateway context and stage variables
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.
~~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.~~

The gateway context, stage variables and lambda runtime variables are automatically populate to the context.

```go
// the methods are available in your instance of the GinLambda
// object and receive the http.Request object
apiGwContext := ginLambda.GetAPIGatewayContext(c.Request)
apiGwStageVars := ginLambda.GetAPIGatewayStageVars(c.Request)
// object and receive the context
apiGwContext := ginLambda.GetAPIGatewayContextFromContext(ctx)
apiGwStageVars := ginLambda.GetStageVarsFromContext(ctx)
runtimeContext := ginLambda.GetRuntimeContextFromContext(ctx)

// you can access the properties of the context directly
log.Println(apiGwContext.RequestID)
log.Println(apiGwContext.Stage)
log.Println(runtimeContext.InvokedFunctionArn)


// stage variables are stored in a map[string]string
stageVarValue := apiGwStageVars["MyStageVar"]
Expand Down
22 changes: 11 additions & 11 deletions core/requestv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ var _ = Describe("RequestAccessorV2 tests", func() {
})

It("Populates stage variables correctly", func() {
varsRequest := getProxyRequest("orders", "GET")
varsRequest := getProxyRequestV2("orders", "GET")
varsRequest.StageVariables = getStageVariables()

accessor := core.RequestAccessor{}
accessor := core.RequestAccessorV2{}
httpReq, err := accessor.ProxyEventToHTTPRequest(varsRequest)
Expect(err).To(BeNil())

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

stageVars, ok := core.GetStageVarsFromContext(httpReq.Context())
stageVars, ok := core.GetStageVarsFromContextV2(httpReq.Context())
// not present in context
Expect(ok).To(BeFalse())

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

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

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

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

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

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

Expand Down