-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (114 loc) · 3.51 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
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"github.com/joho/godotenv"
"github.com/kostiamol/centerms/api"
"github.com/kostiamol/centerms/cfg"
"github.com/kostiamol/centerms/event/pub"
"github.com/kostiamol/centerms/log"
"github.com/kostiamol/centerms/metric"
"github.com/kostiamol/centerms/store"
"github.com/kostiamol/centerms/store/model"
"github.com/kostiamol/centerms/svc"
"github.com/kostiamol/centerms/trace"
)
// todo: move ctrl to main
// todo: error %w
// todo: regen mocks
// todo: cover with tests
// todo: use clickhouse for data
// todo: use zookeper for cfg
// todo: look through the handlers
// todo: check architecture (redis, websocket, nats, raw json data)
// todo: overall test coverage > 80%
// todo: check travis + badges
// todo: swagger
// todo: update README.md: helm (deployment, redis, nats), schema (arrows, k8s)
// todo: update docker-compose and test with fridgems
// todo: start fridgems refactoring
// runner is a contract for all the components (api + services).
type runner interface {
Run()
}
func main() {
loadCfgErr := godotenv.Load(".env")
config, newCfgErr := cfg.New()
logger := log.New(config.Service.AppID, config.Service.LogLevel)
if loadCfgErr != nil {
logger.Infof("func godotenv.Load: %s", loadCfgErr)
}
if newCfgErr != nil {
logger.Fatalf("func cfg.New: %s", newCfgErr)
}
if err := trace.Init(config.Service.AppID, config.TraceAgent); err != nil {
logger.Fatalf("func trace.Init: %s", err)
}
storer, err := store.New(
&store.Cfg{
Addr: cfg.Addr{Host: config.Store.Addr.Host, Port: config.Store.Addr.Port},
Password: config.Store.Password,
MaxIdlePoolConns: config.Store.MaxIdlePoolConns,
IdleTimeout: config.Store.IdleTimeout,
})
if err != nil {
logger.Fatalf("func store.New: %s", err)
}
logger.With("event", log.EventStoreInit)
publisher := pub.New(
&pub.Cfg{
Addr: cfg.Addr{Host: config.Publisher.Addr.Host, Port: config.Publisher.Addr.Port},
RetryTimeout: config.Service.RetryTimeout,
RetryAttempts: config.Service.RetryAttempts,
})
ctrl := svc.Ctrl{StopChan: make(chan struct{})}
mtrc := metric.New(config.Service.AppID)
dataChan := make(chan *model.Data)
confChan := make(chan *model.Cfg)
data := svc.NewDataService(
&svc.DataServiceCfg{
Log: logger,
Ctrl: ctrl,
Metric: mtrc,
Store: storer,
PubChan: dataChan,
})
conf := svc.NewCfgService(
&svc.CfgServiceCfg{
Log: logger,
Ctrl: ctrl,
Metric: mtrc,
Store: storer,
SubChan: confChan,
Publisher: publisher,
})
stream := svc.NewStreamService(
&svc.StreamServiceCfg{
Log: logger,
Ctrl: ctrl,
Metric: mtrc,
SubChan: dataChan,
PortWS: config.Service.PortWebSocket,
TerminationTimeout: config.Service.TerminationTimeout,
})
api := api.New(
&api.Cfg{
Log: logger,
Ctrl: ctrl,
Metric: mtrc,
PubChan: confChan,
PortRPC: config.Service.PortRPC,
PortREST: config.Service.PortREST,
CfgProvider: conf,
DataProvider: data,
Retry: config.Service.RetryTimeout,
PublicKey: config.Token.PublicKey,
PrivateKey: config.Token.PrivateKey,
TerminationTimeout: config.Service.TerminationTimeout,
})
components := []runner{data, conf, stream, api}
for _, c := range components {
go c.Run()
}
ctrl.Wait(config.Service.TerminationTimeout)
logger.With("event", log.EventMSShutdown)
_ = logger.Flush()
}