-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (56 loc) · 1.5 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
package main
import (
"event-source-demo/internal/employee"
"event-source-demo/pkg/service"
"log"
"github.com/NYTimes/gizmo/config"
"github.com/NYTimes/gizmo/pubsub"
"github.com/NYTimes/gizmo/pubsub/kafka"
"github.com/NYTimes/gizmo/server"
badger "github.com/dgraph-io/badger/v2"
)
func main() {
var cfg struct {
Server *server.Config
Kafka *kafka.Config
}
// embebedd KV DB
db, err := badger.Open(badger.DefaultOptions("badger"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
config.LoadJSONFile("./config.json", &cfg)
streamService := service.NewStreamService(cfg.Server.HTTPPort, cfg.Kafka, employee.NewRepository(db))
apiDone := make(chan bool, 1)
employeeConsumerDone := make(chan bool, 1)
// API thread
go func() {
defer func() {
apiDone <- true
server.Log.WithField("topic", "Server").Info("closing API thread")
}()
// set the pubsub's Log to be the same as server's
pubsub.Log = server.Log
// in case we want to override the port or log location via CLI
server.SetConfigOverrides(cfg.Server)
server.Init("event-sourcing-example", cfg.Server)
err = server.Register(streamService)
if err != nil {
server.Log.Fatal(err)
}
if err = server.Run(); err != nil {
server.Log.Fatal(err)
}
}()
// employee consumer thread
go func() {
defer func() {
employeeConsumerDone <- true
server.Log.WithField("topic", "Consumer").Info("closing employee consumer thread")
}()
streamService.ConsumeEmployeeTopic()
}()
<-apiDone
<-employeeConsumerDone
}