|
1 |
| -## AWS Lambda Go Api Proxy [](https://travis-ci.org/awslabs/aws-lambda-go-api-proxy) |
2 |
| -aws-lambda-go-api-proxy makes it easy to run Golang APIs written with frameworks such as [Gin](https://github.com/gin-gonic/gin) with AWS Lambda and Amazon API Gateway. |
| 1 | +## AWS Lambda Go API Proxy [](https://travis-ci.org/awslabs/aws-lambda-go-api-proxy) |
| 2 | +aws-lambda-go-api-proxy makes it easy to run Go APIs written with frameworks such as [Gin](https://github.com/gin-gonic/gin) with AWS Lambda and Amazon API Gateway. |
3 | 3 |
|
4 | 4 | ## Getting started
|
5 |
| -The first step is to install the required dependencies |
| 5 | + |
| 6 | +Install required dependencies. |
6 | 7 |
|
7 | 8 | ```bash
|
8 |
| -# First, we install the Lambda go libraries |
| 9 | +# First, install the Lambda go libraries. |
9 | 10 | $ go get github.com/aws/aws-lambda-go/events
|
10 | 11 | $ go get github.com/aws/aws-lambda-go/lambda
|
11 | 12 |
|
12 |
| -# Next, we install the core library |
| 13 | +# Next, install the core library. |
13 | 14 | $ go get github.com/awslabs/aws-lambda-go-api-proxy/...
|
14 | 15 | ```
|
15 | 16 |
|
16 |
| -Following the instructions from the [Lambda documentation](https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html), we need to declare a `Handler` method for our main package. We will declare a `ginadapter.GinLambda` object |
17 |
| -in the global scope, initialized once it in the Handler with all its API methods, and then use the `Proxy` method to translate requests and responses |
| 17 | +### Standard library |
| 18 | + |
| 19 | +To use with the standard library, the `httpadaptor.New` function takes in a `http.Handler`. The `ProxyWithContent` method on the `httpadapter.HandlerAdapter` can then be used as a Lambda handler. |
| 20 | + |
| 21 | +```go |
| 22 | +package main |
| 23 | + |
| 24 | +import ( |
| 25 | + "io" |
| 26 | + "net/http" |
| 27 | + |
| 28 | + "github.com/aws/aws-lambda-go/lambda" |
| 29 | + "github.com/awslabs/aws-lambda-go-api-proxy/httpadapter" |
| 30 | +) |
| 31 | + |
| 32 | +func main() { |
| 33 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 34 | + io.WriteString(w, "Hello") |
| 35 | + }) |
| 36 | + |
| 37 | + lambda.Start(httpadapter.New(http.DefaultServeMux).ProxyWithContext) |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Gin |
| 42 | + |
| 43 | +To use with the Gin framework, following the instructions from the [Lambda documentation](https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html), declare a `Handler` method for the main package. |
| 44 | + |
| 45 | +Declare a `ginadapter.GinLambda` object in the global scope, and initialize it in the `init` function, adding all API methods. |
| 46 | + |
| 47 | +The `ProxyWithContext` method is then used to translate requests and responses. |
18 | 48 |
|
19 | 49 | ```go
|
20 | 50 | package main
|
|
0 commit comments