forked from mchmarny/dapr-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (64 loc) · 2.1 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
dapr "github.com/dapr/go-sdk/client"
"github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/http"
"github.com/pkg/errors"
)
var (
logger = log.New(os.Stdout, "", 0)
address = getEnvVar("ADDRESS", ":8080")
pubSubName = getEnvVar("PUBSUB_NAME", "tweeter-pubsub")
topicName = getEnvVar("TOPIC_NAME", "tweets")
storeName = getEnvVar("STORE_NAME", "tweet-store")
client dapr.Client
)
func main() {
// create a Dapr service
s := daprd.NewService(address)
// create a Dapr client
c, err := dapr.NewClient()
if err != nil {
logger.Fatalf("error creating Dapr client: %v", err)
}
client = c
defer client.Close()
// add twitter input binding handler
if err := s.AddBindingInvocationHandler("tweets", tweetHandler); err != nil {
logger.Fatalf("error adding binding handler: %v", err)
}
// start the service
if err := s.Start(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("error starting service: %v", err)
}
}
func tweetHandler(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
logger.Printf("Tweet (query: %s, traceID: %s)", in.Metadata["Query"], in.Metadata["Traceparent"])
var m map[string]interface{}
if err := json.Unmarshal(in.Data, &m); err != nil {
return nil, errors.Wrap(err, "error deserializing event data")
}
k := fmt.Sprintf("tw-%s", m["id_str"])
if err := client.SaveState(ctx, storeName, k, in.Data); err != nil {
return nil, errors.Wrapf(err, "error saving to store:%s with key:%s", storeName, k)
}
logger.Printf("Tweet saved in store: %s: %s", storeName, k)
if err := client.PublishEvent(ctx, pubSubName, topicName, in.Data); err != nil {
return nil, errors.Wrapf(err, "error publishing to %s/%s", pubSubName, topicName)
}
logger.Printf("Tweet published to %s/%s", pubSubName, topicName)
return
}
func getEnvVar(key, fallbackValue string) string {
if val, ok := os.LookupEnv(key); ok {
return strings.TrimSpace(val)
}
return fallbackValue
}