-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
91 lines (72 loc) · 1.81 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime/debug"
"time"
"github.com/rs/zerolog"
"github.com/barebitcoin/btc-buf/server"
)
func realMain(cfg *config) error {
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(context.Canceled)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
signal := <-sig
zerolog.Ctx(ctx).Info().
Stringer("signal", signal).
Msg("received signal, canceling context")
cancel(fmt.Errorf("received %s signal", signal))
}()
clientCtx, clientCancel := context.WithTimeout(ctx, time.Second*10)
defer clientCancel()
bitcoind, err := server.NewBitcoind(
clientCtx, cfg.Bitcoind.Host, cfg.Bitcoind.User, cfg.Bitcoind.Pass,
)
if err != nil {
return fmt.Errorf("new server: %w", err)
}
errs := make(chan error)
go func() {
if err := bitcoind.Listen(ctx, cfg.Listen); err != nil {
errs <- err
}
}()
go func() {
<-ctx.Done()
bitcoind.Shutdown(ctx)
errs <- context.Cause(ctx)
}()
return <-errs
}
func main() {
ctx := context.Background()
cfg, err := readConfig(ctx)
if err != nil {
panic(fmt.Sprintf("read config: %s", err))
}
// important: this is only usable AFTER readConfig has been called
log := zerolog.Ctx(ctx)
if info, ok := debug.ReadBuildInfo(); ok {
log.Info().
Str("go", info.GoVersion).
Str("vcs.sha", findSetting("vcs.revision", info.Settings)).
Str("vcs.modified", findSetting("vcs.modified", info.Settings)).
Msgf("starting %s", os.Args[0])
}
if err := realMain(cfg); err != nil {
log.Fatal().Err(err).Msg("main: received error")
}
log.Info().Msgf("main: exiting with 0 code")
}
func findSetting(key string, settings []debug.BuildSetting) string {
for _, setting := range settings {
if setting.Key == key {
return setting.Value
}
}
return "unknown"
}