-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
58 lines (41 loc) · 1.23 KB
/
app.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
package main
import (
"context"
"encoding/json"
"log"
"time"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
)
type App struct {
context context.Context
client *pubsub.Client
config Config
}
type Config struct {
context context.Context
gcpProjectName string
subscriptionName string
topicName string
options []option.ClientOption
}
func newApp(config Config) (*App, error) {
client, err := pubsub.NewClient(config.context, config.gcpProjectName, config.options...)
if err != nil {
return nil, err
}
return &App{context: config.context, client: client, config: config}, nil
}
func (app *App) run() {
log.Println("waiting for messages")
app.client.Subscription(app.config.subscriptionName).Receive(app.config.context, func(ctx context.Context, message *pubsub.Message) {
var messageJson map[string]interface{}
json.Unmarshal(message.Data, &messageJson)
log.Printf("received message with id: %s and content %v", message.ID, messageJson)
messageJson["processed_time"] = time.Now()
result, _ := json.Marshal(messageJson)
app.client.Topic(app.config.topicName).Publish(ctx, &pubsub.Message{Data: result})
message.Ack()
})
log.Println("stopped waiting for messages")
}