2022-04-25 17:29:38 +02:00
|
|
|
package main
|
2022-05-03 17:54:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-05 14:01:34 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-05-06 15:53:57 +02:00
|
|
|
"github.com/grassrootseconomics/cic_go/cic_net"
|
2022-05-03 17:54:51 +02:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
"github.com/knadh/koanf"
|
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
"github.com/nleof/goyesql"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
type config struct {
|
|
|
|
Db struct {
|
|
|
|
Postgres string `koanf:"postgres"`
|
|
|
|
Redis string `koanf:"redis"`
|
|
|
|
}
|
|
|
|
Chain struct {
|
|
|
|
RpcProvider string `koanf:"rpc"`
|
|
|
|
TokenRegistry string `koanf:"index"`
|
|
|
|
}
|
|
|
|
Syncers map[string]string `koanf:"syncers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig(configFilePath string, k *koanf.Koanf) error {
|
2022-05-03 17:54:51 +02:00
|
|
|
confFile := file.Provider(configFilePath)
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := k.Load(confFile, toml.Parser()); err != nil {
|
2022-05-03 17:54:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := k.Load(env.Provider("", ".", func(s string) string {
|
2022-05-03 17:54:51 +02:00
|
|
|
return strings.ReplaceAll(strings.ToLower(
|
2022-05-05 14:01:34 +02:00
|
|
|
strings.TrimPrefix(s, "")), "_", ".")
|
2022-05-03 17:54:51 +02:00
|
|
|
}), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
err := k.UnmarshalWithConf("", &conf, koanf.UnmarshalConf{Tag: "koanf"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
func connectDb(dsn string) error {
|
2022-05-05 14:01:34 +02:00
|
|
|
var err error
|
|
|
|
db, err = pgxpool.Connect(context.Background(), dsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRedis(dsn string) (asynq.RedisConnOpt, error) {
|
|
|
|
rconn, err := asynq.ParseRedisURI(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-05 18:39:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
return rconn, nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
func connectCicNet(rpcProvider string, tokenIndex common.Address) error {
|
|
|
|
var err error
|
|
|
|
|
2022-05-06 15:53:57 +02:00
|
|
|
cicnetClient, err = cic_net.NewCicNet(rpcProvider, tokenIndex)
|
2022-05-03 17:54:51 +02:00
|
|
|
if err != nil {
|
2022-05-05 14:01:34 +02:00
|
|
|
return err
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
return nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
func loadQueries(sqlFile string) error {
|
|
|
|
var err error
|
|
|
|
queries, err = goyesql.ParseFile(sqlFile)
|
2022-05-03 17:54:51 +02:00
|
|
|
if err != nil {
|
2022-05-05 14:01:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
func bootstrapScheduler(redis asynq.RedisConnOpt) (*asynq.Scheduler, error) {
|
2022-05-05 14:01:34 +02:00
|
|
|
scheduler := asynq.NewScheduler(redis, nil)
|
|
|
|
|
|
|
|
for k, v := range conf.Syncers {
|
|
|
|
task := asynq.NewTask(k, nil)
|
|
|
|
|
|
|
|
_, err := scheduler.Register(v, task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("successfully registered %s syncer", k)
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
return scheduler, nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
func bootstrapProcessor(redis asynq.RedisConnOpt) (*asynq.Server, *asynq.ServeMux) {
|
2022-05-05 14:01:34 +02:00
|
|
|
processorServer := asynq.NewServer(
|
|
|
|
redis,
|
|
|
|
asynq.Config{
|
|
|
|
Concurrency: 5,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mux := asynq.NewServeMux()
|
|
|
|
mux.HandleFunc("token", tokenSyncer)
|
|
|
|
mux.HandleFunc("cache", cacheSyncer)
|
|
|
|
mux.HandleFunc("ussd", ussdSyncer)
|
2022-05-03 17:54:51 +02:00
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
return processorServer, mux
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|