github.com/mweagle/Sparta@v1.15.0/doc_newmaingateway_http_test.go (about)

     1  package sparta
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/aws/aws-lambda-go/lambdacontext"
     8  	gocf "github.com/mweagle/go-cloudformation"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  // NOTE: your application MUST use `package main` and define a `main()` function.  The
    13  // example text is to make the documentation compatible with godoc.
    14  func echoAPIGatewayHTTPEvent(ctx context.Context,
    15  	props map[string]interface{}) error {
    16  	lambdaCtx, _ := lambdacontext.FromContext(ctx)
    17  	logger, _ := ctx.Value(ContextKeyLogger).(*logrus.Logger)
    18  	logger.WithFields(logrus.Fields{
    19  		"RequestID":  lambdaCtx.AwsRequestID,
    20  		"Properties": props,
    21  	}).Info("Lambda event")
    22  	return nil
    23  }
    24  
    25  // Should be main() in your application
    26  func ExampleMain_apiGatewayHTTPSEvent() {
    27  
    28  	// Create the MyEchoAPI API Gateway, with stagename /test.  The associated
    29  	// Stage reesource will cause the API to be deployed.
    30  	stage := NewStage("v1")
    31  	apiGateway := NewAPIGateway("MyEchoHTTPAPI", stage)
    32  	// Enable CORS
    33  	apiGateway.CORSOptions = &CORSOptions{
    34  		Headers: map[string]interface{}{
    35  			"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key",
    36  			"Access-Control-Allow-Methods": "*",
    37  			"Access-Control-Allow-Origin":  gocf.String("*"),
    38  		},
    39  	}
    40  	// Create a lambda function
    41  	echoAPIGatewayLambdaFn, _ := NewAWSLambda(LambdaName(echoAPIGatewayHTTPEvent),
    42  		echoAPIGatewayHTTPEvent,
    43  		IAMRoleDefinition{})
    44  
    45  	// Associate a URL path component with the Lambda function
    46  	apiGatewayResource, _ := apiGateway.NewResource("/echoHelloWorld", echoAPIGatewayLambdaFn)
    47  
    48  	// Associate 1 or more HTTP methods with the Resource.
    49  	method, err := apiGatewayResource.NewMethod("GET", http.StatusOK)
    50  	if err != nil {
    51  		panic("Failed to create NewMethod")
    52  	}
    53  	// Whitelist query parameters that should be passed to lambda function
    54  	method.Parameters["method.request.querystring.myKey"] = true
    55  	method.Parameters["method.request.querystring.myOtherKey"] = true
    56  
    57  	// Start
    58  	Main("HelloWorldLambdaHTTPSService", "Description for Hello World HTTPS Lambda", []*LambdaAWSInfo{echoAPIGatewayLambdaFn}, apiGateway, nil)
    59  }