Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Dependencies and Duplicated Code #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*.out

.vscode
vendor/*/
gin/aws-lambda-go-api-proxy-gin
core/aws-lambda-go-api-proxy-core
sample/main
sample/output-sam.yaml
sample/output-sam.yaml
vendor/
.idea/
21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/awslabs/aws-lambda-go-api-proxy/proxy"
"github.com/gin-gonic/gin"
)

var ginLambda *ginadapter.GinLambda
var adapter *proxy.Adapter

func init() {
// stdout and stderr are sent to AWS CloudWatch Logs
Expand All @@ -41,12 +41,12 @@ func init() {
})
})

ginLambda = ginadapter.New(r)
adapter = proxy.NewAdapter(r)
}

func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// If no name is provided in the HTTP request body, throw an error
return ginLambda.ProxyWithContext(ctx, req)
return adapter.ProxyWithContext(ctx, req)
}

func main() {
Expand Down Expand Up @@ -94,19 +94,6 @@ log.Println(apiGwContext.Stage)
stageVarValue := apiGwStageVars["MyStageVar"]
```

## Supporting other frameworks
The `aws-lambda-go-api-proxy`, alongside the various adapters, declares a `core` package. The `core` package, contains utility methods and interfaces to translate API Gateway proxy events into Go's default `http.Request` and `http.ResponseWriter` objects.

You can see that the [`ginlambda.go`](gin/adapter.go) file extends the `RequestAccessor` struct defined in the [`request.go`](core/request.go) file. `RequestAccessor` gives you access to the `ProxyEventToHTTPRequest()` method.

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

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`.

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

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.

## License

This library is licensed under the Apache 2.0 License.
62 changes: 0 additions & 62 deletions chi/adapter.go

This file was deleted.

13 changes: 0 additions & 13 deletions chi/chi_suite_test.go

This file was deleted.

43 changes: 0 additions & 43 deletions chi/chilambda_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions core/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const contentTypeHeaderKey = "Content-Type"
// ProxyResponseWriter implements http.ResponseWriter and adds the method
// necessary to return an events.APIGatewayProxyResponse object
type ProxyResponseWriter struct {
headers http.Header
body bytes.Buffer
status int
observers []chan <-bool
headers http.Header
body bytes.Buffer
status int
observers []chan<- bool
}

// NewProxyResponseWriter returns a new ProxyResponseWriter object.
// The object is initialized with an empty map of headers and a
// status code of -1
func NewProxyResponseWriter() *ProxyResponseWriter {
return &ProxyResponseWriter{
headers: make(http.Header),
status: defaultStatusCode,
headers: make(http.Header),
status: defaultStatusCode,
observers: make([]chan<- bool, 0),
}

Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *ProxyResponseWriter) GetProxyResponse() (events.APIGatewayProxyResponse

return events.APIGatewayProxyResponse{
StatusCode: r.status,
Headers: proxyHeaders,
Headers: proxyHeaders,
MultiValueHeaders: http.Header(r.headers),
Body: output,
IsBase64Encoded: isBase64,
Expand Down
62 changes: 0 additions & 62 deletions echo/adapter.go

This file was deleted.

13 changes: 0 additions & 13 deletions echo/echo_suite_test.go

This file was deleted.

37 changes: 0 additions & 37 deletions echo/echolambda_test.go

This file was deleted.

62 changes: 0 additions & 62 deletions gin/adapter.go

This file was deleted.

Loading