Documentation
¶
Index ¶
- Variables
- func StartV1[Req any, Resp any](handler Handler[Req, Resp], opts ...HttpOption)
- func StartV2[Req any, Resp any](handler Handler[Req, Resp], opts ...HttpOption)
- type APIGatewayProxyRequest
- type APIGatewayProxyResponse
- type APIGatewayV2HTTPResponse
- type Context
- func (l *Context[Req, Resp]) Error() string
- func (l *Context[Req, Resp]) GetLocal(key string) (any, bool)
- func (l *Context[Req, Resp]) Is(err error) bool
- func (l *Context[Req, Resp]) SetLocal(key string, value any) *Context[Req, Resp]
- func (l *Context[Req, Resp]) UnsetLocal(key string) *Context[Req, Resp]
- type Cookie
- type CookieSameSite
- type Error
- type ErrorResponse
- type Handler
- type Headers
- type HttpOption
- type HttpResponse
- type Middleware
- type None
- type PathParams
- type Query
- type Request
- type Resource
- type Response
- func (r *Response[T]) Error() string
- func (r *Response[T]) Header(key string, value string) *Response[T]
- func (r *Response[T]) JSON(data any) *Response[T]
- func (r *Response[T]) Redirect(url string, status ...int) *Response[T]
- func (r *Response[T]) SendString(data string) *Response[T]
- func (r *Response[T]) SetCookie(c Cookie) *Response[T]
- func (r *Response[T]) Status(status int) *Response[T]
- func (r *Response[T]) UnsetCookie(name string) *Response[T]
Constants ¶
This section is empty.
Variables ¶
var DefaultErrorHandler = func(_ context.Context, err error) (HttpResponse, error) { e := err for { if httpErr, ok := e.(ErrorResponse); ok { b, err := httpErr.HttpBody() if err != nil { break } return HttpResponse{ StatusCode: httpErr.HttpStatusCode(), Headers: httpErr.HttpHeaders(), Body: b, }, nil } if e = errors.Unwrap(e); e == nil { break } } return HttpResponse{ StatusCode: http.StatusInternalServerError, Body: []byte(`{"message":"Internal Server Error"}`), }, nil }
DefaultErrorHandler is the default error handler for the lambda function. If th error implements ErrorResponse, it will return an HttpResponse with the status code, headers and body. If the error does not implement ErrorResponse, it will return a generic internal server error message.
The original error message is not returned to avoid accidentally leaking sensitive information.
var (
ErrKeyNotFound = errors.New("key not found")
)
Functions ¶
Types ¶
type APIGatewayProxyRequest ¶
type APIGatewayProxyRequest struct {
Resource string `json:"resource"` // The resource path defined in API Gateway
Path string `json:"path"` // The url path for the caller
HTTPMethod string `json:"httpMethod"`
Headers map[string]string `json:"headers"`
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
QueryStringParameters map[string]string `json:"queryStringParameters"`
MultiValueQueryStringParameters map[string][]string `json:"multiValueQueryStringParameters"`
PathParameters map[string]string `json:"pathParameters"`
StageVariables map[string]string `json:"stageVariables"`
RequestContext events.APIGatewayProxyRequestContext `json:"requestContext"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
}
APIGatewayProxyRequest contains data coming from the API Gateway proxy
type APIGatewayProxyResponse ¶
type APIGatewayProxyResponse struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body json.RawMessage `json:"body"`
}
APIGatewayProxyResponse configures the response to be returned by API Gateway for the request
type Context ¶
type Context[Req any, Resp any] struct { Context context.Context Request *Request[Req] Response *Response[Resp] Locals map[string]any // contains filtered or unexported fields }
func (*Context[Req, Resp]) UnsetLocal ¶
type Cookie ¶
type CookieSameSite ¶
type CookieSameSite string
const ( CookieSameSiteDefaultMode CookieSameSite = "SameSite" CookieSameSiteLaxMode CookieSameSite = "Lax" CookieSameSiteStrictMode CookieSameSite = "Strict" CookieSameSiteNoneMode CookieSameSite = "None" )
type Error ¶
Error is a struct that implements ErrorResponse. It represents an error that can be returned by the lambda function.
func (*Error) HttpHeaders ¶
func (*Error) HttpStatusCode ¶
type ErrorResponse ¶
type HttpOption ¶
type HttpOption func(*options)
func WithErrorHandler ¶
func WithErrorHandler(h func(context.Context, error) (HttpResponse, error)) HttpOption
WithErrorHandler is an option that allows you to pass a custom error handler to the lambda function.
func WithResources ¶
func WithResources(r ...Resource) HttpOption
WithResources is an option that allows you to pass resources to the lambda function.
type HttpResponse ¶
type HttpResponse struct {
StatusCode int
Headers map[string]string
Body json.RawMessage
}