-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
63 lines (55 loc) · 1.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
package main
import (
"flag"
"fmt"
"net"
"os"
"github.com/ghodss/yaml"
curppb "github.com/xline-kv/mock-xline/gen/curppb"
"github.com/xline-kv/mock-xline/server"
"google.golang.org/grpc"
)
func main() {
name := flag.String("name", "node1", "node name")
addr := flag.String("addr", "127.0.0.1:8080", "node address")
config := flag.String("config", "node1", "config name")
flag.Parse()
cfg, err := os.ReadFile(fmt.Sprintf("config/%s.yml", *config))
if err != nil {
panic("read config file fail")
}
ops := []server.Operation{}
err = yaml.Unmarshal(cfg, &ops)
if err != nil {
panic(err)
}
fetchClusterOpMap := map[string][]server.FetchClusterResponse{}
proposeOpMap := map[string]server.ProposeResponse{}
waitSyncedOpMap := map[string]server.WaitSyncedResponse{}
for _, op := range ops {
switch op.Method {
case "fetch_cluster":
fetchClusterOpMap[op.Input.FetchCluster.String()] = op.Output.FetchCluster
case "propose":
proposeOpMap[op.Input.Propose.String()] = op.Output.Propose
case "wait_synced":
waitSyncedOpMap[op.Input.WaitSynced.String()] = op.Output.WaitSynced
default:
panic(fmt.Sprintf("unknown method: %s", op.Method))
}
}
lis, err := net.Listen("tcp", *addr)
if err != nil {
panic(err)
}
s := grpc.NewServer()
curppb.RegisterProtocolServer(s, server.NewProtocolServer(
fetchClusterOpMap,
proposeOpMap,
waitSyncedOpMap,
))
fmt.Printf("%s starts member change service listening at %s\n", *name, lis.Addr())
if err := s.Serve(lis); err != nil {
panic(err)
}
}