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-02 16:46:02 +01:00
|
|
|
type (
|
|
|
|
internalServiceContainer struct {
|
|
|
|
apiService *echo.Echo
|
2023-03-06 09:18:41 +01:00
|
|
|
jetstreamSub *sub.Sub
|
2023-03-02 16:46:02 +01:00
|
|
|
taskerService *tasker.TaskerServer
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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-02-15 08:05:43 +01:00
|
|
|
redisNoncestore := initRedisNoncestore(redisPool, celoProvider)
|
|
|
|
lockProvider := initLockProvider(redisPool.Client)
|
|
|
|
taskerClient := initTaskerClient(asynqRedisPool)
|
2023-03-02 16:46:02 +01:00
|
|
|
systemContainer := initSystemContainer(context.Background(), redisNoncestore)
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
natsConn, jsCtx := initJetStream()
|
|
|
|
jsPub := initPub(jsCtx)
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
custodial := &custodial.Custodial{
|
|
|
|
CeloProvider: celoProvider,
|
|
|
|
Keystore: postgresKeystore,
|
|
|
|
LockProvider: lockProvider,
|
|
|
|
Noncestore: redisNoncestore,
|
|
|
|
PgStore: pgStore,
|
2023-03-06 09:18:41 +01:00
|
|
|
Pub: jsPub,
|
2023-03-08 07:49:09 +01:00
|
|
|
RedisClient: redisPool.Client,
|
2023-02-20 10:56:30 +01:00
|
|
|
SystemContainer: systemContainer,
|
|
|
|
TaskerClient: taskerClient,
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
internalServices := &internalServiceContainer{}
|
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()
|
|
|
|
}
|