-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (47 loc) · 1.06 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"
"log"
"strings"
"time"
"github.com/willdot/raft/raft"
"github.com/willdot/raft/server"
"github.com/willdot/raft/service"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "string slice flag"
}
func (i *arrayFlags) Set(value string) error {
allVals := strings.Split(value, ",")
for _, val := range allVals {
*i = append(*i, strings.TrimSpace(val))
}
return nil
}
var peers arrayFlags
func main() {
port := flag.Int("port", 0, "the port for this server")
flag.Var(&peers, "peers", "an array of peers to connect to")
flag.Parse()
if *port < 1 {
log.Fatal(fmt.Errorf("invalid flag"))
}
addr := fmt.Sprintf("localhost:%d", *port)
fmt.Println(addr)
service := service.NewService(addr)
err := service.AddPeer(peers)
if err != nil {
fmt.Printf("error adding peers: %s\n", err)
}
raft, err := raft.NewRaft(service, time.Second, peers)
if err != nil {
log.Fatal(err)
}
server, err := server.NewRPCServer(addr, raft)
if err != nil {
log.Fatal(err)
}
log.Fatal(server.Start())
}