This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement apex gateway ListenAndServe internally (#101)
* implement apex gateway ListenAndServe internally * implement apex gateway ListenAndServe internally for api gateway v2
- Loading branch information
1 parent
0c5e874
commit 66f5311
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |