forked from mchmarny/dapr-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·115 lines (92 loc) · 2.73 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"gopkg.in/olahol/melody.v1"
"github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/http"
"github.com/pkg/errors"
)
var (
// AppVersion will be overritten during build
AppVersion = "v0.0.1-default"
// service
logger = log.New(os.Stdout, "", 0)
address = getEnvVar("ADDRESS", ":8084")
pubSubName = getEnvVar("PUBSUB_NAME", "processed-tweets-pubsub")
topicName = getEnvVar("TOPIC_NAME", "processed-tweets")
broadcaster *melody.Melody
templates *template.Template
)
func main() {
// server mux
mux := http.NewServeMux()
// static content
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("resource/static"))))
mux.HandleFunc("/favicon.ico", faviconHandler)
// tempalates
templates = template.Must(template.ParseGlob("resource/template/*"))
// websocket upgrade
broadcaster = melody.New()
broadcaster.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// other handlers
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/ws", wsHandler)
// create a Dapr service
s := daprd.NewServiceWithMux(address, mux)
// add some topic subscriptions
subscription := &common.Subscription{
PubsubName: pubSubName,
Topic: topicName,
Route: fmt.Sprintf("/%s", topicName),
}
if err := s.AddTopicEventHandler(subscription, eventHandler); err != nil {
logger.Fatalf("error adding topic subscription: %v", err)
}
// start the service
if err := s.Start(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("error starting service: %v", err)
}
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
broadcaster.HandleRequest(w, r)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
proto := r.Header.Get("x-forwarded-proto")
if proto == "" {
proto = "http"
}
data := map[string]string{
"host": r.Host,
"proto": proto,
"version": AppVersion,
}
err := templates.ExecuteTemplate(w, "index", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./resource/static/img/favicon.ico")
}
func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
b, err := json.Marshal(e.Data)
if err != nil {
return false, errors.Wrap(err, "error marshaling data")
}
broadcaster.Broadcast(b)
return false, nil
}
func getEnvVar(key, fallbackValue string) string {
if val, ok := os.LookupEnv(key); ok {
return strings.TrimSpace(val)
}
return fallbackValue
}