Documentation ¶
Index ¶
- Variables
- func APIGatewayUserID(ctx events.APIGatewayProxyRequestContext) string
- func ContextLambdaRequest(ctx context.Context) *events.APIGatewayProxyRequest
- func ContextLambdaResponse(ctx context.Context) *events.APIGatewayProxyResponse
- func SetContextLambdaRequest(ctx context.Context, req *events.APIGatewayProxyRequest) context.Context
- func SetContextLambdaResponse(ctx context.Context, res *events.APIGatewayProxyResponse) context.Context
- func SetContextPathVars(ctx context.Context, pathVars PathVars) context.Context
- type API
- func (api *API) AddEndpoint(path string, handler interface{}, hooks ...MiddlewareHook)
- func (api *API) Call(ctx context.Context, method, path string, input json.RawMessage) (out interface{}, err error)
- func (api *API) GetMethodsForPath(path string) []string
- func (api *API) HTTPProxy(w http.ResponseWriter, r *http.Request)
- func (api *API) LambdaProxy(corsAllowedOrigin string) func(*events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error)
- func (api *API) MatchEndpoint(method, path string) (*Endpoint, PathVars)
- type APIError
- type APIPath
- type Endpoint
- type EndpointInput
- type MiddlewareHook
- type PathVars
Constants ¶
This section is empty.
Variables ¶
var ErrBadRequest = errors.New("bad request")
ErrBadRequest represents an error from a malformed request.
var ErrInternal = errors.New("internal error")
ErrInternal represents some unexpected internal error.
var ErrNotFound = errors.New("path not found")
ErrNotFound represents a 404 error.
Functions ¶
func APIGatewayUserID ¶ added in v0.0.5
func APIGatewayUserID(ctx events.APIGatewayProxyRequestContext) string
APIGatewayUserID returns the subject from the proxy request's authorizer.
func ContextLambdaRequest ¶ added in v0.1.0
func ContextLambdaRequest(ctx context.Context) *events.APIGatewayProxyRequest
func ContextLambdaResponse ¶ added in v0.1.0
func ContextLambdaResponse(ctx context.Context) *events.APIGatewayProxyResponse
func SetContextLambdaRequest ¶ added in v0.1.0
func SetContextLambdaResponse ¶ added in v0.1.0
Types ¶
type API ¶
type API struct {
Endpoints []*Endpoint
}
API is an object that holds all API methods and can dispatch them.
func (*API) AddEndpoint ¶
func (api *API) AddEndpoint(path string, handler interface{}, hooks ...MiddlewareHook)
AddEndpoint registers an endpoint with this API. It also allows adding middleware hooks to the endpoint.
func (*API) Call ¶
func (api *API) Call(ctx context.Context, method, path string, input json.RawMessage) (out interface{}, err error)
Call sends the input to the endpoint and returns the result.
func (*API) GetMethodsForPath ¶
GetMethodsForPath returns the list of valid methods for a specified path (for use in OPTIONS requests).
func (*API) HTTPProxy ¶
func (api *API) HTTPProxy(w http.ResponseWriter, r *http.Request)
HTTPProxy is a handler function suitable for use in http.HandleFunc. For example:
http.HandleFunc("/", api.HTTPProxy) log.Fatal(http.ListenAndServe(":8000", nil))
The provided handler takes care of access control headers, CORS requests, JSON marshalling, and error handling.
func (*API) LambdaProxy ¶
func (api *API) LambdaProxy(corsAllowedOrigin string) func(*events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error)
LambdaProxy returns a handler function suitable for use with github.com/aws/aws-lambda-go/lambda. For example:
import "github.com/aws/aws-lambda-go/lambda" func main() { lambda.Start(api.LambdaProxy("*")) }
The provided handler takes care of access control headers, CORS requests, JSON marshalling, and error handling.
type APIError ¶
APIError is an error that contains status code information as well as error text.
func NewAPIError ¶ added in v0.0.2
NewAPIError returns an APIError from the given HTTP status code and error string.
func NewAPIErrorFromStatus ¶ added in v0.0.2
NewAPIErrorFromStatus creates an APIError with text from an HTTP status code.
type APIPath ¶
An APIPath represents a specified path and method, such as GET/users/{uuid}.
func NewAPIPath ¶
NewAPIPath creates an APIPath object from a path string, in the format GET/users/{uuid}.
type Endpoint ¶
type Endpoint struct { // Path is the API path string that will be exposed as an API endpoint. Must // be unique. // // The format of Path is METHOD/path/{pathvar}. Any path variables in curly // brace notation will be parsed during API.Call and passed to Handler as // a Context struct value. Path string // Handler must be a function that receives any single input variable, an // input variable of type Context, neither, or both. It can return one // output variable of any time, an error, neither, or both in the order // (output, error). // // The input value for Handler, if not Context, will automatically be // unmarshalled from the input to API.Call. Handler interface{} // PreRequestHook is a middleware hook that runs before the handler. If the // hook returns an error, that error will be returned and the handler will // not be called. PreRequestHooks []MiddlewareHook // contains filtered or unexported fields }
An Endpoint represents an API procedure.
type EndpointInput ¶
EndpointInput represents the input to an endpoint call. These inputs can be modified by middleware hooks.
type MiddlewareHook ¶
type MiddlewareHook func(*EndpointInput) (*EndpointInput, error)
MiddlewareHook is a function type that is called for each request.
These hooks are functions which will be called before the endpoint handler is called, and can choose to modify the method, path, context, or input of the endpoint before it is passed along. If the hook returns an error, execution of the endpoint will halt. This is useful for things like authentication checks, which must happen before the function is triggered, and must be able to return early if a call isn't authorized.