This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filename: cmd/main.go | |
package main | |
import ( | |
“io” | |
“net/http” | |
“os” | |
sparta “github.com/mweagle/Sparta” | |
) | |
// http.HandlerFunc style | |
func helloWorld(w http.ResponseWriter, r *http.Request) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Simple Sparta application that demonstrates core functionality | |
Usage: | |
main [command] | |
Available Commands: | |
delete Delete service | |
describe Describe service | |
execute Execute | |
help Help about any command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AWS Lambda Go style | |
func helloWorld() (string, error) { | |
return "Hello World", nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lambdaFn := sparta.HandleAWSLambda(“Hello World”, | |
helloWorld, | |
sparta.IAMRoleDefinition{}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AWS Lambda go style | |
func helloWorld() string { | |
return “Hello World” | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type helloWorldResponse struct { | |
Message string | |
Request spartaAPIG.APIGatewayRequest | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// Hello world event handler | |
func helloWorld(ctx context.Context, | |
gatewayEvent spartaAPIG.APIGatewayRequest) (helloWorldResponse, error) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Register the function with the API Gateway | |
apiStage := sparta.NewStage("v1") | |
apiGateway := sparta.NewAPIGateway("SpartaHTML", apiStage) | |
// Enable CORS s.t. the S3 site can access the resources | |
apiGateway.CORSOptions = &sparta.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.GetAtt(s3Site.CloudFormationS3ResourceName(), "WebsiteURL"), | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if nil != api { | |
apiGatewayResource, _ := api.NewResource("/hello", lambdaFn) | |
// We only return http.StatusOK | |
apiMethod, apiMethodErr := apiGatewayResource.NewMethod("GET", | |
http.StatusOK, | |
http.StatusOK) | |
if nil != apiMethodErr { | |
panic("Failed to create /hello resource: " + apiMethodErr.Error()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Deploy it | |
stackName := spartaCF.UserScopedStackName("SpartaHTML") | |
sparta.Main(stackName, | |
fmt.Sprintf("SpartaHTML provisions a static S3 hosted website with an API Gateway resource backed by a custom Lambda function"), | |
spartaHTMLLambdaFunctions(apiGateway), | |
apiGateway, | |
s3Site) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO[0000] ════════════════════════════════════════════════ | |
INFO[0000] ╔═╗┌─┐┌─┐┬─┐┌┬┐┌─┐ Version : 1.0.1 | |
INFO[0000] ╚═╗├─┘├─┤├┬┘ │ ├─┤ SHA : 840ab43 | |
INFO[0000] ╚═╝┴ ┴ ┴┴└─ ┴ ┴ ┴ Go : go1.9.2 | |
INFO[0000] ════════════════════════════════════════════════ | |
INFO[0000] Service: SpartaHTML-mweagle LinkFlags= Option=provision UTC="2018-01-22T02:34:47Z" | |
INFO[0000] ════════════════════════════════════════════════ | |
INFO[0000] Provisioning service BuildID=d64f878f9a00d1300e3c8f2ec60cff856487de61 CodePipelineTrigger= InPlaceUpdates=false NOOP=false Tags= | |
INFO[0000] Verifying IAM Lambda execution roles | |
INFO[0000] IAM roles verified Count=1 |