-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
95 lines (80 loc) · 2.2 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
package main
import (
"fmt"
"github.com/ardanlabs/conf"
"github.com/pkg/errors"
"github.com/qubic/go-qubic-nodes/node"
"github.com/qubic/go-qubic-nodes/web"
"log"
"net/http"
"os"
"time"
)
const prefix = "QUBIC_NODES"
type Configuration struct {
Qubic struct {
PeerList []string `conf:"default:5.39.222.64;82.197.173.130;82.197.173.129"`
PeerPort string `conf:"default:21841"`
ExchangeTimeout time.Duration `conf:"default:2s"`
MaxTickErrorThreshold uint32 `conf:"default:50"`
ReliableTickRange uint32 `conf:"default:30"`
}
Service struct {
TickerUpdateInterval time.Duration `conf:"default:5s"`
}
}
func main() {
if err := run(); err != nil {
log.Fatalf("main: exited with error: %s\n", err.Error())
}
}
func run() error {
var config Configuration
if err := conf.Parse(os.Args[1:], prefix, &config); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage(prefix, &config)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString(prefix, &config)
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(&config)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)
container, err := node.NewNodeContainer(config.Qubic.PeerList, config.Qubic.PeerPort, config.Qubic.MaxTickErrorThreshold, config.Qubic.ReliableTickRange, config.Qubic.ExchangeTimeout)
if err != nil {
log.Printf("Error: %v\n", err)
}
go func() {
ticker := time.NewTicker(config.Service.TickerUpdateInterval)
for {
select {
case <-ticker.C:
updateErr := container.Update()
if updateErr != nil {
log.Printf("Error: %v\n", updateErr)
}
}
}
}()
log.Printf("Staring WebServer...\n")
handler := web.RequestHandler{
Container: container,
}
http.HandleFunc("/status", handler.HandleStatus)
http.HandleFunc("/max-tick", handler.HandleMaxTick)
return http.ListenAndServe(":8080", nil)
}