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
import (
"log"
"github.com/spf13/viper"
)
type Config struct {
ListenPort string `toml:"listen_port"`
ListenAddr string `toml:"listen_addr"`
Bootstrap bool `toml:"is_bootstrap"`
BootstrapNodeMultiaddr string `toml:"bootstrap_node_multiaddr"`
Rendezvous string `toml:"rendezvous"`
ProtocolID string `toml:"protocol_id"`
SessionKey string `toml:"session_key"`
PrivateKey string `toml:"private_key"`
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"`
SessionKey string `mapstructure:"session_key"`
Etherium EtheriumConfig `mapstructure:"eth"`
PubSub PubSubConfig `mapstructure:"pubSub"`
}
// viperEnvVariable loads config parameters from .env file
func viperEnvString(key string, default_value string) string {
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error while reading config file %s", err)
}
value, ok := viper.Get(key).(string)
if !ok {
return default_value
}
return value
type EtheriumConfig struct {
PrivateKey string `mapstructure:"private_key"`
}
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
type PubSubConfig struct {
ProtocolID string `mapstructure:"protocolID"`
}
// NewConfig creates a new config based on default values or provided .env file
func NewConfig() *Config {
ListenPort := viperEnvString("LISTEN_PORT", ":8000")
ListenAddr := viperEnvString("LISTEN_ADDRESS", "debug")
Bootstrap := viperEnvBoolean("BOOTSTRAP_NODE", false)
BootstrapNodeMultiaddr := viperEnvString("BOOTSTRAP_NODE_MULTIADDRESS", "/ip4/127.0.0.1/tcp/0")
Rendezvous := viperEnvString("RENDEZVOUS", "filecoin-p2p-oracle")
ProtocolID := viperEnvString("PROTOCOL_ID", "p2p-oracle")
SessionKey := viperEnvString("SESSION_KEY", "go")
PrivateKey := viperEnvString("PRIVATE_KEY", "")
return &Config{
ListenPort: ListenPort,
ListenAddr: ListenAddr,
Bootstrap: Bootstrap,
BootstrapNodeMultiaddr: BootstrapNodeMultiaddr,
Rendezvous: Rendezvous,
ProtocolID: ProtocolID,
SessionKey: SessionKey,
PrivateKey: PrivateKey,
func NewConfig(configPath string) (*Config, error) {
cfg := &Config{
ListenAddr: "localhost",
ListenPort: ":8000",
Bootstrap: false,
BootstrapNodeMultiaddr: "/ip4/127.0.0.1/tcp/0",
Rendezvous: "filecoin-p2p-oracle",
Etherium: EtheriumConfig{
PrivateKey: "",
},
SessionKey: "go",
PubSub: PubSubConfig{
ProtocolID: "p2p-oracle",
},
}
viper.SetConfigFile(configPath)
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
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 (
"context"
"crypto/rand"
"flag"
"fmt"
"github.com/Secured-Finance/p2p-oracle-node/config"
@ -34,16 +35,20 @@ type Node struct {
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{
OracleTopic: "p2p_oracle",
Config: config.NewConfig(),
Logger: log.Logger("rendezvous"),
Config: cfg,
Logger: log.Logger("node"),
networkTopics: mapset.NewSet(),
}
log.SetAllLoggers(log.LevelInfo)
return node
return node, nil
}
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)
}
func Start() {
node := NewNode()
log.SetAllLoggers(log.LevelInfo)
func Start() error {
configPath := flag.String("config", "", "Path to config")
verbose := flag.Bool("verbose", false, "Verbose logging")
flag.Parse()
err := log.SetLogLevel("rendezvous", "info")
if err != nil {
node.Logger.Warn("Failed to set a rendezvous log level:", err)
if *configPath == "" {
return fmt.Errorf("no config path provided")
}
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
@ -87,4 +100,5 @@ func Start() {
node.GlobalCtxCancel = ctxCancel
node.setupNode(ctx, prvKey)
return nil
}

View File

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