-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.48 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"gopkg.in/redis.v3"
"github.com/nats-io/nats"
"github.com/r3labs/sse"
)
var s *sse.Server
var db *redis.Client
func main() {
// Open Nats connection
n, err := nats.Connect(os.Getenv("NATS_URI"))
if err != nil {
log.Println("Could not connect to nats")
return
}
defer n.Close()
redisCfg := redisConfig{}
msg, err := n.Request("config.get.redis", []byte(""), 1*time.Second)
if err != nil {
panic("Cant get redis config")
}
json.Unmarshal(msg.Data, &redisCfg)
// Create new SSE server
s = sse.New()
s.AutoStream = true
defer s.Close()
// Open DB connection
db = redis.NewClient(&redis.Options{
Addr: redisCfg.Host,
Password: redisCfg.Password,
DB: redisCfg.DB,
})
// Create new HTTP Server and add the route handler
mux := http.NewServeMux()
mux.HandleFunc("/events", authMiddleware)
// Start nats handler, subscribe to all events
n.Subscribe(">", natsHandler)
monitorCfg := monitorConfig{}
msg, err = n.Request("config.get.monitor", []byte(""), 1*time.Second)
if err != nil {
panic("Can't get monitor config")
}
json.Unmarshal(msg.Data, &monitorCfg)
// Start Listening
addr := fmt.Sprintf("%s:%s", monitorCfg.Host, monitorCfg.Port)
http.ListenAndServe(addr, mux)
}