-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (51 loc) · 1004 Bytes
/
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
package main
import (
"fmt"
"gframework/log"
"net/http"
)
type CachedContext struct {
hs *HttpServer
opts *Options
raft *raftNodeInfo
}
func main() {
ctx := &CachedContext{
opts: NewOptions(),
}
logConf := log.Config{
Path: "./",
Level: "debug",
MaxSize: 1,
}
log.Init(logConf)
raft, err := newRaftNode(ctx.opts)
if err != nil {
log.Panic(fmt.Sprintf("new raft node failed:%v", err))
}
ctx.raft = raft
if ctx.opts.joinAddress != "" {
err = joinRaftCluster(ctx.opts)
if err != nil {
log.Panic(fmt.Sprintf("join raft cluster failed:%v", err))
}
}
httpServer := NewHttpServer(ctx)
ctx.hs = httpServer
go func() {
http.ListenAndServe(ctx.opts.httpAddress, nil)
}()
// monitor leadership
for {
select {
case leader := <-ctx.raft.leaderNotifyCh:
if leader {
log.Info("become leader, enable write api")
ctx.hs.setLeaderFlag(true)
} else {
log.Info("become follower, close write api")
ctx.hs.setLeaderFlag(false)
}
}
}
}