2020-08-03 20:01:38 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-10-31 03:48:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-03 20:01:38 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-05-26 20:59:05 +00:00
|
|
|
ListenPort int `mapstructure:"listen_port"`
|
|
|
|
ListenAddr string `mapstructure:"listen_addr"`
|
|
|
|
IsBootstrap bool `mapstructure:"is_bootstrap"`
|
|
|
|
BootstrapNodes []string `mapstructure:"bootstrap_node_multiaddr"`
|
|
|
|
Rendezvous string `mapstructure:"rendezvous"`
|
|
|
|
Ethereum EthereumConfig `mapstructure:"ethereum"`
|
|
|
|
Filecoin FilecoinConfig `mapstructure:"filecoin"`
|
|
|
|
PubSub PubSubConfig `mapstructure:"pubsub"`
|
|
|
|
Store StoreConfig `mapstructure:"store"`
|
|
|
|
ConsensusMinApprovals int `mapstructure:"consensus_min_approvals"`
|
|
|
|
Redis RedisConfig `mapstructure:"redis"`
|
|
|
|
CacheType string `mapstructure:"cache_type"`
|
|
|
|
Blockchain BlockchainConfig `mapstructure:"blockchain"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:58:24 +00:00
|
|
|
type EthereumConfig struct {
|
2021-03-05 19:29:09 +00:00
|
|
|
GatewayAddress string `mapstructure:"gateway_address"`
|
2021-04-14 21:04:21 +00:00
|
|
|
ChainID int `mapstructure:"chain_id"`
|
2021-03-05 19:29:09 +00:00
|
|
|
PrivateKey string `mapstructure:"private_key"`
|
2021-04-14 21:04:21 +00:00
|
|
|
MnemonicPhrase string `mapstructure:"mnemonic_phrase"`
|
|
|
|
HDDerivationPath string `mapstructure:"hd_derivation_path"`
|
2021-04-19 19:46:17 +00:00
|
|
|
DioneOracleContractAddress string `mapstructure:"oracle_contract_address"`
|
|
|
|
DioneStakingContractAddress string `mapstructure:"staking_contract_address"`
|
2021-03-05 19:29:09 +00:00
|
|
|
DisputeContractAddress string `mapstructure:"dispute_contract_address"`
|
2021-04-20 21:38:54 +00:00
|
|
|
DisputeVoteWindow int `mapstructure:"dispute_vote_window"` // in secs
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FilecoinConfig struct {
|
|
|
|
LotusHost string `mapstructure:"lotusHost"`
|
|
|
|
LotusToken string `mapstructure:"lotusToken"`
|
2020-08-19 19:26:48 +00:00
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
type PubSubConfig struct {
|
2021-05-26 20:59:05 +00:00
|
|
|
ServiceTopicName string `mapstructure:"service_topic_name"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 03:48:15 +00:00
|
|
|
type StoreConfig struct {
|
|
|
|
DatabaseURL string `mapstructure:"database_url"`
|
|
|
|
}
|
|
|
|
|
2021-03-05 19:29:09 +00:00
|
|
|
type RedisConfig struct {
|
|
|
|
Addr string `mapstructure:"redis_addr"`
|
|
|
|
Password string `mapstructure:"redis_password"`
|
|
|
|
DB int `mapstructure:"redis_db"`
|
|
|
|
}
|
|
|
|
|
2021-05-26 20:59:05 +00:00
|
|
|
type BlockchainConfig struct {
|
|
|
|
DatabasePath string `mapstructure:"database_path"`
|
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
// NewConfig creates a new config based on default values or provided .env file
|
|
|
|
func NewConfig(configPath string) (*Config, error) {
|
2020-10-31 03:48:15 +00:00
|
|
|
dbName := "dione"
|
|
|
|
username := "user"
|
|
|
|
password := "password"
|
|
|
|
dbURL := fmt.Sprintf("host=localhost user=%s password=%s dbname=%s sslmode=disable", username, password, dbName)
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
cfg := &Config{
|
2020-11-04 17:46:35 +00:00
|
|
|
ListenAddr: "localhost",
|
2020-11-04 18:17:01 +00:00
|
|
|
ListenPort: 8000,
|
2020-11-04 17:46:35 +00:00
|
|
|
BootstrapNodes: []string{"/ip4/127.0.0.1/tcp/0"},
|
2021-05-26 20:59:05 +00:00
|
|
|
Rendezvous: "dione",
|
2020-10-20 18:58:24 +00:00
|
|
|
Ethereum: EthereumConfig{
|
2020-08-19 19:26:48 +00:00
|
|
|
PrivateKey: "",
|
|
|
|
},
|
|
|
|
PubSub: PubSubConfig{
|
2021-05-26 20:59:05 +00:00
|
|
|
ServiceTopicName: "dione",
|
2020-08-19 19:26:48 +00:00
|
|
|
},
|
2020-10-31 03:48:15 +00:00
|
|
|
Store: StoreConfig{
|
|
|
|
DatabaseURL: dbURL,
|
|
|
|
},
|
2021-03-05 19:29:09 +00:00
|
|
|
Redis: RedisConfig{
|
|
|
|
Addr: "redisDB:6379",
|
|
|
|
Password: "",
|
|
|
|
DB: 0,
|
|
|
|
},
|
|
|
|
CacheType: "in-memory",
|
2020-08-19 19:26:48 +00:00
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
viper.SetConfigFile(configPath)
|
2020-08-03 20:01:38 +00:00
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2020-08-19 19:26:48 +00:00
|
|
|
return nil, err
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
err = viper.Unmarshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
2020-08-19 19:26:48 +00:00
|
|
|
|
|
|
|
return cfg, nil
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|