2018-07-13 21:12:31 +00:00
|
|
|
package lambdarouter
|
|
|
|
|
|
|
|
import (
|
2018-08-03 07:01:13 +00:00
|
|
|
"context"
|
2018-07-13 21:12:31 +00:00
|
|
|
"encoding/json"
|
2018-07-18 21:04:18 +00:00
|
|
|
"log"
|
2018-07-13 21:12:31 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
radix "github.com/armon/go-radix"
|
|
|
|
"github.com/aws/aws-lambda-go/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-07-14 18:32:18 +00:00
|
|
|
post = http.MethodPost
|
|
|
|
get = http.MethodGet
|
|
|
|
put = http.MethodPut
|
2018-07-14 21:58:30 +00:00
|
|
|
patch = http.MethodPatch
|
2018-07-14 18:32:18 +00:00
|
|
|
delete = http.MethodDelete
|
2018-07-13 21:12:31 +00:00
|
|
|
)
|
|
|
|
|
2018-07-17 21:01:18 +00:00
|
|
|
// APIGContext is used as the input and output of handler functions.
|
|
|
|
// The Body, Claims, Path, and QryStr will be populated by the the APIGatewayProxyRequest.
|
2018-07-14 21:58:30 +00:00
|
|
|
// The Request itself is also passed through if you need further access.
|
2018-07-17 21:01:18 +00:00
|
|
|
// Fill the Status and Body, or Status and Error to respond.
|
|
|
|
type APIGContext struct {
|
2018-07-14 18:32:18 +00:00
|
|
|
Claims map[string]interface{}
|
2018-08-03 07:01:13 +00:00
|
|
|
Context context.Context
|
2018-07-13 21:12:31 +00:00
|
|
|
Path map[string]string
|
|
|
|
QryStr map[string]string
|
|
|
|
Request *events.APIGatewayProxyRequest
|
2018-07-17 21:01:18 +00:00
|
|
|
Status int
|
|
|
|
Body []byte
|
|
|
|
Err error
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// APIGHandler is the interface a handler function must implement to be used
|
|
|
|
// with Get, Post, Put, Patch, and Delete.
|
2018-07-17 21:01:18 +00:00
|
|
|
type APIGHandler func(ctx *APIGContext)
|
2018-07-13 21:12:31 +00:00
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// APIGRouter is the object that handlers build upon and is used in the end to respond.
|
|
|
|
type APIGRouter struct {
|
2018-07-13 21:12:31 +00:00
|
|
|
request *events.APIGatewayProxyRequest
|
|
|
|
endpoints map[string]*radix.Tree
|
2018-09-24 23:59:18 +00:00
|
|
|
params map[string]interface{}
|
2018-07-19 02:52:14 +00:00
|
|
|
prefix string
|
|
|
|
headers map[string]string
|
2018-08-03 07:01:13 +00:00
|
|
|
context context.Context
|
2018-07-19 02:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// APIGRouterConfig is used as the input to NewAPIGRouter, request is your incoming
|
|
|
|
// apig request and prefix will be stripped of all incoming request paths. Headers
|
|
|
|
// will be sent with all responses.
|
|
|
|
type APIGRouterConfig struct {
|
2018-08-03 07:01:13 +00:00
|
|
|
Context context.Context
|
2018-07-19 02:52:14 +00:00
|
|
|
Request *events.APIGatewayProxyRequest
|
|
|
|
Prefix string
|
|
|
|
Headers map[string]string
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Begin router methods.
|
|
|
|
|
2018-07-19 02:52:14 +00:00
|
|
|
// NewAPIGRouter creates a new router using the given router config.
|
|
|
|
func NewAPIGRouter(cfg *APIGRouterConfig) *APIGRouter {
|
2018-07-14 21:58:30 +00:00
|
|
|
return &APIGRouter{
|
2018-07-19 02:52:14 +00:00
|
|
|
request: cfg.Request,
|
2018-07-13 21:12:31 +00:00
|
|
|
endpoints: map[string]*radix.Tree{
|
2018-07-14 18:32:18 +00:00
|
|
|
post: radix.New(),
|
|
|
|
get: radix.New(),
|
|
|
|
put: radix.New(),
|
2018-07-14 21:58:30 +00:00
|
|
|
patch: radix.New(),
|
2018-07-14 18:32:18 +00:00
|
|
|
delete: radix.New(),
|
2018-07-13 21:12:31 +00:00
|
|
|
},
|
2018-09-24 23:59:18 +00:00
|
|
|
params: map[string]interface{}{},
|
2018-07-19 02:52:14 +00:00
|
|
|
prefix: cfg.Prefix,
|
|
|
|
headers: cfg.Headers,
|
2018-08-03 18:07:37 +00:00
|
|
|
context: cfg.Context,
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// Get creates a new get endpoint.
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) Get(route string, handlers ...APIGHandler) {
|
|
|
|
r.addEndpoint(get, route, handlers)
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// Post creates a new post endpoint.
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) Post(route string, handlers ...APIGHandler) {
|
|
|
|
r.addEndpoint(post, route, handlers)
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// Put creates a new put endpoint.
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) Put(route string, handlers ...APIGHandler) {
|
|
|
|
r.addEndpoint(put, route, handlers)
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// Patch creates a new patch endpoint
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) Patch(route string, handlers ...APIGHandler) {
|
|
|
|
r.addEndpoint(patch, route, handlers)
|
2018-07-14 21:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete creates a new delete endpoint.
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) Delete(route string, handlers ...APIGHandler) {
|
|
|
|
r.addEndpoint(delete, route, handlers)
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:58:30 +00:00
|
|
|
// Respond returns an APIGatewayProxyResponse to respond to the lambda request.
|
|
|
|
func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
|
2018-07-13 21:12:31 +00:00
|
|
|
var (
|
2018-07-16 23:44:38 +00:00
|
|
|
handlersInterface interface{}
|
|
|
|
ok bool
|
|
|
|
status int
|
|
|
|
respbody []byte
|
|
|
|
err error
|
2018-07-13 21:12:31 +00:00
|
|
|
|
|
|
|
endpointTree = r.endpoints[r.request.HTTPMethod]
|
2018-07-19 02:52:14 +00:00
|
|
|
path = strings.TrimPrefix(r.request.Path, r.prefix)
|
2018-07-13 21:12:31 +00:00
|
|
|
response = events.APIGatewayProxyResponse{}
|
2018-07-18 23:15:07 +00:00
|
|
|
splitPath = stripSlashesAndSplit(path)
|
2018-07-13 21:12:31 +00:00
|
|
|
)
|
|
|
|
|
2018-09-24 23:59:18 +00:00
|
|
|
for p := range r.params {
|
|
|
|
if r.request.PathParameters[p] != "" {
|
|
|
|
pval := r.request.PathParameters[p]
|
2018-07-18 23:15:07 +00:00
|
|
|
for i, v := range splitPath {
|
|
|
|
if v == pval {
|
2018-09-24 23:59:18 +00:00
|
|
|
splitPath[i] = "{" + p + "}"
|
|
|
|
break
|
2018-07-18 23:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 23:15:07 +00:00
|
|
|
path = "/" + strings.Join(splitPath, "/")
|
2018-07-13 21:12:31 +00:00
|
|
|
|
2018-07-16 23:44:38 +00:00
|
|
|
if handlersInterface, ok = endpointTree.Get(path); !ok {
|
2018-09-24 23:59:18 +00:00
|
|
|
respbody, _ = json.Marshal(map[string]string{"error": "no route matching path found"})
|
2018-07-13 21:12:31 +00:00
|
|
|
|
|
|
|
response.StatusCode = http.StatusNotFound
|
|
|
|
response.Body = string(respbody)
|
2018-08-30 20:07:44 +00:00
|
|
|
response.Headers = r.headers
|
2018-07-13 21:12:31 +00:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2018-07-16 23:44:38 +00:00
|
|
|
handlers := handlersInterface.([]APIGHandler)
|
2018-07-13 21:12:31 +00:00
|
|
|
|
2018-07-16 23:44:38 +00:00
|
|
|
for _, handler := range handlers {
|
2018-07-17 21:01:18 +00:00
|
|
|
ctx := &APIGContext{
|
|
|
|
Body: []byte(r.request.Body),
|
2018-07-16 23:44:38 +00:00
|
|
|
Path: r.request.PathParameters,
|
|
|
|
QryStr: r.request.QueryStringParameters,
|
|
|
|
Request: r.request,
|
2018-08-03 07:01:13 +00:00
|
|
|
Context: r.context,
|
2018-07-16 23:44:38 +00:00
|
|
|
}
|
|
|
|
if r.request.RequestContext.Authorizer["claims"] != nil {
|
2018-07-17 21:01:18 +00:00
|
|
|
ctx.Claims = r.request.RequestContext.Authorizer["claims"].(map[string]interface{})
|
2018-07-16 23:44:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:01:18 +00:00
|
|
|
handler(ctx)
|
|
|
|
status, respbody, err = ctx.respDeconstruct()
|
2018-07-16 23:44:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-24 23:59:18 +00:00
|
|
|
respbody, _ = json.Marshal(map[string]string{"error": err.Error()})
|
2018-07-16 23:44:38 +00:00
|
|
|
if strings.Contains(err.Error(), "record not found") {
|
|
|
|
status = 204
|
2018-08-30 20:07:44 +00:00
|
|
|
} else if status != 204 && status < 400 {
|
2018-07-16 23:44:38 +00:00
|
|
|
status = 400
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:04:18 +00:00
|
|
|
log.Printf("%v error: %v", status, err.Error())
|
2018-07-16 23:44:38 +00:00
|
|
|
response.StatusCode = status
|
|
|
|
response.Body = string(respbody)
|
2018-08-30 20:07:44 +00:00
|
|
|
response.Headers = r.headers
|
2018-07-16 23:44:38 +00:00
|
|
|
return response
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.StatusCode = status
|
|
|
|
response.Body = string(respbody)
|
2018-07-19 02:52:14 +00:00
|
|
|
response.Headers = r.headers
|
2018-07-13 21:12:31 +00:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Begin helper functions.
|
|
|
|
func stripSlashesAndSplit(s string) []string {
|
|
|
|
s = strings.TrimPrefix(s, "/")
|
|
|
|
s = strings.TrimSuffix(s, "/")
|
|
|
|
return strings.Split(s, "/")
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:01:18 +00:00
|
|
|
func (ctx *APIGContext) respDeconstruct() (int, []byte, error) {
|
|
|
|
return ctx.Status, ctx.Body, ctx.Err
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 23:44:38 +00:00
|
|
|
func (r *APIGRouter) addEndpoint(method string, route string, handlers []APIGHandler) {
|
|
|
|
if _, overwrite := r.endpoints[method].Insert(route, handlers); overwrite {
|
2018-07-13 21:12:31 +00:00
|
|
|
panic("endpoint already existent")
|
|
|
|
}
|
|
|
|
|
|
|
|
rtearr := stripSlashesAndSplit(route)
|
|
|
|
for _, v := range rtearr {
|
|
|
|
if strings.HasPrefix(v, "{") {
|
2018-09-24 23:59:18 +00:00
|
|
|
v = strings.TrimPrefix(v, "{")
|
|
|
|
v = strings.TrimSuffix(v, "}")
|
|
|
|
r.params[v] = nil // adding params as *unique* keys
|
2018-07-13 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|