Skip to content

Commit 6bfde97

Browse files
committed
use idiomatic init func in example code
1 parent 3a4d350 commit 6bfde97

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

README.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,22 @@ import (
2828
"github.com/gin-gonic/gin"
2929
)
3030

31-
var initialized = false
3231
var ginLambda *ginadapter.GinLambda
3332

34-
func Handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
35-
36-
if !initialized {
37-
// stdout and stderr are sent to AWS CloudWatch Logs
38-
log.Printf("Gin cold start")
39-
r := gin.Default()
40-
r.GET("/ping", func(c *gin.Context) {
41-
c.JSON(200, gin.H{
42-
"message": "pong",
43-
})
33+
func init() {
34+
// stdout and stderr are sent to AWS CloudWatch Logs
35+
log.Printf("Gin cold start")
36+
r := gin.Default()
37+
r.GET("/ping", func(c *gin.Context) {
38+
c.JSON(200, gin.H{
39+
"message": "pong",
4440
})
41+
})
4542

46-
ginLambda = ginadapter.New(r)
47-
initialized = true
48-
}
43+
ginLambda = ginadapter.New(r)
44+
}
4945

46+
func Handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
5047
// If no name is provided in the HTTP request body, throw an error
5148
return ginLambda.Proxy(req)
5249
}
@@ -57,10 +54,10 @@ func main() {
5754
```
5855

5956
## Other frameworks
60-
This package also supports [Negroni](https://github.com/urfave/negroni), [GorillaMux](https://github.com/gorilla/mux), and plain old `HandlerFunc` - take a look at the code in their respective sub-directories. All packages implement the `Proxy` method exactly like our Gin sample above.
57+
This package also supports [Negroni](https://github.com/urfave/negroni), [GorillaMux](https://github.com/gorilla/mux), and plain old `HandlerFunc` - take a look at the code in their respective sub-directories. All packages implement the `Proxy` method exactly like our Gin sample above.
6158

6259
## Deploying the sample
63-
We have included a [SAM template](https://github.com/awslabs/serverless-application-model) with our sample application. You can use the [AWS CLI](https://aws.amazon.com/cli/) to quickly deploy the application in your AWS account.
60+
We have included a [SAM template](https://github.com/awslabs/serverless-application-model) with our sample application. You can use the [AWS CLI](https://aws.amazon.com/cli/) to quickly deploy the application in your AWS account.
6461

6562
First, build the sample application by running `make` from the `aws-lambda-go-api-proxy` directory.
6663

@@ -103,12 +100,12 @@ You can see that the [`ginlambda.go`](gin/adapter.go) file extends the `RequestA
103100

104101
The `GinLambda` object is initialized with an instance of `gin.Engine`. `gin.Engine` implements methods defined in the `http.Handler` interface.
105102

106-
The `Proxy` method of the `GinLambda` object simply receives the `events.APIGatewayProxyRequest` object and uses the `ProxyEventToHTTPRequest()` method to convert it into an `http.Request` object. Next, it creates a new `ProxyResponseWriter` object (defined in the [`response.go`](core/response.go)) file and passes both request and response writer to the `ServeHTTP` method of the `gin.Engine`.
103+
The `Proxy` method of the `GinLambda` object simply receives the `events.APIGatewayProxyRequest` object and uses the `ProxyEventToHTTPRequest()` method to convert it into an `http.Request` object. Next, it creates a new `ProxyResponseWriter` object (defined in the [`response.go`](core/response.go)) file and passes both request and response writer to the `ServeHTTP` method of the `gin.Engine`.
107104

108-
The `ProxyResponseWriter` exports a method called `GetProxyResponse()` to generate an `events.APIGatewayProxyResponse` object from the data written to the response writer.
105+
The `ProxyResponseWriter` exports a method called `GetProxyResponse()` to generate an `events.APIGatewayProxyResponse` object from the data written to the response writer.
109106

110107
Support for frameworks other than Gin can rely on the same methods from the `core` package and swap the `gin.Engine` object for the relevant framework's object.
111108

112109
## License
113110

114-
This library is licensed under the Apache 2.0 License.
111+
This library is licensed under the Apache 2.0 License.

0 commit comments

Comments
 (0)