-
Notifications
You must be signed in to change notification settings - Fork 216
/
main.go
61 lines (50 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"log"
"os"
"github.com/cloudevents/sdk-go"
cepubsub "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/pubsub"
"github.com/kelseyhightower/envconfig"
)
type envConfig struct {
ProjectID string `envconfig:"GOOGLE_CLOUD_PROJECT" required:"true"`
TopicID string `envconfig:"PUBSUB_TOPIC" default:"demo_cloudevents" required:"true"`
}
// Basic data struct.
type Example struct {
Sequence int `json:"id"`
Message string `json:"message"`
}
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Printf("[ERROR] Failed to process env var: %s", err)
os.Exit(1)
}
t, err := cepubsub.New(context.Background(),
cepubsub.WithProjectID(env.ProjectID),
cepubsub.WithTopicID(env.TopicID))
if err != nil {
log.Printf("failed to create pubsub transport, %s", err.Error())
os.Exit(1)
}
c, err := cloudevents.NewClient(t, cloudevents.WithTimeNow(), cloudevents.WithUUIDs())
if err != nil {
log.Printf("failed to create client, %s", err.Error())
os.Exit(1)
}
event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetType("com.cloudevents.sample.sent")
event.SetSource("github.com/cloudevents/sdk-go/cmd/samples/pubsub/sender/")
_ = event.SetData(&Example{
Sequence: 0,
Message: "HELLO",
})
_, _, err = c.Send(context.Background(), event)
if err != nil {
log.Printf("failed to send: %v", err)
os.Exit(1)
}
os.Exit(0)
}