Documentation
¶
Overview ¶
Package apigatewayrouter is a basic router for APIGateway-triggered Lambda functions. Each route defined in the router needs to be replicated into the APIGateway configuration.
Example ¶
// Initialise a new Router.
r := NewRouter()
// Add a single Route to it.
r.AddRoute("test", &Route{
Match: func(req events.APIGatewayProxyRequest) bool {
return req.Path == "/test"
},
Handle: func(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Println("Current route:", r.CurrentRouteName)
return events.APIGatewayProxyResponse{
Body: "OK",
StatusCode: 200,
}, nil
},
})
// Start handling request events.
lambda.Start(r.Handle)
Index ¶
- type HandleFunc
- type MatchFunc
- type Route
- type Router
- func (r *Router) AddRegExpRoute(name string, method string, re *regexp.Regexp, handler HandleFunc) *Router
- func (r *Router) AddRoute(name string, route *Route) *Router
- func (r *Router) AddStaticRoute(name string, method string, uri string, handler HandleFunc) *Router
- func (r *Router) Handle(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandleFunc ¶
type HandleFunc func(events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
HandleFunc handles a matched APIGateway request event.
type MatchFunc ¶
type MatchFunc func(events.APIGatewayProxyRequest) bool
MatchFunc checks if the APIGateway request event can be handled.
type Route ¶
type Route struct {
Match MatchFunc
Handle HandleFunc
}
Route determines if it can handle an APIGateway request event and handles it if possible.
type Router ¶
type Router struct {
CurrentRouteName string
Routes map[string]*Route
NotFound HandleFunc
}
Router stores a map of named routes which will be looped over to find the first matching Route.
func (*Router) AddRegExpRoute ¶
func (r *Router) AddRegExpRoute(name string, method string, re *regexp.Regexp, handler HandleFunc) *Router
AddRegExpRoute creates a MatchFunc using a regular expression matcher then adds it and the handler to the Router using AddRoute.
func (*Router) AddRoute ¶
AddRoute puts the defined Route into the Router. There is no clash detection for route names, if the same name string is used multiple times then only the most recent Route value is used.
This is also used internally by the other `Add*Route` functions.
func (*Router) AddStaticRoute ¶
AddStaticRoute creates a MatchFunc for an exact path match then adds it and the handler to the Router using AddRoute.
func (*Router) Handle ¶
func (r *Router) Handle(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
Handle is the routing part of the Router, it is responsible for finding a matching Route and executing it. If no matching routes are found, an error is triggered.
This function is a valid handler for use in lambda.Start - for example:-
r := NewRouter() // configure the router's routes... lambda.Start(r.Handle)