forked from lightninglabs/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faraday.go
123 lines (105 loc) · 3.32 KB
/
faraday.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
// Package faraday contains the main function for faraday.
package faraday
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jessevdk/go-flags"
"github.com/lightninglabs/faraday/chain"
"github.com/lightninglabs/faraday/frdrpcserver"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/signal"
)
// MinLndVersion is the minimum lnd version required. Note that apis that are
// only available in more recent versions are available at compile time, so this
// version should be bumped if additional functionality is included that depends
// on newer apis.
var MinLndVersion = &verrpc.Version{
AppMajor: 0,
AppMinor: 15,
AppPatch: 4,
}
// Main is the real entry point for faraday. It is required to ensure that
// defers are properly executed when os.Exit() is called.
func Main() error {
// Start with a default config.
config := DefaultConfig()
// Parse command line options to obtain user specified values.
if _, err := flags.Parse(&config); err != nil {
return err
}
// Show the version and exit if the version flag was specified.
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
if config.ShowVersion {
fmt.Println(appName, "version", Version())
os.Exit(0)
}
// Hook interceptor for os signals.
shutdownInterceptor, err := signal.Intercept()
if err != nil {
return err
}
// Setup logging before parsing the config.
logWriter := build.NewRotatingLogWriter()
SetupLoggers(logWriter, shutdownInterceptor)
err = build.ParseAndSetDebugLevels(config.DebugLevel, logWriter)
if err != nil {
return err
}
if err := ValidateConfig(&config); err != nil {
return fmt.Errorf("error validating config: %v", err)
}
serverTLSCfg, restClientCreds, err := getTLSConfig(&config)
if err != nil {
return fmt.Errorf("error loading TLS config: %v", err)
}
// Connect to the full suite of lightning services offered by lnd's
// subservers.
client, err := lndclient.NewLndServices(&lndclient.LndServicesConfig{
LndAddress: config.Lnd.RPCServer,
Network: lndclient.Network(config.Network),
CustomMacaroonPath: config.Lnd.MacaroonPath,
TLSPath: config.Lnd.TLSCertPath,
CheckVersion: MinLndVersion,
RPCTimeout: config.Lnd.RequestTimeout,
})
if err != nil {
return fmt.Errorf("cannot connect to lightning services: %v",
err)
}
defer client.Close()
// Instantiate the faraday gRPC server.
cfg := &frdrpcserver.Config{
Lnd: client.LndServices,
RPCListen: config.RPCListen,
RESTListen: config.RESTListen,
CORSOrigin: config.CORSOrigin,
TLSServerConfig: serverTLSCfg,
RestClientConfig: restClientCreds,
FaradayDir: config.FaradayDir,
MacaroonPath: config.MacaroonPath,
}
// If the client chose to connect to a bitcoin client, get one now.
if config.ChainConn {
cfg.BitcoinClient, err = chain.NewBitcoinClient(config.Bitcoin)
if err != nil {
return err
}
}
server := frdrpcserver.NewRPCServer(cfg)
// Start the server.
if err := server.Start(); err != nil {
return err
}
// Run until the user terminates.
<-shutdownInterceptor.ShutdownChannel()
log.Infof("Received shutdown signal.")
if err := server.Stop(); err != nil {
return err
}
return nil
}