-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (75 loc) · 2.11 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
80
81
82
83
84
85
package main
import (
"context"
"log"
"os"
"cloud.google.com/go/pubsub"
firebase "firebase.google.com/go"
)
func main() {
// Find and initialize the runtime configuration variables
env := retrieveEnvironmentRunParams()
ctx := context.Background()
// Initialize the pubsub client
pubSubClient, err := pubsub.NewClient(ctx, env.pubsubProjectId)
if err != nil {
log.Fatal(err)
}
defer pubSubClient.Close()
// Initialize the firebase client
conf := &firebase.Config{ProjectID: env.firestoreProjectId}
app, err := firebase.NewApp(ctx, conf)
if err != nil {
log.Fatalln(err)
}
firestoreClient, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer firestoreClient.Close()
// Attach to the subscription
sub := pubSubClient.Subscription(env.subscription)
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
_, _, err := firestoreClient.Collection(env.firestoreCollection).Add(ctx, map[string]interface{}{
"messageId": m.ID,
"attributes": m.Attributes,
"orderingKey": m.OrderingKey,
"publishTime": m.PublishTime,
"payload": m.Data,
"deliveryAttempt": m.DeliveryAttempt,
})
if err != nil {
log.Fatalf("Failed adding message: %v", err)
m.Nack()
}
m.Ack()
})
if err != nil {
log.Println(err)
}
}
func retrieveEnvironmentRunParams() environment {
projectId, found := os.LookupEnv("PROJECT_ID")
if !found {
panic("PROJECT_ID not found. Exiting.")
}
subscription, found := os.LookupEnv("SUBSCRIPTION")
if !found {
panic("SUBSCRIPTION not found. Exiting.")
}
firestoreCollection, found := os.LookupEnv("FIRESTORE_COLLECTION")
if !found {
panic("FIRESTORE_COLLECTION not found. Exiting.")
}
firestoreProjectId, found := os.LookupEnv("FIRESTORE_PROJECT_ID")
if !found {
panic("FIRESTORE_PROJECT_ID not found. Exiting.")
}
return environment{pubsubProjectId: projectId, firestoreProjectId: firestoreProjectId, subscription: subscription, firestoreCollection: firestoreCollection}
}
type environment struct {
pubsubProjectId string
firestoreProjectId string
subscription string
firestoreCollection string
}