-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (76 loc) · 2.07 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
package main
import (
"context"
"log"
"github.com/hidromatologia-v2/alerts/watcher"
"github.com/hidromatologia-v2/models"
"github.com/hidromatologia-v2/models/common/postgres"
"github.com/memphisdev/memphis.go"
uuid "github.com/satori/go.uuid"
"github.com/sethvargo/go-envconfig"
)
func logFatalErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func newConsumer(config *Config) *memphis.Consumer {
var connOpts []memphis.Option
if config.Consumer.Password != nil {
connOpts = append(connOpts, memphis.Password(*config.Consumer.Password))
}
if config.Consumer.ConnectionToken != nil {
connOpts = append(connOpts, memphis.ConnectionToken(*config.Consumer.ConnectionToken))
}
conn, connErr := memphis.Connect(
config.Consumer.Host,
config.Consumer.Username,
connOpts...,
)
logFatalErr(connErr)
consumer, consumerErr := conn.CreateConsumer(
config.Consumer.Station,
config.Consumer.Consumer+uuid.NewV4().String(),
)
logFatalErr(consumerErr)
return consumer
}
func newProducer(config *Config) *memphis.Producer {
var connOpts []memphis.Option
if config.Producer.Password != nil {
connOpts = append(connOpts, memphis.Password(*config.Producer.Password))
}
if config.Producer.ConnectionToken != nil {
connOpts = append(connOpts, memphis.ConnectionToken(*config.Producer.ConnectionToken))
}
conn, connErr := memphis.Connect(
config.Producer.Host,
config.Producer.Username,
connOpts...,
)
logFatalErr(connErr)
producer, producerErr := conn.CreateProducer(
config.Producer.Station,
config.Producer.Producer+uuid.NewV4().String(),
)
logFatalErr(producerErr)
return producer
}
func main() {
var config Config
eErr := envconfig.Process(context.Background(), &config)
logFatalErr(eErr)
controllerOpts := models.Options{
Database: postgres.New(config.Postgres.DSN),
}
srCons := newConsumer(&config)
msgProd := newProducer(&config)
w := &watcher.Watcher{
Controller: models.NewController(&controllerOpts),
SensorRegistryConsumer: srCons,
MessageProducer: msgProd,
}
rErr := w.Run()
logFatalErr(rErr)
<-make(chan struct{}, 1)
}