-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhandler_proxy.go
More file actions
54 lines (45 loc) · 1.85 KB
/
handler_proxy.go
File metadata and controls
54 lines (45 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// From https://github.com/awslabs/aws-lambda-go-api-proxy
// Aegis expands upon it a bit to include `Handler` as well
// as to export `HandlerFunc` field on `HandlerFuncAdapter`.
package framework
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
)
// HandlerFuncAdapter is an interface for adapting and handling Lambda APIGatewayProxyRequests to standard http Requests
type HandlerFuncAdapter struct {
core.RequestAccessor
HandlerFunc http.HandlerFunc
Handler http.Handler
}
// NewHandlerAdapter creates a new stdlib http.HandlerFunc adapter allowing the use of standard http request handling
func NewHandlerAdapter(handlerFunc http.HandlerFunc) *HandlerFuncAdapter {
return &HandlerFuncAdapter{
HandlerFunc: handlerFunc,
}
}
// Proxy will proxy Lambda APIGatewayProxyRequests through a standard http handler and return an APIGatewayProxyResponse
func (h *HandlerFuncAdapter) Proxy(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
req, err := h.ProxyEventToHTTPRequest(event)
// Set the context from the Lambda event on to the http.Request
req.WithContext(ctx)
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
}
w := core.NewProxyResponseWriter()
// a Handler could include middleware, ie. when using Alice in chains.
// Apollo is a fork there that may eventually be useful too.
// So the `Handler` is generic, it need not be the alice package.
if h.Handler != nil {
h.Handler.ServeHTTP(http.ResponseWriter(w), req)
} else {
h.HandlerFunc.ServeHTTP(http.ResponseWriter(w), req)
}
resp, err := w.GetProxyResponse()
if err != nil {
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
}
return resp, nil
}