cloudeventsaws

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2020 License: MIT Imports: 3 Imported by: 7

README

cloudevents-aws-transport

Publish/Receive CloudEvents messages using AWS SNS/SQS

SNS Example

package main

import (
	"context"
	"fmt"
	"log"

	cloudevents "github.com/cloudevents/sdk-go"
	transport "github.com/jakubknejzlik/cloudevents-aws-transport"
)

func Receive(event cloudevents.Event) {
	log.Printf("? %v", event)
	// do something with event.Context and event.Data (via event.DataAs(foo)
}

func main() {
	t, err := transport.NewSNSTransport("arn:aws:sns:eu-central-1:123456789:test")
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}
	c, err := cloudevents.NewClient(t)
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}

    event := cloudevents.NewEvent()
    event.SetID(fmt.Sprintf("test123 %d", i))
    event.SetType("com.cloudevents.readme.sent")
    event.SetSource("http://localhost:8080/")
    event.SetData(fmt.Sprintf("hello world %d", i))
    log.Println(c.Send(context.Background(), event))

	log.Fatal(c.StartReceiver(context.Background(), Receive))
}

SQS Example

package main

import (
	"context"
	"fmt"
	"log"

	cloudevents "github.com/cloudevents/sdk-go"
	transport "github.com/jakubknejzlik/cloudevents-aws-transport"
)

func Receive(event cloudevents.Event) {
	log.Printf("? %v", event)
	// do something with event.Context and event.Data (via event.DataAs(foo)
}

func main() {
	t, err := transport.NewSQSTransport("https://sqs.eu-central-1.amazonaws.com/123456789/sqs-queue-test")
	if err != nil {
		log.Fatalf("failed to create transport, %v", err)
	}
	c, err := cloudevents.NewClient(t)
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}

    // send message
    event := cloudevents.NewEvent()
    event.SetID(fmt.Sprintf("test123 %d", i))
    event.SetType("com.cloudevents.readme.sent")
    event.SetSource("http://localhost:8080/")
    event.SetData(fmt.Sprintf("hello world %d", i))
    c.Send(context.Background(), event)

    // start receiver
	log.Fatal(c.StartReceiver(context.Background(), Receive))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SQS
	NewSQSTransport = sqs.New

	// SNS
	NewSNSTransport = sns.New
	WithPort        = sns.WithPort
	WithPath        = sns.WithPath

	// EventBridge
	NewEventBridgeTransport = eventbridge.New
)

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL