Documentation
¶
Overview ¶
Package lambdahttp adapts a standard library http.Handler so it can serve AWS Lambda requests originating from API Gateway HTTP APIs (payload format v2) or Lambda Function URLs.
The adapter is a bidirectional translator: it converts the incoming Lambda event into an *http.Request, runs your handler against a buffering ResponseWriter, and converts the buffered result back into a Lambda response. Your handler code stays completely unaware that it is running on Lambda.
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello world")
})
lambdahttp.New(mux).Start()
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequestEvent ¶
func RequestEvent(ctx context.Context) (events.APIGatewayV2HTTPRequest, bool)
RequestEvent returns the original API Gateway v2 event associated with the request context, if present. Handlers can use it to reach data that has no http.Request equivalent, such as authorizer claims, stage variables, or the full request context.
func handler(w http.ResponseWriter, r *http.Request) {
if event, ok := lambdahttp.RequestEvent(r.Context()); ok {
// event.RequestContext.Authorizer.JWT.Claims, etc.
}
}
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter wraps an http.Handler so it can serve AWS Lambda requests originating from API Gateway HTTP APIs (payload format v2) or Lambda Function URLs.
func (*Adapter) Proxy ¶
func (a *Adapter) Proxy(ctx context.Context, event events.APIGatewayV2HTTPRequest) (resp events.APIGatewayV2HTTPResponse, err error)
Proxy translates a Lambda event into an *http.Request, runs the wrapped handler, and translates the buffered result back into a Lambda response. Its signature satisfies lambda.Start.
A panic in the handler is recovered, logged with its stack trace via slog, and reported to the caller as a 500 response rather than crashing the Lambda invocation. An event whose body cannot be decoded is logged and answered with a 400 without invoking the handler; in both cases the returned error is nil so the invocation itself still succeeds.
type Option ¶
type Option func(*Adapter)
Option configures an Adapter.
func WithBasePath ¶
WithBasePath strips the given prefix from the request path before the request reaches the handler. This is useful when the API is served under a custom domain base path (for example "/api") that the handler should not see. The prefix only matches on segment boundaries: "/api" strips "/api/users" but leaves "/apiv2/users" untouched. A trailing slash on basePath is ignored.
func WithLogger ¶
WithLogger sets the logger the adapter uses to report recovered panics and rejected events. When not set, slog.Default() is used.