Refactor configuration system of the node

This commit is contained in:
ChronosX88 2020-08-19 23:26:48 +04:00
parent ab2f8639c4
commit 1b71a23e73
5 changed files with 88 additions and 98 deletions

View File

@ -1,74 +1,55 @@
package config package config
import ( import (
"log"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type Config struct { type Config struct {
ListenPort string `toml:"listen_port"` ListenPort string `mapstructure:"listen_port"`
ListenAddr string `toml:"listen_addr"` ListenAddr string `mapstructure:"listen_addr"`
Bootstrap bool `toml:"is_bootstrap"` Bootstrap bool `mapstructure:"is_bootstrap"`
BootstrapNodeMultiaddr string `toml:"bootstrap_node_multiaddr"` BootstrapNodeMultiaddr string `mapstructure:"bootstrap_node_multiaddr"`
Rendezvous string `toml:"rendezvous"` Rendezvous string `mapstructure:"rendezvous"`
ProtocolID string `toml:"protocol_id"` SessionKey string `mapstructure:"session_key"`
SessionKey string `toml:"session_key"` Etherium EtheriumConfig `mapstructure:"eth"`
PrivateKey string `toml:"private_key"` PubSub PubSubConfig `mapstructure:"pubSub"`
} }
// viperEnvVariable loads config parameters from .env file type EtheriumConfig struct {
func viperEnvString(key string, default_value string) string { PrivateKey string `mapstructure:"private_key"`
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error while reading config file %s", err)
} }
value, ok := viper.Get(key).(string) type PubSubConfig struct {
ProtocolID string `mapstructure:"protocolID"`
if !ok {
return default_value
}
return value
}
func viperEnvBoolean(key string, default_value bool) bool {
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error while reading config file %s", err)
}
value := viper.GetBool(key)
return value
} }
// NewConfig creates a new config based on default values or provided .env file // NewConfig creates a new config based on default values or provided .env file
func NewConfig() *Config { func NewConfig(configPath string) (*Config, error) {
ListenPort := viperEnvString("LISTEN_PORT", ":8000") cfg := &Config{
ListenAddr := viperEnvString("LISTEN_ADDRESS", "debug") ListenAddr: "localhost",
Bootstrap := viperEnvBoolean("BOOTSTRAP_NODE", false) ListenPort: ":8000",
BootstrapNodeMultiaddr := viperEnvString("BOOTSTRAP_NODE_MULTIADDRESS", "/ip4/127.0.0.1/tcp/0") Bootstrap: false,
Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle") BootstrapNodeMultiaddr: "/ip4/127.0.0.1/tcp/0",
ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle") Rendezvous: "filecoin-p2p-oracle",
SessionKey := viperEnvString("SESSION_KEY", "go") Etherium: EtheriumConfig{
PrivateKey := viperEnvString("PRIVATE_KEY", "") PrivateKey: "",
},
SessionKey: "go",
PubSub: PubSubConfig{
ProtocolID: "p2p-oracle",
},
}
return &Config{ viper.SetConfigFile(configPath)
ListenPort: ListenPort, err := viper.ReadInConfig()
ListenAddr: ListenAddr, if err != nil {
Bootstrap: Bootstrap, return nil, err
BootstrapNodeMultiaddr: BootstrapNodeMultiaddr,
Rendezvous: Rendezvous,
ProtocolID: ProtocolID,
SessionKey: SessionKey,
PrivateKey: PrivateKey,
} }
err = viper.Unmarshal(cfg)
if err != nil {
return nil, err
}
return cfg, nil
} }

19
node/config.go Normal file
View File

@ -0,0 +1,19 @@
package node
type Config struct {
EthPrivateKey string `toml:"ethPrivateKey"`
ListenAddress string `toml:"listenAddress"`
ListenPort string `toml:"listenPort"`
BootstrapAddress string `toml:"bootstrapAddress"`
RendezvousString string `toml:"rendezvousString"`
PubSub PubSubConfig `toml:"pubSub"`
}
type PubSubConfig struct {
ProtocolID string `toml:"protocolID"`
}
type EtheriumConfig struct {
PrivateKey string `toml:"privateKey"`
GatewayAddress string `toml:"gatewayAddress"`
}

