-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (60 loc) · 1.37 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
package main
import (
"flag"
"strings"
"strconv"
"github.com/mmpei/gossip/model"
"net/http"
"github.com/gorilla/mux"
"fmt"
"github.com/mmpei/gossip/handler"
"time"
"github.com/mmpei/gossip/sync"
)
type sliceFlagValue []string
func (s *sliceFlagValue) Set(val string) error {
*s = sliceFlagValue(strings.Split(val, ","))
return nil
}
func (s *sliceFlagValue) String() string {
*s = sliceFlagValue(strings.Split("20202,20203", ","))
return ""
}
func main() {
var port int
var seeds sliceFlagValue
flag.Var(&seeds, "seeds", "the seeds of gossip")
flag.IntVar(&port, "port", 0, "server port")
flag.Parse()
ss := []string(seeds)
var seedPorts []int
for _, seed := range ss {
s, _ := strconv.Atoi(seed)
seedPorts = append(seedPorts, s)
// add seed
handler.Manager.Set(
&model.PeerInfo{
PeerId: s,
Version: 0,
Seed: true,
Alive: true,
SyncTime: time.Now(),
},
)
}
model.Self = model.NewPeer(port)
// add self
handler.Manager.Set(model.Self)
syncManager := sync.NewSyncManager()
ticker:=time.NewTicker(time.Second*2)
go func() {
for range ticker.C {
syncManager.Announce()
}
}()
listenAddr := fmt.Sprintf("0.0.0.0:%d", port)
router := mux.NewRouter()
router.HandleFunc("/metrics", handler.Metrics).Methods("GET")
router.HandleFunc("/announce", handler.Announce).Methods("POST")
http.ListenAndServe(listenAddr, router)
}