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

     1  package archetype
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	_ "net/http/pprof" // include pprop
     7  
     8  	awsLambdaEvents "github.com/aws/aws-lambda-go/events"
     9  	sparta "github.com/mweagle/Sparta"
    10  	gocf "github.com/mweagle/go-cloudformation"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // ExampleS3Reactor illustrates how to create an S3 event subscriber
    15  func ExampleS3Reactor() {
    16  	inlineReactor := func(ctx context.Context,
    17  		s3Event awsLambdaEvents.S3Event) (interface{}, error) {
    18  		logger, loggerOk := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
    19  		if loggerOk {
    20  			for _, eachRecord := range s3Event.Records {
    21  				logger.WithField("EventType", eachRecord.EventName).
    22  					WithField("Entity", eachRecord.S3).
    23  					Info("Event info")
    24  			}
    25  		}
    26  		return len(s3Event.Records), nil
    27  	}
    28  	// Create the *sparta.LambdaAWSInfo wrapper
    29  	lambdaFn, lambdaFnErr := NewS3Reactor(S3ReactorFunc(inlineReactor),
    30  		gocf.String("MY-S3-BUCKET-TO-REACT"),
    31  		nil)
    32  	fmt.Printf("LambdaFn: %#v, LambdaFnErr: %#v", lambdaFn, lambdaFnErr)
    33  }
    34  
    35  // ExampleSNSReactor illustrates how to create an SNS notification subscriber
    36  func ExampleSNSReactor() {
    37  	inlineReactor := func(ctx context.Context, snsEvent awsLambdaEvents.SNSEvent) (interface{}, error) {
    38  		logger, loggerOk := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
    39  		if loggerOk {
    40  			logger.WithFields(logrus.Fields{
    41  				"Event": snsEvent,
    42  			}).Info("Event received")
    43  		}
    44  		return &snsEvent, nil
    45  	}
    46  	// Create the *sparta.LambdaAWSInfo wrapper
    47  	lambdaFn, lambdaFnErr := NewSNSReactor(SNSReactorFunc(inlineReactor),
    48  		gocf.String("MY-SNS-TOPIC"),
    49  		nil)
    50  	fmt.Printf("LambdaFn: %#v, LambdaFnErr: %#v", lambdaFn, lambdaFnErr)
    51  }