-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (134 loc) · 3.21 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"bytes"
"context"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"crypto/rand"
"github.com/ecadlabs/go-tezos-keygen/charger"
"github.com/ecadlabs/go-tezos-keygen/config"
"github.com/ecadlabs/go-tezos-keygen/keypool"
"github.com/ecadlabs/go-tezos-keygen/server"
"github.com/ecadlabs/go-tezos-keygen/server/middleware"
"github.com/ecadlabs/go-tezos-keygen/service"
"github.com/ecadlabs/go-tezos-keygen/utils"
"github.com/ecadlabs/gotez/v2/client"
log "github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)
func main() {
var (
networksFile string
databaseFile string
address string
level string
genSeed bool
)
flag.StringVar(&networksFile, "n", "", "Networks configuration file")
flag.StringVar(&databaseFile, "d", "", "Database")
flag.StringVar(&address, "a", ":3000", "Address")
flag.StringVar(&level, "l", "info", "Level [panic,fatal,error,warn,info,debug,trace]")
flag.BoolVar(&genSeed, "seed", false, "Generate 512 bit seed and exit")
flag.Parse()
l, err := log.ParseLevel(level)
if err != nil {
log.Fatal(err)
}
log.SetLevel(l)
if genSeed {
var seed [64]byte
if _, err := rand.Read(seed[:]); err != nil {
log.Fatal(err)
}
fmt.Println(hex.EncodeToString(seed[:]))
return
}
if networksFile == "" {
networksFile = os.Getenv("KEYGEN_NETWORKS")
}
if databaseFile == "" {
databaseFile = os.Getenv("KEYGEN_DB")
}
var rd io.Reader
if x := os.Getenv("KEYGEN_NETWORKS_DATA"); x != "" {
rd = bytes.NewReader([]byte(x))
} else {
fd, err := os.Open(networksFile)
if err != nil {
log.Fatal(err)
}
defer fd.Close()
rd = bufio.NewReader(fd)
}
cfg, err := config.New(rd)
if err != nil {
log.Fatal(err)
}
log.Infof("Database file: %s", databaseFile)
db, err := bolt.Open(databaseFile, 0600, nil)
if err != nil {
log.Fatal(err)
}
nets := make(map[string]*service.Network, len(cfg))
for name, net := range cfg {
client := &client.Client{
URL: net.GetURL(),
DebugLogger: (*utils.DebugLogger)(log.StandardLogger()),
}
charger := charger.New(net, client)
pool, err := keypool.New(db, net, charger)
if err != nil {
log.Fatal(err)
}
nets[name] = &service.Network{
Pool: pool,
Charger: charger,
Config: net,
}
}
service := service.Service{Networks: nets}
server := server.Server{Service: &service}
handler := server.Router()
logger := middleware.Logging{}
handler.Use(logger.Handler)
srv := &http.Server{
Handler: handler,
Addr: address,
}
errCh := make(chan error)
go func() {
log.Printf("HTTP server is listening for connections on %s", srv.Addr)
errCh <- srv.ListenAndServe()
}()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
select {
case <-signalCh:
case <-errCh:
log.Fatal(err) // happened before shutdown
}
log.Info("Shutting down...")
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
if err := <-errCh; !errors.Is(err, http.ErrServerClosed) {
log.Error(err)
}
for _, n := range nets {
if err := n.Pool.Stop(context.Background()); err != nil {
log.Error(err)
}
}
if err := db.Close(); err != nil {
log.Fatal(err)
}
log.Info("Bye")
}