Skip to content

Commit

Permalink
initial viper configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Oct 20, 2023
1 parent 9b06242 commit b0f1c25
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20 AS builder
FROM golang:1.21.1 AS builder

WORKDIR /src
COPY . .
Expand Down
84 changes: 64 additions & 20 deletions cmd/census3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,107 @@ import (
"time"

flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/vocdoni/census3/api"
"github.com/vocdoni/census3/db"
"github.com/vocdoni/census3/service"
"github.com/vocdoni/census3/state"
"go.vocdoni.io/dvote/log"
)

type Census3Config struct {
dataDir, logLevel, connectKey string
listOfWeb3Providers []string
port int
}

func main() {
// init service config
config := Census3Config{}
// get home directory to create the data directory for persistent storage
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
home += "/.census3"
dataDir := flag.String("dataDir", home, "data directory for persistent storage")
logLevel := flag.String("logLevel", "info", "log level (debug, info, warn, error)")
port := flag.Int("port", 7788, "HTTP port for the API")
connectKey := flag.String("connectKey", "", "connect group key for IPFS connect")
listOfWeb3Providers := flag.String("web3Providers", "", "the list of URL's of available web3 providers (separated with commas)")
// parse flags
flag.StringVar(&config.dataDir, "dataDir", home, "data directory for persistent storage")
flag.StringVar(&config.logLevel, "logLevel", "info", "log level (debug, info, warn, error)")
flag.IntVar(&config.port, "port", 7788, "HTTP port for the API")
flag.StringVar(&config.connectKey, "connectKey", "", "connect group key for IPFS connect")
var strWeb3Providers string
flag.StringVar(&strWeb3Providers, "web3Providers", "", "the list of URL's of available web3 providers")
flag.Parse()
// init viper to read config file
pviper := viper.New()
pviper.SetConfigName("census3")
pviper.SetConfigType("yml")
pviper.SetEnvPrefix("CENSUS3")
pviper.AutomaticEnv()
pviper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// bind flags to viper
if err := pviper.BindPFlag("dataDir", flag.Lookup("dataDir")); err != nil {
panic(err)
}
config.dataDir = pviper.GetString("dataDir")
// read config file
pviper.AddConfigPath(config.dataDir)
_ = pviper.ReadInConfig()

log.Init(*logLevel, "stdout", nil)
if *listOfWeb3Providers == "" {
if err := pviper.BindPFlag("logLevel", flag.Lookup("logLevel")); err != nil {
panic(err)
}
config.logLevel = pviper.GetString("logLevel")
if err := pviper.BindPFlag("port", flag.Lookup("port")); err != nil {
panic(err)
}
config.port = pviper.GetInt("port")
if err := pviper.BindPFlag("connectKey", flag.Lookup("connectKey")); err != nil {
panic(err)
}
config.connectKey = pviper.GetString("connectKey")
if err := pviper.BindPFlag("web3Providers", flag.Lookup("web3Providers")); err != nil {
panic(err)
}
config.listOfWeb3Providers = strings.Split(pviper.GetString("web3Providers"), ",")
// init logger
log.Init(config.logLevel, "stdout", nil)
log.Info(config)
// check if the web3 providers are defined
if len(config.listOfWeb3Providers) == 0 {
log.Fatal("no web3 providers defined")
}

database, err := db.Init(*dataDir)
// check if the web3 providers are valid
w3p, err := state.CheckWeb3Providers(config.listOfWeb3Providers)
if err != nil {
log.Fatal(err)
}
web3Providers := strings.Split(*listOfWeb3Providers, ",")
w3p, err := state.CheckWeb3Providers(web3Providers)
log.Info(w3p)
// init the database
database, err := db.Init(config.dataDir)
if err != nil {
log.Fatal(err)
}
log.Info(w3p)

// Start the holder scanner
// start the holder scanner
hc, err := service.NewHoldersScanner(database, w3p)
if err != nil {
log.Fatal(err)
}

// Start the API
// start the API
err = api.Init(database, api.Census3APIConf{
Hostname: "0.0.0.0",
Port: *port,
DataDir: *dataDir,
Port: config.port,
DataDir: config.dataDir,
Web3Providers: w3p,
GroupKey: *connectKey,
GroupKey: config.connectKey,
})
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
go hc.Start(ctx)

// Wait for SIGTERM
// wait for SIGTERM
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
Expand Down
8 changes: 1 addition & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ services:
net.core.somaxconn: 8128
volumes:
- census3:/app/data
command:
- "--web3Providers=${WEB3_PROVIDERS}"
- "--port=${PORT}"
- "--logLevel=${LOGLEVEL}"
- "--connectKey=${CONNECT_KEY}"
- "--dataDir=/app/data/census3"
labels:
- "traefik.enable=true"
- "traefik.http.routers.census3.rule=Host(`${DOMAIN}`)"
- "traefik.http.routers.census3.entrypoints=websecure"
- "traefik.http.routers.census3.tls.certresolver=le"
- "traefik.http.routers.census3.service=census3"
- "traefik.http.services.census3.loadbalancer.server.port=${PORT}"
- "traefik.http.services.census3.loadbalancer.server.port=${CENSUS3_PORT}"

traefik:
image: traefik:2.5
Expand Down
88 changes: 49 additions & 39 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module github.com/vocdoni/census3

go 1.20
go 1.21.1

require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2
github.com/ethereum/go-ethereum v1.11.6
github.com/frankban/quicktest v1.14.5
github.com/google/uuid v1.3.0
github.com/frankban/quicktest v1.14.6
github.com/google/uuid v1.3.1
github.com/mattn/go-sqlite3 v1.14.17
github.com/pressly/goose/v3 v3.11.0
github.com/pressly/goose/v3 v3.10.0
github.com/spf13/pflag v1.0.5
go.vocdoni.io/dvote v1.9.1-0.20230913102839-3c074b3046f1
go.vocdoni.io/proto v1.14.6-0.20230802094125-e07a41fda290
github.com/spf13/viper v1.16.0
go.vocdoni.io/dvote v1.9.1-0.20231019083027-0e8e00e106bb
go.vocdoni.io/proto v1.15.4-0.20231017174559-1d9ea54cd9ad
)

require (
Expand All @@ -37,10 +38,10 @@ require (
github.com/cockroachdb/pebble v0.0.0-20230620232302-06034ff014e0 // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230613231145-182959a1fad6 // indirect
github.com/cometbft/cometbft v0.38.0-rc3 // indirect
github.com/cometbft/cometbft v0.38.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/gogoproto v1.4.6 // indirect
github.com/cosmos/gogoproto v1.4.11 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/cskr/pubsub v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -62,7 +63,7 @@ require (
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/glendc/go-external-ip v0.1.0 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-chi/chi/v5 v5.0.10 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
Expand All @@ -80,15 +81,16 @@ require (
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
github.com/google/pprof v0.0.0-20230821062121-407c9e7a662f // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.3 // indirect
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
github.com/holiman/uint256 v1.2.2 // indirect
github.com/huin/goupnp v1.2.0 // indirect
github.com/iden3/go-iden3-crypto v0.0.13 // indirect
Expand All @@ -97,7 +99,7 @@ require (
github.com/iden3/go-rapidsnark/verifier v0.0.3 // indirect
github.com/iden3/go-rapidsnark/witness v0.0.3 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/boxo v0.11.0 // indirect
github.com/ipfs/boxo v0.13.1 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-block-format v0.1.2 // indirect
github.com/ipfs/go-blockservice v0.5.1 // indirect
Expand All @@ -109,9 +111,9 @@ require (
github.com/ipfs/go-ds-leveldb v0.5.0 // indirect
github.com/ipfs/go-ds-measure v0.2.0 // indirect
github.com/ipfs/go-fs-lock v0.0.7 // indirect
github.com/ipfs/go-graphsync v0.14.4 // indirect
github.com/ipfs/go-graphsync v0.15.1 // indirect
github.com/ipfs/go-ipfs-blockstore v1.3.0 // indirect
github.com/ipfs/go-ipfs-cmds v0.9.0 // indirect
github.com/ipfs/go-ipfs-cmds v0.10.0 // indirect
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
github.com/ipfs/go-ipfs-exchange-interface v0.2.0 // indirect
Expand All @@ -129,17 +131,17 @@ require (
github.com/ipfs/go-merkledag v0.11.0 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipfs/go-unixfsnode v1.7.1 // indirect
github.com/ipfs/go-unixfsnode v1.8.1 // indirect
github.com/ipfs/go-verifcid v0.0.2 // indirect
github.com/ipfs/kubo v0.22.0 // indirect
github.com/ipfs/kubo v0.23.0 // indirect
github.com/ipld/go-car v0.5.0 // indirect
github.com/ipld/go-car/v2 v2.10.2-0.20230622090957-499d0c909d33 // indirect
github.com/ipld/go-codec-dagpb v1.6.0 // indirect
github.com/ipld/go-ipld-prime v0.20.0 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -148,24 +150,25 @@ require (
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-doh-resolver v0.4.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.29.2 // indirect
github.com/libp2p/go-libp2p v0.31.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-libp2p-gostream v0.6.0 // indirect
github.com/libp2p/go-libp2p-http v0.5.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.24.2 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.24.4 // indirect
github.com/libp2p/go-libp2p-kbucket v0.6.3 // indirect
github.com/libp2p/go-libp2p-pubsub v0.9.3 // indirect
github.com/libp2p/go-libp2p-pubsub-router v0.6.0 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.7.1 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.7.3 // indirect
github.com/libp2p/go-libp2p-xor v0.1.0 // indirect
github.com/libp2p/go-mplex v0.7.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.2.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/libp2p/go-reuseport v0.3.0 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand All @@ -177,11 +180,12 @@ require (
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.10.1 // indirect
github.com/multiformats/go-multiaddr v0.11.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
Expand All @@ -191,10 +195,11 @@ require (
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
github.com/petermattis/goid v0.0.0-20221018141743-354ef7f2fd21 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -203,23 +208,26 @@ require (
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/statsd_exporter v0.22.8 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.3 // indirect
github.com/quic-go/qtls-go1-20 v0.2.3 // indirect
github.com/quic-go/quic-go v0.36.4 // indirect
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/quic-go/quic-go v0.38.1 // indirect
github.com/quic-go/webtransport-go v0.5.3 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/rs/zerolog v1.30.0 // indirect
github.com/samber/lo v1.36.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
Expand Down Expand Up @@ -256,27 +264,29 @@ require (
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.uber.org/zap v1.25.0 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.11.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gonum.org/v1/gonum v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
pgregory.net/rapid v0.4.8 // indirect
)
Loading

0 comments on commit b0f1c25

Please sign in to comment.