forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
108 lines (90 loc) · 3.38 KB
/
event.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
// Event represents an event passed to an AWS Lambda function by the runtime
type Event struct {
HTTPMethod string `json:"httpMethod"`
Body string `json:"body"`
Resource string `json:"resource"`
RequestContext RequestContext `json:"requestContext"`
QueryStringParams map[string]string `json:"queryStringParameters"`
Headers map[string]string `json:"headers"`
PathParameters map[string]string `json:"pathParameters"`
StageVariables map[string]string `json:"stageVariables"`
Path string `json:"path"`
}
// RequestContext represents the context object that gets passed to an AWS Lambda function
type RequestContext struct {
ResourceID string `json:"resourceId,omitempty"`
APIID string `json:"apiId,omitempty"`
ResourcePath string `json:"resourcePath,omitempty"`
HTTPMethod string `json:"httpMethod,omitempty"`
RequestID string `json:"requestId,omitempty"`
AccountsID string `json:"accountId,omitempty"`
Stage string `json:"stage,omitempty"`
Identity ContextIdentity `json:"identity,omitempty"`
}
// ContextIdentity represents the identity section of the context object that gets passed to an AWS Lambda function
type ContextIdentity struct {
APIKey string `json:"apiKey,omitempty"`
UserARN string `json:"userArn,omitempty"`
CognitoAuthenticationType string `json:"cognitoAuthenticationType,omitempty"`
Caller string `json:"caller,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
User string `json:"user,omitempty"`
CognitoIdentityPoolID string `json:"cognitoIdentityPoolId,omitempty"`
CognitoIdentityID string `json:"cognitoIdentityId,omitempty"`
CognitoAuthenticationProvider string `json:"cognitoAuthenticationProvider,omitempty"`
SourceIP string `json:"sourceIp,omitempty"`
AccountID string `json:"accountId,omitempty"`
}
// NewEvent initalises and populates a new ApiEvent with
// event details from a http.Request
func NewEvent(req *http.Request) (*Event, error) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
headers := map[string]string{}
for name, values := range req.Header {
for _, value := range values {
headers[name] = value
}
}
query := map[string]string{}
for name, values := range req.URL.Query() {
for _, value := range values {
query[name] = value
}
}
pathParams := mux.Vars(req)
if len(pathParams) == 0 {
pathParams = nil
}
event := &Event{
HTTPMethod: req.Method,
Body: string(body),
Headers: headers,
QueryStringParams: query,
Path: req.URL.Path,
Resource: req.URL.Path,
PathParameters: pathParams,
}
event.RequestContext.Identity.SourceIP = req.RemoteAddr
event.RequestContext.ResourcePath = req.URL.Path
event.RequestContext.HTTPMethod = req.Method
event.RequestContext.Stage = "prod"
return event, nil
}
// JSON returns the event as a JSON string
func (e *Event) JSON() (string, error) {
data, err := json.Marshal(e)
if err != nil {
return "", err
}
return string(data), nil
}