/
main.go
83 lines (73 loc) · 2.11 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
package main
import (
"fmt"
"net/http"
"os"
"runtime/pprof"
"github.com/jessevdk/go-flags"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/tapcfg"
"github.com/lightningnetwork/lnd/signal"
)
func main() {
// Hook interceptor for os signals.
shutdownInterceptor, err := signal.Intercept()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Load the configuration, and parse any command line options. This
// function will also set up logging properly.
cfg, cfgLogger, err := tapcfg.LoadConfig(shutdownInterceptor)
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
// Print error if not due to help request.
err = fmt.Errorf("failed to load config: %w", err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Help was requested, exit normally.
os.Exit(0)
}
// Enable http profiling server if requested.
if cfg.Profile != "" {
go func() {
profileRedirect := http.RedirectHandler("/debug/pprof",
http.StatusSeeOther)
http.Handle("/", profileRedirect)
cfgLogger.Infof("Pprof listening on %v", cfg.Profile)
//nolint:gosec
fmt.Println(http.ListenAndServe(cfg.Profile, nil))
}()
}
// Write cpu profile if requested.
if cfg.CPUProfile != "" {
f, err := os.Create(cfg.CPUProfile)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
_ = pprof.StartCPUProfile(f)
defer f.Close()
defer pprof.StopCPUProfile()
}
// This concurrent error queue can be used by every component that can
// raise runtime errors. Using a queue will prevent us from blocking on
// sending errors to it, as long as the queue is running.
errQueue := fn.NewConcurrentQueue[error](fn.DefaultQueueSize)
errQueue.Start()
defer errQueue.Stop()
server, err := tapcfg.CreateServerFromConfig(
cfg, cfgLogger, shutdownInterceptor, errQueue.ChanIn(),
)
if err != nil {
err := fmt.Errorf("error creating server: %v", err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
err = server.RunUntilShutdown(errQueue.ChanOut())
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}