-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
224 lines (191 loc) · 6.2 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/SkynetLabs/siacoin-promoter/api"
"github.com/SkynetLabs/siacoin-promoter/dependencies"
"github.com/SkynetLabs/siacoin-promoter/promoter"
"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/errors"
"gitlab.com/SkynetLabs/skyd/node/api/client"
)
type (
// config contains the configuration for the service which is parsed
// from the environment vars.
config struct {
AccountsAPIAddr string
LogLevel logrus.Level
Port int
DBURI string
DBUser string
DBPassword string
ServerDomain string
SkydOpts client.Options
}
)
const (
// dbName is the name of the database to use for the siacoin promoter.
dbName = "siacoin-promoter"
// defaultSkydUserAgent defines the default agent used when no other
// value is specified by the user.
defaultSkydUserAgent = "Sia-Agent"
// envAccountsHost is the address of the accounts API.
envAccountsHost = "ACCOUNTS_HOST"
// envAccountsPort is the port the accounts service listens on.
envAccountsPort = "ACCOUNTS_PORT"
// envAPIShutdownTimeout is the timeout for gracefully shutting down the
// API before killing it.
envAPIShutdownTimeout = 20 * time.Second
// envMongoDBURI is the environment variable for the mongodb URI.
envMongoDBURI = "MONGODB_URI"
// envMongoDBUser is the environment variable for the mongodb user.
envMongoDBUser = "MONGODB_USER"
// envMongoDBPassword is the environment variable for the mongodb password.
envMongoDBPassword = "MONGODB_PASSWORD"
// envLogLevel is the environment variable for the log level used by
// this service.
envLogLevel = "SIACOIN_PROMOTER_LOG_LEVEL"
// envSkydAPIAddr is the environment variable for setting the skyd
// address.
envSkydAPIAddr = "SKYD_API_ADDRESS"
// envSkydAPIUserAgent is the environment variable for setting the skyd
// User Agent.
envSkydAPIUserAgent = "SKYD_API_USER_AGENT"
// envSiaAPIPassword is the environment variable for setting the skyd
// API password.
// nolint:gosec // this is not a credential
envSiaAPIPassword = "SIA_API_PASSWORD"
// envServerDomain is the environment variable for setting the domain of
// the server within the cluster.
envServerDomain = "SERVER_DOMAIN"
)
// parseConfig parses a Config struct from the environment.
func parseConfig() (*config, error) {
// Create config with default vars.
cfg := &config{
LogLevel: logrus.InfoLevel,
SkydOpts: client.Options{
UserAgent: defaultSkydUserAgent,
},
}
// Parse custom vars from environment.
var ok bool
var err error
logLevelStr, ok := os.LookupEnv(envLogLevel)
if ok {
cfg.LogLevel, err = logrus.ParseLevel(logLevelStr)
if err != nil {
return nil, errors.AddContext(err, "failed to parse log level")
}
}
accountsHostStr, ok := os.LookupEnv(envAccountsHost)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envAccountsHost)
}
accountsPortStr, ok := os.LookupEnv(envAccountsPort)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envAccountsPort)
}
cfg.AccountsAPIAddr = fmt.Sprintf("%s:%s", accountsHostStr, accountsPortStr)
cfg.DBURI, ok = os.LookupEnv(envMongoDBURI)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envMongoDBURI)
}
cfg.DBUser, ok = os.LookupEnv(envMongoDBUser)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envMongoDBUser)
}
cfg.DBPassword, ok = os.LookupEnv(envMongoDBPassword)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envMongoDBPassword)
}
cfg.ServerDomain, ok = os.LookupEnv(envServerDomain)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envServerDomain)
}
cfg.SkydOpts.Address, ok = os.LookupEnv(envSkydAPIAddr)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envSkydAPIAddr)
}
userAgent, ok := os.LookupEnv(envSkydAPIUserAgent)
if ok {
cfg.SkydOpts.UserAgent = userAgent
}
cfg.SkydOpts.Password, ok = os.LookupEnv(envSiaAPIPassword)
if !ok {
return nil, fmt.Errorf("%s wasn't specified", envSiaAPIPassword)
}
return cfg, nil
}
func main() {
logger := logrus.New()
// Create application context.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Parse env vars.
cfg, err := parseConfig()
if err != nil {
logger.WithError(err).Fatal("Failed to parse Config")
}
// Create the loggers for the submodules.
logger.SetLevel(cfg.LogLevel)
apiLogger := logger.WithField("modules", "api")
dbLogger := logger.WithField("modules", "promoter")
// Connect to skyd.
skydClient := client.New(cfg.SkydOpts)
_, err = skydClient.DaemonReadyGet()
if err != nil {
logger.WithError(err).Fatal("Failed to connect to skyd")
}
// Connect to accounts.
accountsClient := promoter.NewAccountsClient(cfg.AccountsAPIAddr)
_, err = accountsClient.Health()
if err != nil {
logger.WithError(err).Fatal("Failed to connect to accounts")
}
// Create the promoter that talks to skyd and the database.
db, err := promoter.New(ctx, dependencies.ProdDependencies, accountsClient, skydClient, dbLogger, cfg.DBURI, cfg.DBUser, cfg.DBPassword, cfg.ServerDomain, dbName)
if err != nil {
logger.WithError(err).Fatal("Failed to connect to database")
}
// Create API.
api, err := api.New(apiLogger, db, cfg.Port)
if err != nil {
logger.WithError(err).Fatal("Failed to init API")
}
// Register handler for shutdown.
var wg sync.WaitGroup
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
wg.Add(1)
go func() {
defer wg.Done()
<-sigChan
// Log that we are shutting down.
logger.Info("Caught stop signal. Shutting down...")
// Shut down API with sane timeout.
shutdownCtx, cancel := context.WithTimeout(ctx, envAPIShutdownTimeout)
defer cancel()
if err := api.Shutdown(shutdownCtx); err != nil {
logger.WithError(err).Error("Failed to shut down api")
}
}()
// Start serving API.
err = api.ListenAndServe()
if err != nil && !errors.Contains(err, http.ErrServerClosed) {
logger.WithError(err).Error("ListenAndServe returned an error")
}
// Wait for the goroutine to finish before continuing with the remaining
// shutdown procedures.
wg.Wait()
// Close database.
if err := db.Close(); err != nil {
logger.WithError(err).Fatal("Failed to close database gracefully")
}
}