mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2024-11-14 11:56:45 +01:00
Mohamed Sohail
77f127e14a
* feat: add admin/auth api - jwt cookie based auth - /auth - admin/* * add: meta proxy * fix: remove ussd account status from syncer * add: cookie defaults and nuxt init check route * add: pin and address handlers
166 lines
3.3 KiB
Go
166 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
batch_balance "github.com/grassrootseconomics/cic-go/batch_balance"
|
|
"github.com/grassrootseconomics/cic-go/meta"
|
|
cic_net "github.com/grassrootseconomics/cic-go/net"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/grassrootseconomics/cic-go/provider"
|
|
"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"
|
|
)
|
|
|
|
type config struct {
|
|
Db struct {
|
|
Postgres string `koanf:"postgres"`
|
|
Redis string `koanf:"redis"`
|
|
}
|
|
Server struct {
|
|
Address string `koanf:"address"`
|
|
Cors []string `koanf:"cors"`
|
|
}
|
|
Chain struct {
|
|
RpcProvider string `koanf:"rpc"`
|
|
TokenRegistry string `koanf:"index"`
|
|
BalanceResolver string `koanf:"balances_resolver"`
|
|
}
|
|
Syncer struct {
|
|
Enabled bool `koanf:"enabled"`
|
|
}
|
|
Api struct {
|
|
Enabled bool `koan:"enabled"`
|
|
}
|
|
Syncers map[string]string `koanf:"syncers"`
|
|
Meta struct {
|
|
Endpoint string `koanf:"meta"`
|
|
}
|
|
Jwt struct {
|
|
Secret string `koanf:"secret"`
|
|
}
|
|
}
|
|
|
|
type queries struct {
|
|
core goyesql.Queries
|
|
dashboard goyesql.Queries
|
|
public goyesql.Queries
|
|
admin goyesql.Queries
|
|
}
|
|
|
|
func loadConfig(configFilePath string, k *koanf.Koanf) error {
|
|
confFile := file.Provider(configFilePath)
|
|
if err := k.Load(confFile, toml.Parser()); err != nil {
|
|
return err
|
|
}
|
|
if err := k.Load(env.Provider("", ".", func(s string) string {
|
|
return strings.ReplaceAll(strings.ToLower(
|
|
strings.TrimPrefix(s, "")), "_", ".")
|
|
}), nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
err := k.UnmarshalWithConf("", &conf, koanf.UnmarshalConf{Tag: "koanf"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func connectDb(dsn string) error {
|
|
var err error
|
|
db, err = pgxpool.Connect(context.Background(), dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseRedis(dsn string) error {
|
|
var err error
|
|
rClient, err = asynq.ParseRedisURI(dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadProvider(rpcEndpoint string) error {
|
|
var err error
|
|
|
|
rpcProvider, err = provider.NewRpcProvider(rpcEndpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadCicNet(tokenIndex common.Address) error {
|
|
var err error
|
|
|
|
cicnetClient, err = cic_net.NewCicNet(rpcProvider, tokenIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadCicMeta(metaEndpoint string) {
|
|
metaClient = meta.NewCicMeta(metaEndpoint)
|
|
}
|
|
|
|
func loadBatchBalance(balanceResolver common.Address) error {
|
|
var err error
|
|
|
|
batchBalance, err = batch_balance.NewBatchBalance(rpcProvider, balanceResolver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadQueries(sqlFilesPath string) error {
|
|
coreQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/core.sql", sqlFilesPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dashboardQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/dashboard.sql", sqlFilesPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
publicQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/public.sql", sqlFilesPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
adminQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/admin.sql", sqlFilesPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
preparedQueries = &queries{
|
|
core: coreQueries,
|
|
dashboard: dashboardQueries,
|
|
public: publicQueries,
|
|
admin: adminQueries,
|
|
}
|
|
|
|
return nil
|
|
}
|