github.com/mweagle/Sparta@v1.15.0/decorator/apigateway_domain_test.go (about) 1 package decorator 2 3 import ( 4 "context" 5 "net/http" 6 "testing" 7 8 sparta "github.com/mweagle/Sparta" 9 spartaCF "github.com/mweagle/Sparta/aws/cloudformation" 10 spartaAWSEvents "github.com/mweagle/Sparta/aws/events" 11 spartaTesting "github.com/mweagle/Sparta/testing" 12 gocf "github.com/mweagle/go-cloudformation" 13 ) 14 15 func TestAPIGatewayCustomDomain(t *testing.T) { 16 helloWorld := func(ctx context.Context, 17 gatewayEvent spartaAWSEvents.APIGatewayRequest) (interface{}, error) { 18 return "Hello World", nil 19 } 20 lambdaFuncs := func(api *sparta.API) []*sparta.LambdaAWSInfo { 21 var lambdaFunctions []*sparta.LambdaAWSInfo 22 lambdaFn := sparta.HandleAWSLambda(sparta.LambdaName(helloWorld), 23 helloWorld, 24 sparta.IAMRoleDefinition{}) 25 apiGatewayResource, _ := api.NewResource("/hello", lambdaFn) 26 27 // We only return http.StatusOK 28 apiMethod, apiMethodErr := apiGatewayResource.NewMethod("GET", 29 http.StatusOK, 30 http.StatusInternalServerError) 31 if nil != apiMethodErr { 32 panic("Failed to create /hello resource: " + apiMethodErr.Error()) 33 } 34 // The lambda resource only supports application/json Unmarshallable 35 // requests. 36 apiMethod.SupportedRequestContentTypes = []string{"application/json"} 37 return append(lambdaFunctions, lambdaFn) 38 } 39 40 apigatewayHooks := func(apiGateway *sparta.API) *sparta.WorkflowHooks { 41 hooks := &sparta.WorkflowHooks{} 42 43 serviceDecorator := APIGatewayDomainDecorator(apiGateway, 44 gocf.String("arn:aws:acm:us-west-2:123412341234:certificate/6486C3FF-A3B7-46B6-83A0-9AE329FEC4E3"), 45 "", // Optional base path value 46 "noice.spartademo.net") 47 hooks.ServiceDecorators = []sparta.ServiceDecoratorHookHandler{ 48 serviceDecorator, 49 } 50 return hooks 51 } 52 53 // Register the function with the API Gateway 54 apiStage := sparta.NewStage("v1") 55 56 apiGateway := sparta.NewAPIGateway("SpartaHTMLDomain", apiStage) 57 apiGateway.EndpointConfiguration = &gocf.APIGatewayRestAPIEndpointConfiguration{ 58 Types: gocf.StringList( 59 gocf.String("REGIONAL"), 60 ), 61 } 62 hooks := apigatewayHooks(apiGateway) 63 // Deploy it 64 spartaTesting.ProvisionEx(t, 65 lambdaFuncs(apiGateway), 66 apiGateway, 67 nil, 68 hooks, 69 false, 70 nil) 71 } 72 73 func ExampleAPIGatewayDomainDecorator() { 74 helloWorld := func(ctx context.Context, 75 gatewayEvent spartaAWSEvents.APIGatewayRequest) (interface{}, error) { 76 return "Hello World", nil 77 } 78 lambdaFuncs := func(api *sparta.API) []*sparta.LambdaAWSInfo { 79 var lambdaFunctions []*sparta.LambdaAWSInfo 80 lambdaFn := sparta.HandleAWSLambda(sparta.LambdaName(helloWorld), 81 helloWorld, 82 sparta.IAMRoleDefinition{}) 83 apiGatewayResource, _ := api.NewResource("/hello", lambdaFn) 84 85 // We only return http.StatusOK 86 apiMethod, apiMethodErr := apiGatewayResource.NewMethod("GET", 87 http.StatusOK, 88 http.StatusInternalServerError) 89 if nil != apiMethodErr { 90 panic("Failed to create /hello resource: " + apiMethodErr.Error()) 91 } 92 // The lambda resource only supports application/json Unmarshallable 93 // requests. 94 apiMethod.SupportedRequestContentTypes = []string{"application/json"} 95 return append(lambdaFunctions, lambdaFn) 96 } 97 98 apigatewayHooks := func(apiGateway *sparta.API) *sparta.WorkflowHooks { 99 hooks := &sparta.WorkflowHooks{} 100 101 serviceDecorator := APIGatewayDomainDecorator(apiGateway, 102 gocf.String("arn:aws:acm:us-west-2:123412341234:certificate/6486C3FF-A3B7-46B6-83A0-9AE329FEC4E3"), 103 "", // Optional base path value 104 "noice.spartademo.net") 105 hooks.ServiceDecorators = []sparta.ServiceDecoratorHookHandler{ 106 serviceDecorator, 107 } 108 return hooks 109 } 110 111 // Register the function with the API Gateway 112 apiStage := sparta.NewStage("v1") 113 114 apiGateway := sparta.NewAPIGateway("SpartaHTMLDomain", apiStage) 115 apiGateway.EndpointConfiguration = &gocf.APIGatewayRestAPIEndpointConfiguration{ 116 Types: gocf.StringList( 117 gocf.String("REGIONAL"), 118 ), 119 } 120 hooks := apigatewayHooks(apiGateway) 121 // Deploy it 122 stackName := spartaCF.UserScopedStackName("CustomAPIGateway") 123 sparta.MainEx(stackName, 124 "CustomAPIGateway defines a stack with a custom APIGateway Domain Name", 125 lambdaFuncs(apiGateway), 126 apiGateway, 127 nil, 128 hooks, 129 false) 130 }