This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
100 lines (88 loc) · 2.28 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"github.com/ardanlabs/conf"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
"log"
"net/http"
"os"
"time"
)
const prefix = "NODE_FETCHER"
func main() {
err := run()
if err != nil {
log.Fatal(err.Error())
}
}
func run() error {
var cfg struct {
Server struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
}
Qubic struct {
FixedPeerList bool `conf:"default:false"`
StartingPeersIP []string `conf:"default:185.70.186.149;185.70.186.153"`
WhitelistPeers []string
MaxPeers int `conf:"default:50"`
ExchangeTimeout time.Duration `conf:"default:2s"`
StorageFolder string `conf:"default:store"`
}
}
if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}
out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)
db, err := pebble.Open(cfg.Qubic.StorageFolder, &pebble.Options{})
if err != nil {
log.Fatalf("err opening pebble: %s", err.Error())
}
rp, err := NewPeers(cfg.Qubic.FixedPeerList, cfg.Qubic.StartingPeersIP, cfg.Qubic.WhitelistPeers, cfg.Qubic.MaxPeers, cfg.Qubic.ExchangeTimeout, db)
if err != nil {
return errors.Wrap(err, "creating new peers")
}
err = rp.Compute()
if err != nil {
return errors.Wrap(err, "computing first batch of reliable peers")
}
h := Handler{rp: rp}
go func() {
ticker := time.NewTicker(15 * time.Second)
for {
select {
case <-ticker.C:
err := rp.Compute()
if err != nil {
log.Printf("Computing reliable peers: %s", err.Error())
}
}
}
}()
fmt.Println("Server started")
http.HandleFunc("/peers", h.Handle)
log.Fatal(http.ListenAndServe(":8080", nil))
return nil
}