/
main.go
226 lines (187 loc) · 6.02 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"log"
"runtime"
"github.com/PuerkitoBio/throttled"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stellar/go/services/horizon/internal"
hlog "github.com/stellar/go/services/horizon/internal/log"
)
var app *horizon.App
var config horizon.Config
var rootCmd *cobra.Command
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
rootCmd.Execute()
}
func init() {
viper.SetDefault("port", 8000)
viper.SetDefault("history-retention-count", 0)
viper.BindEnv("port", "PORT")
viper.BindEnv("db-url", "DATABASE_URL")
viper.BindEnv("stellar-core-db-url", "STELLAR_CORE_DATABASE_URL")
viper.BindEnv("stellar-core-url", "STELLAR_CORE_URL")
viper.BindEnv("friendbot-secret", "FRIENDBOT_SECRET")
viper.BindEnv("per-hour-rate-limit", "PER_HOUR_RATE_LIMIT")
viper.BindEnv("redis-url", "REDIS_URL")
viper.BindEnv("ruby-horizon-url", "RUBY_HORIZON_URL")
viper.BindEnv("log-level", "LOG_LEVEL")
viper.BindEnv("sentry-dsn", "SENTRY_DSN")
viper.BindEnv("loggly-token", "LOGGLY_TOKEN")
viper.BindEnv("loggly-host", "LOGGLY_HOST")
viper.BindEnv("tls-cert", "TLS_CERT")
viper.BindEnv("tls-key", "TLS_KEY")
viper.BindEnv("ingest", "INGEST")
viper.BindEnv("network-passphrase", "NETWORK_PASSPHRASE")
viper.BindEnv("history-retention-count", "HISTORY_RETENTION_COUNT")
viper.BindEnv("history-stale-threshold", "HISTORY_STALE_THRESHOLD")
viper.BindEnv("skip-cursor-update", "SKIP_CURSOR_UPDATE")
rootCmd = &cobra.Command{
Use: "horizon",
Short: "client-facing api server for the stellar network",
Long: "client-facing api server for the stellar network",
Run: func(cmd *cobra.Command, args []string) {
initApp(cmd, args)
app.Serve()
},
}
rootCmd.Flags().String(
"db-url",
"",
"horizon postgres database to connect with",
)
rootCmd.Flags().String(
"stellar-core-db-url",
"",
"stellar-core postgres database to connect with",
)
rootCmd.Flags().String(
"stellar-core-url",
"",
"stellar-core to connect with (for http commands)",
)
rootCmd.Flags().Int(
"port",
8000,
"tcp port to listen on for http requests",
)
rootCmd.Flags().Int(
"per-hour-rate-limit",
3600,
"max count of requests allowed in a one hour period, by remote ip address",
)
rootCmd.Flags().String(
"redis-url",
"",
"redis to connect with, for rate limiting",
)
rootCmd.Flags().String(
"log-level",
"info",
"Minimum log severity (debug, info, warn, error) to log",
)
rootCmd.Flags().String(
"sentry-dsn",
"",
"Sentry URL to which panics and errors should be reported",
)
rootCmd.Flags().String(
"loggly-token",
"",
"Loggly token, used to configure log forwarding to loggly",
)
rootCmd.Flags().String(
"loggly-host",
"",
"Hostname to be added to every loggly log event",
)
rootCmd.Flags().String(
"friendbot-secret",
"",
"Secret seed for friendbot functionality. When empty, friendbot will be disabled",
)
rootCmd.Flags().String(
"tls-cert",
"",
"The TLS certificate file to use for securing connections to horizon",
)
rootCmd.Flags().String(
"tls-key",
"",
"The TLS private key file to use for securing connections to horizon",
)
rootCmd.Flags().Bool(
"ingest",
false,
"causes this horizon process to ingest data from stellar-core into horizon's db",
)
rootCmd.Flags().String(
"network-passphrase",
"",
"Override the network passphrase",
)
rootCmd.Flags().Uint(
"history-retention-count",
0,
"the minimum number of ledgers to maintain within horizon's history tables. 0 signifies an unlimited number of ledgers will be retained",
)
rootCmd.Flags().Uint(
"history-stale-threshold",
0,
"the maximum number of ledgers the history db is allowed to be out of date from the connected stellar-core db before horizon considers history stale",
)
rootCmd.AddCommand(dbCmd)
viper.BindPFlags(rootCmd.Flags())
}
func initApp(cmd *cobra.Command, args []string) {
initConfig()
var err error
app, err = horizon.NewApp(config)
if err != nil {
log.Fatal(err.Error())
}
}
func initConfig() {
if viper.GetString("db-url") == "" {
log.Fatal("Invalid config: db-url is blank. Please specify --db-url on the command line or set the DATABASE_URL environment variable.")
}
if viper.GetString("stellar-core-db-url") == "" {
log.Fatal("Invalid config: stellar-core-db-url is blank. Please specify --stellar-core-db-url on the command line or set the STELLAR_CORE_DATABASE_URL environment variable.")
}
if viper.GetString("stellar-core-url") == "" {
log.Fatal("Invalid config: stellar-core-url is blank. Please specify --stellar-core-url on the command line or set the STELLAR_CORE_URL environment variable.")
}
ll, err := logrus.ParseLevel(viper.GetString("log-level"))
if err != nil {
log.Fatalf("Could not parse log-level: %v", viper.GetString("log-level"))
}
hlog.DefaultLogger.Level = ll
cert, key := viper.GetString("tls-cert"), viper.GetString("tls-key")
switch {
case cert != "" && key == "":
log.Fatal("Invalid TLS config: key not configured")
case cert == "" && key != "":
log.Fatal("Invalid TLS config: cert not configured")
}
config = horizon.Config{
DatabaseURL: viper.GetString("db-url"),
StellarCoreDatabaseURL: viper.GetString("stellar-core-db-url"),
StellarCoreURL: viper.GetString("stellar-core-url"),
Port: viper.GetInt("port"),
RateLimit: throttled.PerHour(viper.GetInt("per-hour-rate-limit")),
RedisURL: viper.GetString("redis-url"),
LogLevel: ll,
SentryDSN: viper.GetString("sentry-dsn"),
LogglyToken: viper.GetString("loggly-token"),
LogglyHost: viper.GetString("loggly-host"),
FriendbotSecret: viper.GetString("friendbot-secret"),
TLSCert: cert,
TLSKey: key,
Ingest: viper.GetBool("ingest"),
HistoryRetentionCount: uint(viper.GetInt("history-retention-count")),
StaleThreshold: uint(viper.GetInt("history-stale-threshold")),
SkipCursorUpdate: viper.GetBool("skip-cursor-update"),
}
}