forked from mweagle/Sparta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_newmaingateway_http_test.go
59 lines (51 loc) · 2.03 KB
/
doc_newmaingateway_http_test.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
package sparta
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/lambdacontext"
gocf "github.com/mweagle/go-cloudformation"
"github.com/sirupsen/logrus"
)
// NOTE: your application MUST use `package main` and define a `main()` function. The
// example text is to make the documentation compatible with godoc.
func echoAPIGatewayHTTPEvent(ctx context.Context,
props map[string]interface{}) error {
lambdaCtx, _ := lambdacontext.FromContext(ctx)
logger, _ := ctx.Value(ContextKeyLogger).(*logrus.Logger)
logger.WithFields(logrus.Fields{
"RequestID": lambdaCtx.AwsRequestID,
"Properties": props,
}).Info("Lambda event")
return nil
}
// Should be main() in your application
func ExampleMain_apiGatewayHTTPSEvent() {
// Create the MyEchoAPI API Gateway, with stagename /test. The associated
// Stage reesource will cause the API to be deployed.
stage := NewStage("v1")
apiGateway := NewAPIGateway("MyEchoHTTPAPI", stage)
// Enable CORS
apiGateway.CORSOptions = &CORSOptions{
Headers: map[string]interface{}{
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Origin": gocf.String("*"),
},
}
// Create a lambda function
echoAPIGatewayLambdaFn, _ := NewAWSLambda(LambdaName(echoAPIGatewayHTTPEvent),
echoAPIGatewayHTTPEvent,
IAMRoleDefinition{})
// Associate a URL path component with the Lambda function
apiGatewayResource, _ := apiGateway.NewResource("/echoHelloWorld", echoAPIGatewayLambdaFn)
// Associate 1 or more HTTP methods with the Resource.
method, err := apiGatewayResource.NewMethod("GET", http.StatusOK)
if err != nil {
panic("Failed to create NewMethod")
}
// Whitelist query parameters that should be passed to lambda function
method.Parameters["method.request.querystring.myKey"] = true
method.Parameters["method.request.querystring.myOtherKey"] = true
// Start
Main("HelloWorldLambdaHTTPSService", "Description for Hello World HTTPS Lambda", []*LambdaAWSInfo{echoAPIGatewayLambdaFn}, apiGateway, nil)
}