-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (171 loc) · 5.67 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"go/types"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/diamcircle/go/exp/services/captivecore/internal"
"github.com/diamcircle/go/ingest/ledgerbackend"
"github.com/diamcircle/go/network"
"github.com/diamcircle/go/support/config"
"github.com/diamcircle/go/support/db"
supporthttp "github.com/diamcircle/go/support/http"
supportlog "github.com/diamcircle/go/support/log"
)
func main() {
var port int
var networkPassphrase, binaryPath, configPath, dbURL string
var captiveCoreTomlParams ledgerbackend.CaptiveCoreTomlParams
var historyArchiveURLs []string
var checkpointFrequency uint32
var logLevel logrus.Level
logger := supportlog.New()
configOpts := config.ConfigOptions{
{
Name: "port",
Usage: "Port to listen and serve on",
OptType: types.Int,
ConfigKey: &port,
FlagDefault: 8000,
Required: true,
},
{
Name: "network-passphrase",
Usage: "Network passphrase of the Diamcircle network transactions should be signed for",
OptType: types.String,
ConfigKey: &networkPassphrase,
FlagDefault: network.TestNetworkPassphrase,
Required: true,
},
&config.ConfigOption{
Name: "diamcircle-core-binary-path",
OptType: types.String,
FlagDefault: "",
Required: true,
Usage: "path to diamcircle core binary",
ConfigKey: &binaryPath,
},
&config.ConfigOption{
Name: "captive-core-config-path",
OptType: types.String,
FlagDefault: "",
Required: true,
Usage: "path to additional configuration for the Diamcircle Core configuration file used by captive core. It must, at least, include enough details to define a quorum set",
ConfigKey: &configPath,
},
&config.ConfigOption{
Name: "history-archive-urls",
ConfigKey: &historyArchiveURLs,
OptType: types.String,
Required: true,
FlagDefault: "",
CustomSetValue: func(co *config.ConfigOption) error {
stringOfUrls := viper.GetString(co.Name)
urlStrings := strings.Split(stringOfUrls, ",")
*(co.ConfigKey.(*[]string)) = urlStrings
return nil
},
Usage: "comma-separated list of diamcircle history archives to connect with",
},
&config.ConfigOption{
Name: "log-level",
ConfigKey: &logLevel,
OptType: types.String,
FlagDefault: "info",
CustomSetValue: func(co *config.ConfigOption) error {
ll, err := logrus.ParseLevel(viper.GetString(co.Name))
if err != nil {
return fmt.Errorf("Could not parse log-level: %v", viper.GetString(co.Name))
}
*(co.ConfigKey.(*logrus.Level)) = ll
return nil
},
Usage: "minimum log severity (debug, info, warn, error) to log",
},
&config.ConfigOption{
Name: "db-url",
EnvVar: "DATABASE_URL",
ConfigKey: &dbURL,
OptType: types.String,
Required: false,
Usage: "aurora postgres database to connect with",
},
&config.ConfigOption{
Name: "diamcircle-captive-core-http-port",
ConfigKey: &captiveCoreTomlParams.HTTPPort,
OptType: types.Uint,
CustomSetValue: config.SetOptionalUint,
Required: false,
FlagDefault: uint(11626),
Usage: "HTTP port for Captive Core to listen on (0 disables the HTTP server)",
},
&config.ConfigOption{
Name: "checkpoint-frequency",
ConfigKey: &checkpointFrequency,
OptType: types.Uint32,
FlagDefault: uint32(64),
Required: false,
Usage: "establishes how many ledgers exist between checkpoints, do NOT change this unless you really know what you are doing",
},
}
cmd := &cobra.Command{
Use: "captivecore",
Short: "Run the remote captive core server",
Run: func(_ *cobra.Command, _ []string) {
configOpts.Require()
configOpts.SetValues()
logger.SetLevel(logLevel)
captiveCoreTomlParams.HistoryArchiveURLs = historyArchiveURLs
captiveCoreTomlParams.NetworkPassphrase = networkPassphrase
captiveCoreTomlParams.Strict = true
captiveCoreToml, err := ledgerbackend.NewCaptiveCoreTomlFromFile(configPath, captiveCoreTomlParams)
if err != nil {
logger.WithError(err).Fatal("Invalid captive core toml")
}
captiveConfig := ledgerbackend.CaptiveCoreConfig{
BinaryPath: binaryPath,
NetworkPassphrase: networkPassphrase,
HistoryArchiveURLs: historyArchiveURLs,
CheckpointFrequency: checkpointFrequency,
Log: logger.WithField("subservice", "diamcircle-core"),
Toml: captiveCoreToml,
}
var dbConn *db.Session
if len(dbURL) > 0 {
dbConn, err = db.Open("postgres", dbURL)
if err != nil {
logger.WithError(err).Fatal("Could not create db connection instance")
}
captiveConfig.LedgerHashStore = ledgerbackend.NewAuroraDBLedgerHashStore(dbConn)
}
core, err := ledgerbackend.NewCaptive(captiveConfig)
if err != nil {
logger.WithError(err).Fatal("Could not create captive core instance")
}
api := internal.NewCaptiveCoreAPI(core, logger.WithField("subservice", "api"))
supporthttp.Run(supporthttp.Config{
ListenAddr: fmt.Sprintf(":%d", port),
Handler: internal.Handler(api),
OnStarting: func() {
logger.Infof("Starting Captive Core server on %v", port)
},
OnStopping: func() {
// TODO: Check this aborts in-progress requests instead of letting
// them finish, to preserve existing behaviour.
api.Shutdown()
if dbConn != nil {
dbConn.Close()
}
},
})
},
}
if err := configOpts.Init(cmd); err != nil {
logger.WithError(err).Fatal("could not parse config options")
}
if err := cmd.Execute(); err != nil {
logger.WithError(err).Fatal("could not run")
}
}