From ce34a432addb1f368108b93b7e1e219405a9d873 Mon Sep 17 00:00:00 2001 From: An Li Date: Wed, 22 Dec 2021 22:31:43 +0800 Subject: [PATCH] Change the document for PR#33 --- README.md | 13 +++++++++---- core/requestv2_test.go | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a3794d7..8013550 100644 --- a/README.md +++ b/README.md @@ -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"] diff --git a/core/requestv2_test.go b/core/requestv2_test.go index 3771e6d..be6f857 100644 --- a/core/requestv2_test.go +++ b/core/requestv2_test.go @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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())