-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathbssd.go
139 lines (121 loc) · 3.37 KB
/
bssd.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
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/juju/loggo"
"github.com/hemilabs/heminetwork/api/bfgapi"
"github.com/hemilabs/heminetwork/api/bssapi"
"github.com/hemilabs/heminetwork/config"
"github.com/hemilabs/heminetwork/service/bss"
"github.com/hemilabs/heminetwork/version"
)
const (
daemonName = "bssd"
defaultLogLevel = daemonName + "=INFO;protocol=INFO;bss=INFO"
)
var (
log = loggo.GetLogger(daemonName)
welcome string
cfg = bss.NewDefaultConfig()
cm = config.CfgMap{
"BSS_BFG_URL": config.Config{
Value: &cfg.BFGURL,
DefaultValue: bfgapi.DefaultPrivateURL,
Help: "bfgd endpoint",
Print: config.PrintAll,
},
"BSS_ADDRESS": config.Config{
Value: &cfg.ListenAddress,
DefaultValue: bssapi.DefaultListen,
Help: "address and port bssd listens on",
Print: config.PrintAll,
},
"BSS_LOG_LEVEL": config.Config{
Value: &cfg.LogLevel,
DefaultValue: defaultLogLevel,
Help: "loglevel for various packages; INFO, DEBUG and TRACE",
Print: config.PrintAll,
},
"BSS_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "", // bssapi.DefaultPrometheusListen,
Help: "address and port bssd prometheus listens on",
Print: config.PrintAll,
},
"BSS_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port bssd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
}
)
func HandleSignals(ctx context.Context, cancel context.CancelFunc, callback func(os.Signal)) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
defer func() {
signal.Stop(signalChan)
cancel()
}()
select {
case <-ctx.Done():
case s := <-signalChan: // First signal, cancel context.
if callback != nil {
callback(s) // Do whatever caller wants first.
cancel()
}
}
<-signalChan // Second signal, hard exit.
os.Exit(2)
}
func _main() error {
// Parse configuration from environment
if err := config.Parse(cm); err != nil {
return err
}
if err := loggo.ConfigureLoggers(cfg.LogLevel); err != nil {
return err
}
log.Infof("%v", welcome)
pc := config.PrintableConfig(cm)
for k := range pc {
log.Infof("%v", pc[k])
}
ctx, cancel := context.WithCancel(context.Background())
go HandleSignals(ctx, cancel, func(s os.Signal) {
log.Infof("bss service received signal: %s", s)
})
server, err := bss.NewServer(cfg)
if err != nil {
return fmt.Errorf("create BSS server: %w", err)
}
if err := server.Run(ctx); !errors.Is(err, context.Canceled) {
return fmt.Errorf("bss server terminated: %w", err)
}
return nil
}
func init() {
version.Component = "bssd"
welcome = "Hemi Bitcoin Secure Sequencer " + version.BuildInfo()
}
func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\thelp (this help)\n")
fmt.Fprintf(os.Stderr, "Environment:\n")
config.Help(os.Stderr, cm)
os.Exit(1)
}
if err := _main(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}