View File

@ -1,28 +0,0 @@
package node
import (
"flag"
"github.com/Secured-Finance/p2p-oracle-node/config"
)
func (node *Node) parseFlags() {
listenPort := flag.String("port", node.Config.ListenPort, "Listen port number")
listenAddr := flag.String("addr", node.Config.ListenAddr, "Listen address")
bootstrap := flag.Bool("bootstrap", node.Config.Bootstrap, "Start up bootstrap node")
bootstrapAddress := flag.String("baddr", node.Config.BootstrapNodeMultiaddr, "Address of bootstrap node")
rendezvousString := flag.String("rendezvous", node.Config.Rendezvous, "DHT rendezvous string")
protocolID := flag.String("protocol-id", node.Config.ProtocolID, "PubSub protocol ID")
flag.Parse()
new_config := &config.Config{
ListenPort: *listenPort,
ListenAddr: *listenAddr,
Bootstrap: *bootstrap,
BootstrapNodeMultiaddr: *bootstrapAddress,
Rendezvous: *rendezvousString,
ProtocolID: *protocolID,
}
node.Config = new_config
}

View File

@ -3,6 +3,7 @@ package node
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"flag"
"fmt" "fmt"
"github.com/Secured-Finance/p2p-oracle-node/config" "github.com/Secured-Finance/p2p-oracle-node/config"
@ -34,16 +35,20 @@ type Node struct {
Ethereum *rpcclient.EthereumClient Ethereum *rpcclient.EthereumClient
} }
func NewNode() *Node { func NewNode(configPath string) (*Node, error) {
cfg, err := config.NewConfig(configPath)
if err != nil {
return nil, err
}
node := &Node{ node := &Node{
OracleTopic: "p2p_oracle", OracleTopic: "p2p_oracle",
Config: config.NewConfig(), Config: cfg,
Logger: log.Logger("rendezvous"), Logger: log.Logger("node"),
networkTopics: mapset.NewSet(), networkTopics: mapset.NewSet(),
} }
log.SetAllLoggers(log.LevelInfo) log.SetAllLoggers(log.LevelInfo)
return node return node, nil
} }
func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) { func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) {
@ -63,16 +68,24 @@ func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) {
node.startPubSub(ctx, host) node.startPubSub(ctx, host)
} }
func Start() { func Start() error {
node := NewNode() configPath := flag.String("config", "", "Path to config")
log.SetAllLoggers(log.LevelInfo) verbose := flag.Bool("verbose", false, "Verbose logging")
flag.Parse()
err := log.SetLogLevel("rendezvous", "info") if *configPath == "" {
if err != nil { return fmt.Errorf("no config path provided")
node.Logger.Warn("Failed to set a rendezvous log level:", err)
} }
node.parseFlags() node, err := NewNode(*configPath)
if *verbose {
log.SetAllLoggers(log.LevelDebug)
} else {
log.SetAllLoggers(log.LevelInfo)
}
if err != nil {
log.Logger("node").Panic(err)
}
r := rand.Reader r := rand.Reader
@ -87,4 +100,5 @@ func Start() {
node.GlobalCtxCancel = ctxCancel node.GlobalCtxCancel = ctxCancel
node.setupNode(ctx, prvKey) node.setupNode(ctx, prvKey)
return nil
} }

View File

@ -2,8 +2,12 @@ package main
import ( import (
"github.com/Secured-Finance/p2p-oracle-node/node" "github.com/Secured-Finance/p2p-oracle-node/node"
"github.com/ipfs/go-log"
) )
func main() { func main() {
node.Start() err := node.Start()
if err != nil {
log.Logger("node").Panic(err)
}
} }