-
Notifications
You must be signed in to change notification settings - Fork 218
/
main.go
79 lines (61 loc) · 1.47 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"encoding/json"
"log"
"os"
"cloud.google.com/go/pubsub"
"github.com/kelseyhightower/envconfig"
)
/*
To view: gcloud pubsub subscriptions pull --auto-ack foo
To post: gcloud pubsub topics publish demo_cloudevents --message '{"id":123,"message":"hi from the terminal"}'
*/
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 {
ID 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)
}
ctx := context.Background()
client, err := pubsub.NewClient(ctx, env.ProjectID)
if err != nil {
log.Fatal(err)
}
topic := client.Topic(env.TopicID)
// Create the topic if it doesn't exist.
exists, err := topic.Exists(ctx)
if err != nil {
log.Fatal(err)
}
if !exists {
log.Printf("Topic %v doesn't exist - creating it", env.TopicID)
_, err = client.CreateTopic(ctx, env.TopicID)
if err != nil {
log.Fatal(err)
}
}
data := &Example{
ID: 123,
Message: "Hello, World!",
}
b, err := json.Marshal(data)
if err != nil {
log.Fatalln(err)
}
msg := &pubsub.Message{
Data: b,
}
if _, err := topic.Publish(ctx, msg).Get(ctx); err != nil {
log.Printf("Could not publish message: %v", err)
}
}