Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
implement apex gateway ListenAndServe internally (#101)
Browse files Browse the repository at this point in the history
* implement apex gateway ListenAndServe internally

* implement apex gateway ListenAndServe internally for api gateway v2
  • Loading branch information
yusuf-erdem authored Mar 9, 2022
1 parent 0c5e874 commit 66f5311
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil v2.20.8+incompatible
github.com/stretchr/testify v1.6.1
github.com/apex/gateway v1.1.2
github.com/apex/gateway/v2 v2.0.0
)
34 changes: 34 additions & 0 deletions wrappers/apexgateway/thundra_apex_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package apexgateway

import (
"context"
"github.com/apex/gateway"
"github.com/thundra-io/thundra-lambda-agent-go/v2/thundra"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func ListenAndServe(h http.Handler) error {
if h == nil {
h = http.DefaultServeMux
}

lambda.Start(thundra.Wrap(wrapper(h)))

return nil
}

func wrapper(h http.Handler) func(ctx context.Context, e events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return func(ctx context.Context, e events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
r, err := gateway.NewRequest(ctx, e)
if err != nil {
return events.APIGatewayProxyResponse{}, err
}

w := gateway.NewResponse()
h.ServeHTTP(w, r)
return w.End(), nil
}
}
35 changes: 35 additions & 0 deletions wrappers/apexgatewayv2/thundra_apex_gateway_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package apexgatewayv2

import (
"context"
"github.com/apex/gateway/v2"
"github.com/thundra-io/thundra-lambda-agent-go/v2/thundra"
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func ListenAndServe(h http.Handler) error {
if h == nil {
h = http.DefaultServeMux
}

lambda.Start(thundra.Wrap(wrapper(h)))

return nil
}

func wrapper(h http.Handler) func(ctx context.Context, e events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
return func(ctx context.Context, e events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
r, err := gateway.NewRequest(ctx, e)
if err != nil {
return events.APIGatewayV2HTTPResponse{}, err
}

w := gateway.NewResponse()
h.ServeHTTP(w, r)

return w.End(), nil
}
}

0 comments on commit 66f5311

Please sign in to comment.