2023-02-15 08:05:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
2023-03-06 09:18:41 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/sub"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
2023-03-01 12:19:57 +01:00
|
|
|
"github.com/knadh/koanf/v2"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/zerodha/logf"
|
|
|
|
)
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
type internalServicesContainer struct {
|
|
|
|
apiService *echo.Echo
|
|
|
|
jetstreamSub *sub.Sub
|
|
|
|
taskerService *tasker.TaskerServer
|
|
|
|
}
|
2023-03-02 16:46:02 +01:00
|
|
|
|
2023-02-15 08:05:43 +01:00
|
|
|
var (
|
2023-03-08 11:02:56 +01:00
|
|
|
build string
|
|
|
|
|
2023-03-01 12:19:57 +01:00
|
|
|
confFlag string
|
|
|
|
debugFlag bool
|
|
|
|
migrationsFolderFlag string
|
|
|
|
queriesFlag string
|
2023-02-15 08:05:43 +01:00
|
|
|
|
|
|
|
lo logf.Logger
|
|
|
|
ko *koanf.Koanf
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&confFlag, "config", "config.toml", "Config file location")
|
2023-03-08 11:02:56 +01:00
|
|
|
flag.BoolVar(&debugFlag, "debug", false, "Enable debug logging")
|
2023-03-01 12:19:57 +01:00
|
|
|
flag.StringVar(&migrationsFolderFlag, "migrations", "migrations/", "Migrations folder location")
|
|
|
|
flag.StringVar(&queriesFlag, "queries", "queries.sql", "Queries file location")
|
2023-02-15 08:05:43 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
lo = initLogger()
|
|
|
|
ko = initConfig()
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-03-08 15:19:33 +01:00
|
|
|
lo.Info("main: starting cic-custodial", "build", build)
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
parsedQueries := initQueries()
|
|
|
|
celoProvider := initCeloProvider()
|
|
|
|
postgresPool := initPostgresPool()
|
|
|
|
asynqRedisPool := initAsynqRedisPool()
|
|
|
|
redisPool := initCommonRedisPool()
|
|
|
|
|
|
|
|
postgresKeystore := initPostgresKeystore(postgresPool, parsedQueries)
|
|
|
|
pgStore := initPostgresStore(postgresPool, parsedQueries)
|
2023-03-29 18:10:58 +02:00
|
|
|
redisNoncestore := initRedisNoncestore(redisPool)
|
2023-02-15 08:05:43 +01:00
|
|
|
lockProvider := initLockProvider(redisPool.Client)
|
|
|
|
taskerClient := initTaskerClient(asynqRedisPool)
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
natsConn, jsCtx := initJetStream()
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
custodial, err := custodial.NewCustodial(custodial.Opts{
|
|
|
|
CeloProvider: celoProvider,
|
|
|
|
Keystore: postgresKeystore,
|
|
|
|
LockProvider: lockProvider,
|
|
|
|
Noncestore: redisNoncestore,
|
|
|
|
PgStore: pgStore,
|
|
|
|
RedisClient: redisPool.Client,
|
|
|
|
RegistryAddress: ko.MustString("chain.registry_address"),
|
|
|
|
SystemPrivateKey: ko.MustString("system.private_key"),
|
|
|
|
SystemPublicKey: ko.MustString("system.public_key"),
|
|
|
|
TaskerClient: taskerClient,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
lo.Fatal("main: crtical error loading custodial container", "error", err)
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
internalServices := &internalServicesContainer{}
|
2023-02-20 10:56:30 +01:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
signalCh, closeCh := createSigChannel()
|
|
|
|
defer closeCh()
|
|
|
|
|
|
|
|
internalServices.apiService = initApiServer(custodial)
|
2023-02-15 08:05:43 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-03-06 09:18:41 +01:00
|
|
|
host := ko.MustString("service.address")
|
|
|
|
lo.Info("main: starting API server", "host", host)
|
|
|
|
if err := internalServices.apiService.Start(host); err != nil {
|
2023-02-15 08:05:43 +01:00
|
|
|
if strings.Contains(err.Error(), "Server closed") {
|
|
|
|
lo.Info("main: shutting down server")
|
|
|
|
} else {
|
|
|
|
lo.Fatal("main: critical error shutting down server", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
internalServices.taskerService = initTasker(custodial, asynqRedisPool)
|
2023-02-15 08:05:43 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-03-06 09:18:41 +01:00
|
|
|
lo.Info("main: starting tasker")
|
2023-03-02 16:46:02 +01:00
|
|
|
if err := internalServices.taskerService.Start(); err != nil {
|
2023-02-15 08:05:43 +01:00
|
|
|
lo.Fatal("main: could not start task server", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
internalServices.jetstreamSub = initSub(natsConn, jsCtx, custodial)
|
2023-03-02 10:12:39 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-03-06 09:18:41 +01:00
|
|
|
lo.Info("main: starting jetstream sub")
|
|
|
|
if err := internalServices.jetstreamSub.Process(); err != nil {
|
2023-03-02 16:46:02 +01:00
|
|
|
lo.Fatal("main: error running jetstream sub", "err", err)
|
2023-03-02 10:12:39 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
lo.Info("main: graceful shutdown triggered", "signal", <-signalCh)
|
2023-03-02 16:46:02 +01:00
|
|
|
startGracefulShutdown(context.Background(), internalServices)
|
2023-02-15 08:05:43 +01:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|