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 {
|
2020-08-19 19:26:48 +00:00
|
|
|
ListenPort string `mapstructure:"listen_port"`
|
|
|
|
ListenAddr string `mapstructure:"listen_addr"`
|
|
|
|
Bootstrap bool `mapstructure:"is_bootstrap"`
|
|
|
|
BootstrapNodeMultiaddr string `mapstructure:"bootstrap_node_multiaddr"`
|
|
|
|
Rendezvous string `mapstructure:"rendezvous"`
|
2020-10-20 18:58:24 +00:00
|
|
|
Ethereum EthereumConfig `mapstructure:"ethereum"`
|
2020-10-20 18:18:36 +00:00
|
|
|
Filecoin FilecoinConfig `mapstructure:"filecoin"`
|
2020-08-19 19:26:48 +00:00
|
|
|
PubSub PubSubConfig `mapstructure:"pubSub"`
|
2020-10-31 03:48:15 +00:00
|
|
|
Store StoreConfig `mapstructure:"store"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:58:24 +00:00
|
|
|
type EthereumConfig struct {
|
2020-10-20 18:18:36 +00:00
|
|
|
GatewayAddress string `mapstructure:"gateway_address"`
|
|
|
|
PrivateKey string `mapstructure:"private_key"`
|
|
|
|
OracleEmitterContractAddress string `mapstructure:"oracle_emitter_contract_address"`
|
|
|
|
AggregatorContractAddress string `mapstructure:"aggregator_contract_address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ProtocolID string `mapstructure:"protocolID"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 03:48:15 +00:00
|
|
|
type StoreConfig struct {
|
|
|
|
DatabaseURL string `mapstructure:"database_url"`
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
ListenAddr: "localhost",
|
|
|
|
ListenPort: ":8000",
|
|
|
|
Bootstrap: false,
|
|
|
|
BootstrapNodeMultiaddr: "/ip4/127.0.0.1/tcp/0",
|
|
|
|
Rendezvous: "filecoin-p2p-oracle",
|
2020-10-20 18:58:24 +00:00
|
|
|
Ethereum: EthereumConfig{
|
2020-08-19 19:26:48 +00:00
|
|
|
PrivateKey: "",
|
|
|
|
},
|
|
|
|
PubSub: PubSubConfig{
|
|
|
|
ProtocolID: "p2p-oracle",
|
|
|
|
},
|
2020-10-31 03:48:15 +00:00
|
|
|
Store: StoreConfig{
|
|
|
|
DatabaseURL: dbURL,
|
|
|
|
},
|
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
|
|
|
}
|