forked from mdevilliers/redishappy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
72 lines (53 loc) · 1.93 KB
/
engine.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
package redishappy
import (
"github.com/mdevilliers/redishappy/api"
"github.com/mdevilliers/redishappy/configuration"
"github.com/mdevilliers/redishappy/sentinel"
"github.com/mdevilliers/redishappy/services/logger"
"github.com/mdevilliers/redishappy/types"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
type Muxonate func(*web.Mux)
type RedisHappyEngine struct {
cm *configuration.ConfigurationManager
sm *sentinel.SentinelManager
}
func NewRedisHappyEngine(flipper types.FlipperClient, cm *configuration.ConfigurationManager) *RedisHappyEngine {
masterEvents := make(chan types.MasterSwitchedEvent)
sentinelManager := sentinel.NewManager(masterEvents, cm)
go loopSentinelEvents(flipper, masterEvents)
go intiliseTopology(flipper, sentinelManager)
return &RedisHappyEngine{
cm: cm,
sm: sentinelManager,
}
}
func (r *RedisHappyEngine) ConfigureHandlersAndServe(fn Muxonate) {
initApiServer(r.sm, r.cm, fn)
}
func (r *RedisHappyEngine) Serve() {
initApiServer(r.sm, r.cm, func(_ *web.Mux) {})
}
func loopSentinelEvents(flipper types.FlipperClient, masterEvents chan types.MasterSwitchedEvent) {
for event := range masterEvents {
flipper.Orchestrate(event)
}
}
func intiliseTopology(flipper types.FlipperClient, sentinelManager *sentinel.SentinelManager) {
topology := sentinelManager.GetCurrentTopology()
flipper.InitialiseRunningState(&topology)
}
func initApiServer(manager *sentinel.SentinelManager, cm *configuration.ConfigurationManager, muxonator Muxonate) {
logger.Info.Print("hosting json endpoint.")
pongApi := api.PingApi{}
sentinelApi := api.SentinelApi{Manager: manager}
configurationApi := api.ConfigurationApi{ConfigurationManager: cm}
topologyApi := api.TopologyApi{Manager: manager}
goji.Get("/api/ping", pongApi.Get)
goji.Get("/api/sentinels", sentinelApi.Get)
goji.Get("/api/configuration", configurationApi.Get)
goji.Get("/api/topology", topologyApi.Get)
muxonator(goji.DefaultMux)
goji.Serve()
}