-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
460 lines (415 loc) · 13.1 KB
/
Copy pathmain.go
File metadata and controls
460 lines (415 loc) · 13.1 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/strahe/synaps3/internal/admin"
"github.com/strahe/synaps3/internal/app"
"github.com/strahe/synaps3/internal/buildinfo"
"github.com/strahe/synaps3/internal/config"
"github.com/strahe/synaps3/internal/db"
"github.com/strahe/synaps3/internal/observability"
"github.com/strahe/synaps3/internal/provider"
"github.com/strahe/synaps3/internal/synapse"
sdk "github.com/strahe/synapse-go"
"github.com/uptrace/bun"
"github.com/urfave/cli/v3"
)
const configEnvVar = "SYNAPS3_CONFIG"
func main() {
if err := newRootCommand().Run(context.Background(), os.Args); err != nil {
fmt.Fprintf(os.Stderr, "fatal: %v\n", err)
os.Exit(1)
}
}
func newRootCommand() *cli.Command {
return &cli.Command{
Name: "synaps3",
Usage: "S3-compatible gateway to Filecoin",
HideVersion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "path to TOML config file; defaults to ~/.synaps3/config.toml",
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
if cmd.Args().Len() > 0 {
return fmt.Errorf("unknown command %q, run --help for available commands", cmd.Args().First())
}
return cli.ShowRootCommandHelp(cmd)
},
Commands: []*cli.Command{
initCommand(),
serveCommand(),
migrateCommand(),
providerCommand(),
walletCommand(),
adminAuthCommand(),
adminCommand(),
versionCommand(),
},
}
}
func initCommand() *cli.Command {
return &cli.Command{
Name: "init",
Usage: "initialize config and runtime directories",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Usage: "app data directory to initialize; defaults to ~/.synaps3",
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
if cmd.Args().Len() > 0 {
return fmt.Errorf("unexpected argument %q, init takes no positional arguments", cmd.Args().First())
}
if cmd.Root().IsSet("config") {
return fmt.Errorf("init does not use --config; use --dir to choose the app data directory")
}
result, err := config.InitAppDataDir(config.InitOptions{Dir: cmd.String("dir")})
if err != nil {
return err
}
return writeInitResult(cmd, result)
},
}
}
func writeInitResult(cmd *cli.Command, result config.InitResult) error {
adminPasswordLine := ""
if shouldPrintInitialAdminPassword(cmd.Root().Writer) {
adminPasswordLine = fmt.Sprintf("Admin username: admin\nAdmin initial password: %s\n", result.AdminInitialPassword)
} else {
passwordPath, err := config.WriteAdminInitialPasswordFile(result.Dir, result.AdminInitialPassword)
if err != nil {
return err
}
adminPasswordLine = fmt.Sprintf("Admin username: admin\nAdmin initial password file: %s\n", passwordPath)
}
output := fmt.Sprintf(
"Initialized SynapS3 app data directory: %s\nConfig: %s\n%sSet filecoin.private_key in the config file or SYNAPS3_FILECOIN_PRIVATE_KEY before serving.\n",
result.Dir,
result.ConfigPath,
adminPasswordLine,
)
if result.DefaultDir {
output += "Next: synaps3 serve\n"
} else {
output += fmt.Sprintf("Next: synaps3 serve --config %s\n", result.ConfigPath)
}
n, err := cmd.Root().Writer.Write([]byte(output))
if err != nil {
return fmt.Errorf("writing init output: %w", err)
}
if n != len(output) {
return io.ErrShortWrite
}
return nil
}
func shouldPrintInitialAdminPassword(w io.Writer) bool {
file, ok := w.(*os.File)
if !ok || file != os.Stdout {
return false
}
info, err := file.Stat()
if err != nil {
return false
}
return info.Mode()&os.ModeCharDevice != 0
}
func serveCommand() *cli.Command {
return &cli.Command{
Name: "serve",
Usage: "start the S3 gateway server",
Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Len() > 0 {
return fmt.Errorf("unexpected argument %q, serve takes no positional arguments", cmd.Args().First())
}
src, err := configSourceFromCommand(cmd)
if err != nil {
return err
}
return runServe(ctx, src)
},
}
}
func migrateCommand() *cli.Command {
return &cli.Command{
Name: "migrate",
Usage: "run database migrations and exit",
Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.Args().Len() > 0 {
return fmt.Errorf("unexpected argument %q, migrate takes no positional arguments", cmd.Args().First())
}
src, err := configSourceFromCommand(cmd)
if err != nil {
return err
}
return runMigrate(ctx, src)
},
}
}
func versionCommand() *cli.Command {
return &cli.Command{
Name: "version",
Usage: "print version information",
Action: func(_ context.Context, _ *cli.Command) error {
fmt.Println(buildinfo.String())
return nil
},
}
}
func configSourceFromCommand(cmd *cli.Command) (config.Source, error) {
path, explicit := configPathFromCommand(cmd)
return config.ResolveSource(path, explicit)
}
func configPathFromCommand(cmd *cli.Command) (string, bool) {
root := cmd.Root()
if root.IsSet("config") {
return root.String("config"), true
}
if envPath := strings.TrimSpace(os.Getenv(configEnvVar)); envPath != "" {
return envPath, true
}
return root.String("config"), false
}
func configSourceExplicitlyProvided(cmd *cli.Command) bool {
_, explicit := configPathFromCommand(cmd)
return explicit
}
// loadConfigAndDB is the shared setup used by serve and migrate.
// It loads config and opens the database but does NOT run full config validation,
// so that commands like "migrate" only need DB-related config (not S3 credentials etc.).
func loadConfigAndDB(ctx context.Context, src config.Source) (*config.Config, *bun.DB, error) {
cfg, err := config.LoadSource(src)
if err != nil {
return nil, nil, fmt.Errorf("loading config: %w", err)
}
database, err := db.New(cfg.Database)
if err != nil {
return nil, nil, fmt.Errorf("opening database: %w", err)
}
if err := db.Ping(ctx, database); err != nil {
_ = database.Close()
return nil, nil, fmt.Errorf("pinging database: %w", err)
}
return cfg, database, nil
}
func runMigrate(ctx context.Context, src config.Source) error {
_, database, err := loadConfigAndDB(ctx, src)
if err != nil {
return err
}
defer func() { _ = database.Close() }()
slog.Info("running database migrations")
if err := db.RunMigrations(ctx, database); err != nil {
return fmt.Errorf("running migrations: %w", err)
}
slog.Info("migrations completed successfully")
return nil
}
func runServe(ctx context.Context, src config.Source) error {
cfg, database, err := loadConfigAndDB(ctx, src)
if err != nil {
return err
}
defer func() { _ = database.Close() }()
settingsSvc, err := admin.NewSettingsService(cfg, src)
if err != nil {
return fmt.Errorf("initializing settings service: %w", err)
}
// Full config validation — only required for serve, not migrate.
if fieldErrs := cfg.FieldValidationErrors(); len(fieldErrs) > 0 {
if shouldStartSetupMode(fieldErrs) {
return runSetupMode(ctx, cfg, settingsSvc, fieldErrs)
}
return fmt.Errorf("validating config: %w", joinFieldErrors(fieldErrs))
}
// Set up structured logging so migration and runtime logs use the configured level/format.
logger := setupLogger(cfg.Logging)
slog.SetDefault(logger)
logger.Info("starting SynapS3",
"version", buildinfo.Version,
"port", cfg.Server.Port,
"network", cfg.Filecoin.Network,
"db_driver", cfg.Database.Driver,
)
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Run migrations on startup.
if err := db.RunMigrations(ctx, database); err != nil {
return fmt.Errorf("running migrations: %w", err)
}
// Initialize Filecoin SDK clients.
client, err := synapse.NewClient(ctx, synapse.ClientConfig{
PrivateKey: cfg.Filecoin.PrivateKey,
RPCURL: cfg.Filecoin.RPCURL,
WithCDN: cfg.Filecoin.WithCDN,
AllowPrivateNetworks: cfg.Filecoin.AllowPrivateNetworks,
Logger: logger,
})
if err != nil {
return fmt.Errorf("initializing Filecoin SDK: %w", err)
}
defer func() { _ = client.Close() }()
resolvedAddresses := client.ResolvedAddresses()
storageClient := synapse.AdaptStorageService(client.Storage())
walletQuerier := synapse.NewWalletQuerier(client.Payments(), client.Address(), client.Chain(), resolvedAddresses)
walletOperator := synapse.NewWalletOperator(client.Payments(), resolvedAddresses.USDFC)
filecoinReadiness := synapse.NewReadinessChecker(
synapse.ReadinessConfigFromFilecoinConfig(cfg.Filecoin),
synapse.AdaptReadinessClient(client),
synapse.WithReadinessLogger(logger),
)
observabilityChecker := newObservabilityChecker(cfg, client, logger)
walletReceiptClient, err := ethclient.DialContext(ctx, cfg.Filecoin.RPCURL)
if err != nil {
return fmt.Errorf("creating wallet receipt client: %w", err)
}
defer walletReceiptClient.Close()
runtime, err := app.NewRuntime(ctx, app.RuntimeOptions{
Config: cfg,
Database: database,
Settings: settingsSvc,
Filecoin: app.FilecoinServices{
Storage: storageClient,
WalletQuery: walletQuerier,
Wallet: walletOperator,
Receipts: walletReceiptClient,
Readiness: filecoinReadiness,
Observability: observabilityChecker,
Terminator: storageClient,
// The epoch comes from the same node that reports wallet receipts,
// so replacement adds no new RPC connection.
Epochs: synapse.NewChainEpochReader(walletReceiptClient),
},
ProviderIdentity: admin.NewProviderIdentityResolver(client.SPRegistry(), cfg.Filecoin.RPCURL, logger),
Logger: logger,
})
if err != nil {
return fmt.Errorf("initializing application runtime: %w", err)
}
return runtime.Run(ctx)
}
func runSetupMode(ctx context.Context, cfg *config.Config, settingsSvc *admin.SettingsService, fieldErrs []config.FieldError) error {
logger := setupLogger(cfg.Logging)
slog.SetDefault(logger)
logger.Warn("starting SynapS3 admin setup mode", "validation_errors", len(fieldErrs), "admin_addr", cfg.Admin.Addr)
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
filecoinReadiness := synapse.NewReadinessChecker(
synapse.ReadinessConfigFromFilecoinConfig(cfg.Filecoin),
nil,
synapse.WithReadinessLogger(logger),
)
adminSrv := admin.NewSetup(cfg.Admin.Addr, settingsSvc, logger).
WithFilecoinReadiness(filecoinReadiness)
if err := adminSrv.WithTrustedProxies(cfg.Admin.TrustedProxies); err != nil {
return fmt.Errorf("initializing admin trusted proxies: %w", err)
}
if err := adminSrv.WithAuthConfig(cfg.Admin.Auth); err != nil {
return fmt.Errorf("initializing admin auth: %w", err)
}
if err := adminSrv.Run(ctx); err != nil {
return fmt.Errorf("admin setup server error: %w", err)
}
return nil
}
func newObservabilityChecker(cfg *config.Config, client *sdk.Client, logger *slog.Logger) observability.RefreshChecker {
providerHealth := provider.NewHealthChecker(nil)
return observability.NewChecker(observability.CheckerOptions{
ProviderSource: observability.NewRegistryProviderSource(provider.NewRegistryService(client.SPRegistry())),
ProviderHealth: providerHealth.Check,
DataSetScanner: observability.NewStorageDataSetScanner(client.Storage()),
Timeout: cfg.Filecoin.Observability.Timeout,
Concurrency: cfg.Filecoin.Observability.Concurrency,
Logger: logger,
})
}
func shouldStartSetupMode(errs []config.FieldError) bool {
if len(errs) == 0 {
return false
}
for _, fieldErr := range errs {
if !setupModeAllowedField(fieldErr.Field) {
return false
}
}
return true
}
func setupModeAllowedField(field string) bool {
switch field {
case "server.port",
"server.max_connections",
"server.max_requests",
"server.tls.cert_file",
"server.tls.key_file",
"s3.region",
"cache.dir",
"cache.max_size_gb",
"cache.eviction_policy",
"cache.lru_high_watermark_percent",
"cache.lru_low_watermark_percent",
"filecoin.network",
"filecoin.rpc_url",
"filecoin.private_key",
"filecoin.default_copies",
"filecoin.observability.interval",
"filecoin.observability.timeout",
"filecoin.observability.concurrency",
"worker.upload.concurrency",
"worker.upload.poll_interval",
"worker.upload.max_retries",
"worker.evictor.concurrency",
"worker.evictor.poll_interval",
"worker.evictor.max_retries",
"worker.storage_cleanup.concurrency",
"worker.storage_cleanup.poll_interval",
"worker.storage_cleanup.max_retries",
"logging.level",
"logging.format",
"logging.s3_access.enabled",
"logging.s3_access.level":
return true
default:
return false
}
}
func joinFieldErrors(fieldErrs []config.FieldError) error {
errs := make([]error, 0, len(fieldErrs))
for _, fieldErr := range fieldErrs {
errs = append(errs, fieldErr)
}
return errors.Join(errs...)
}
func setupLogger(cfg config.LoggingConfig) *slog.Logger {
var level slog.Level
switch cfg.Level {
case "debug":
level = slog.LevelDebug
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
default:
level = slog.LevelInfo
}
opts := &slog.HandlerOptions{Level: level}
var handler slog.Handler
switch cfg.Format {
case "text":
handler = slog.NewTextHandler(os.Stdout, opts)
default:
handler = slog.NewJSONHandler(os.Stdout, opts)
}
return slog.New(handler)
}