Skip to content

Commit

Permalink
Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
akrylysov committed Mar 3, 2023
1 parent 4c6f785 commit 7f40f45
Show file tree
Hide file tree
Showing 22 changed files with 1,210 additions and 818 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: go
go:
- "1.11.x"
- "1.18.x"
- "1.x"
25 changes: 22 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# Changelog

## [0.12.1] - 2019-09-26
## [1.0.0] - 2023-03-02
### Fixed
- API Gateway V2: Fixed response header support.
- API Gateway V2: Fixed handling request cookies.
- API Gateway V2: Fixed multi-value query parameters.
- ALB: Fixed double escaping of query parameters.

### Changed
- `RequestTypeAPIGateway` renamed to `RequestTypeAPIGatewayV1`.
- `ProxyRequestFromContext` renamed to `APIGatewayV1RequestFromContext`.
- `APIGatewayV2HTTPRequestFromContext` renamed to `APIGatewayV2RequestFromContext`.
- `TargetGroupRequestFromContext` renamed to `ALBRequestFromContext`.
- Improved unit tests.
- Go 1.18 is the minimum supported version now.

## [0.13.0] - 2022-01-08
### Added
- Fixed compatibility with Go versions older than 1.13.
- API Gateway V2 support (@a-h).

## [0.12.1] - 2019-09-26
### Fixed
- Compatibility with Go versions older than 1.13.

## [0.12.0] - 2019-09-26
### Added
Expand All @@ -16,7 +35,7 @@
### Changed
- Set RequestURI on request (@RossHammer).
- Unescape Path (@RossHammer).
- Multi-value header support implemented using APIGatewayProxyResponse.MultiValueHeaders.
- Multi-value header support implemented using `APIGatewayProxyResponse.MultiValueHeaders`.

## [0.9] - 2018-12-10
### Added
Expand Down
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/akrylysov/algnhsa"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("index"))
}

func addHandler(w http.ResponseWriter, r *http.Request) {
f, _ := strconv.Atoi(r.FormValue("first"))
s, _ := strconv.Atoi(r.FormValue("second"))
Expand All @@ -27,14 +23,13 @@ func addHandler(w http.ResponseWriter, r *http.Request) {
}

func contextHandler(w http.ResponseWriter, r *http.Request) {
proxyReq, ok := algnhsa.ProxyRequestFromContext(r.Context())
lambdaEvent, ok := algnhsa.APIGatewayV2RequestFromContext(r.Context())
if ok {
fmt.Fprint(w, proxyReq.RequestContext.AccountID)
fmt.Fprint(w, lambdaEvent.RequestContext.AccountID)
}
}

func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/add", addHandler)
http.HandleFunc("/context", contextHandler)
algnhsa.ListenAndServe(http.DefaultServeMux, nil)
Expand All @@ -56,26 +51,48 @@ import (
func main() {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("index"))
w.Write([]byte("hi"))
})
algnhsa.ListenAndServe(r, nil)
}
```

## Setting up API Gateway
## Deployment

First, build your Go application for Linux and zip it:

```bash
GOOS=linux GOARCH=amd64 go build -o handler
zip handler.zip handler
```

AWS provides plenty of ways to expose a Lambda function to the internet.

### Lambda Function URL

This is the easier way to deploy your Lambda function as an HTTP endpoint.
It only requires going to the "Function URL" section of the Lambda function configuration and clicking "Configure Function URL".

### API Gateway

#### HTTP API

1. Create a new HTTP API.

2. Configure a catch-all `$default` route.

#### REST API

1. Create a new REST API.

2. In the "Resources" section create a new `ANY` method to handle requests to `/` (check "Use Lambda Proxy Integration").

![API Gateway index](https://akrylysov.github.io/algnhsa/apigateway-index.png)

3. Add a catch-all `{proxy+}` resource to handle requests to every other path (check "Configure as proxy resource").

![API Gateway catch-all](https://akrylysov.github.io/algnhsa/apigateway-catchall.png)

## Setting up ALB
### ALB

1. Create a new ALB and point it to your Lambda function.

2. In the target group settings enable "Multi value headers".
2. In the target group settings in the "Attributes" section enable "Multi value headers".

[AWS Documentation](https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html)
9 changes: 8 additions & 1 deletion adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package algnhsa
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"

Expand All @@ -21,10 +22,16 @@ func (handler lambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte
if err != nil {
return nil, err
}
if handler.opts.DebugLog {
fmt.Printf("Response: %+v", resp)
}
return json.Marshal(resp)
}

func (handler lambdaHandler) handleEvent(ctx context.Context, payload []byte) (lambdaResponse, error) {
if handler.opts.DebugLog {
fmt.Printf("Request: %s", payload)
}
eventReq, err := newLambdaRequest(ctx, payload, handler.opts)
if err != nil {
return lambdaResponse{}, err
Expand All @@ -35,7 +42,7 @@ func (handler lambdaHandler) handleEvent(ctx context.Context, payload []byte) (l
}
w := httptest.NewRecorder()
handler.httpHandler.ServeHTTP(w, r)
return newLambdaResponse(w, handler.opts.binaryContentTypeMap)
return newLambdaResponse(w, handler.opts.binaryContentTypeMap, eventReq.requestType)
}

// ListenAndServe starts the AWS Lambda runtime (aws-lambda-go lambda.Start) with a given handler.
Expand Down
Loading

0 comments on commit 7f40f45

Please sign in to comment.