Skip to content

Commit

Permalink
viper bind flags to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Omidiora Samuel authored and Omidiora Samuel committed May 29, 2018
1 parent 2345503 commit 2c829e2
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 298 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Yosher Lutski <[email protected]>
Kare Nuorteva <[email protected]>
David Gonzalez <[email protected]>
Aviv Eyal <[email protected]>
Omidiora Samuel <[email protected]>

24 changes: 10 additions & 14 deletions api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@ package config

const (
defaultStartGRPCServer = false
defaultGRPCServerPort = 9091
defaultGRPCServerPort = 9091
defaultStartJSONServer = false
defaultJSONServerPort = 9090
defaultJSONServerPort = 9090
)

// Config defines the api config params
type Config struct {
StartGrpcServer bool
GrpcServerPort int
StartJSONServer bool
JSONServerPort int
StartGrpcServer bool `mapstructure:"grpc-server"`
GrpcServerPort int `mapstructure:"grpc-port"`
StartJSONServer bool `mapstructure:"json-server"`
JSONServerPort int `mapstructure:"json-port"`
}

var ConfigValues = Config {
StartGrpcServer: false, // note: all bool flags default to false so don't set one of these to true here
GrpcServerPort: 9091,
StartJSONServer: false,
JSONServerPort: 9090,
}
// ConfigValues set default values
var ConfigValues = DefaultConfig()

func init() {
// todo: update default config params based on runtime env here
}

// DefaultConfig defines the default configuration options for api
func DefaultConfig() *Config {
return &Config{
func DefaultConfig() Config {
return Config{
StartGrpcServer: defaultStartGRPCServer, // note: all bool flags default to false so don't set one of these to true here
GrpcServerPort: defaultGRPCServerPort,
StartJSONServer: defaultStartJSONServer,
Expand Down
13 changes: 10 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"testing"

"github.com/spacemeshos/go-spacemesh/assert"
"github.com/spacemeshos/go-spacemesh/config"
"github.com/spacemeshos/go-spacemesh/filesystem"
)

func TestApp(t *testing.T) {

filesystem.SetupTestSpacemeshDataFolders(t, "app_test")

// remove all injected test flags for now
os.Args = []string{"/go-spacemesh", "-json-server"}
os.Args = []string{"/go-spacemesh", "--json-server=true"}

go Main("", "master", "")
go Main()

<-EntryPointCreated

Expand All @@ -32,3 +32,10 @@ func TestApp(t *testing.T) {
filesystem.DeleteSpacemeshDataFolders(t)

}

func TestParseConfig(t *testing.T) {
err := config.LoadConfig("./config.toml")
assert.Nil(t, err)
_, err = ParseConfig()
assert.Nil(t, err)
}
76 changes: 76 additions & 0 deletions app/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
cfg "github.com/spacemeshos/go-spacemesh/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
config = cfg.DefaultConfig()
)

// RootCmd is the application root command
var RootCmd = &cobra.Command{
Use: "spacemesh",
Short: "Spacemesh Core ( PoST )",
}

func init() {

/** ======================== BaseConfig Flags ========================== **/
RootCmd.PersistentFlags().StringVarP(&config.BaseConfig.ConfigFile,
"config", "c", config.BaseConfig.ConfigFile, "Set Load configuration from file")
RootCmd.PersistentFlags().StringVarP(&config.BaseConfig.DataDir, "datadir", "d",
config.BaseConfig.DataDir, "Specify data directory for spacemesh")

/** ======================== P2P Flags ========================== **/
RootCmd.PersistentFlags().IntVar(&config.P2P.SecurityParam, "security-param",
config.P2P.SecurityParam, "Consensus protocol k security param")
RootCmd.PersistentFlags().IntVar(&config.P2P.TCPPort, "tcp-port",
config.P2P.SecurityParam, "TCP Port to listen on")
//RootCmd.PersistentFlags().StringVar(&config.P2P.DialTimeout.string, "dial-timeout",
// config.P2P.DialTimeout.string, "Network dial timeout duration")
//RootCmd.PersistentFlags().StringVar(&config.P2P.ConnKeepAlive.string, "conn-keepalive",
// config.P2P.ConnKeepAlive.string,"Network connection keep alive")
RootCmd.PersistentFlags().IntVar(&config.P2P.NetworkID, "network-id",
config.P2P.NetworkID, "NetworkID to run on (0 - mainnet, 1 - testnet)")
RootCmd.PersistentFlags().StringVar(&config.P2P.NodeID, "node-id",
config.P2P.NodeID, "Load node data by id (pub key) from local store")
RootCmd.PersistentFlags().BoolVar(&config.P2P.SwarmConfig.Bootstrap, "swarm-bootstrap",
config.P2P.SwarmConfig.Bootstrap, "Bootstrap the swarm")
RootCmd.PersistentFlags().IntVar(&config.P2P.SwarmConfig.RoutingTableBucketSize, "swarm-rtbs",
config.P2P.SwarmConfig.RoutingTableBucketSize, "The rounding table bucket size")
RootCmd.PersistentFlags().IntVar(&config.P2P.SwarmConfig.RoutingTableAlpha, "swarm-rtalpha",
config.P2P.SwarmConfig.RoutingTableAlpha, "The rounding table Alpha")
RootCmd.PersistentFlags().IntVar(&config.P2P.SwarmConfig.RandomConnections, "swarm-randcon",
config.P2P.SwarmConfig.RoutingTableAlpha, "Number of random connections")
//RootCmd.PersistentFlags().StringSliceVar(&config.P2P.SwarmConfig.BootstrapNodes, "swarm-bootstrap-nodes",
// config.P2P.SwarmConfig.BootstrapNodes, "Number of random connections")
//RootCmd.PersistentFlags().StringVar(&config.P2P.TimeConfig.MaxAllowedDrift, "max-allowed-time-drift",
// config.P2P.TimeConfig.MaxAllowedDrift, "When to close the app until user resolves time sync problems")
RootCmd.PersistentFlags().IntVar(&config.P2P.TimeConfig.NtpQueries, "ntp-queries",
config.P2P.TimeConfig.NtpQueries, "Number of random connections")

/** ======================== API Flags ========================== **/
// StartJSONApiServerFlag determines if json api server should be started
RootCmd.PersistentFlags().BoolVar(&config.API.StartJSONServer, "json-server",
config.API.StartJSONServer, "StartService the json http server. "+
"Note that starting the Json server also starts the grpc server.",
)
// JSONServerPortFlag determines the json api server local listening port
RootCmd.PersistentFlags().IntVar(&config.API.JSONServerPort, "json-port",
config.API.JSONServerPort, "JSON api server port")
// StartGrpcAPIServerFlag determines if the grpc server should be started
RootCmd.PersistentFlags().BoolVar(&config.API.StartGrpcServer, "grpc-server",
config.API.StartGrpcServer, "StartService the grpc server")
// GrpcServerPortFlag determines the grpc server local listening port
RootCmd.PersistentFlags().IntVar(&config.API.GrpcServerPort, "grpc-port",
config.API.GrpcServerPort, "GRPC api server port")

RootCmd.AddCommand(VersionCmd)

// Bind Flags to config
viper.BindPFlags(RootCmd.PersistentFlags())

}
23 changes: 23 additions & 0 deletions app/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"
"github.com/spacemeshos/go-spacemesh/version"
"github.com/spf13/cobra"
)

const (
appUsage = "the go-spacemesh node"
appAuthor = "The go-spacemesh authors"
appAuthorEmail = "[email protected]"
appCopyrightNotice = "(c) 2017 The go-spacemesh Authors"
)

// VersionCmd returns the current version of spacemesh
var VersionCmd = &cobra.Command{
Use: "version",
Short: "Show version info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.Version)
},
}
5 changes: 3 additions & 2 deletions app/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package config

// test app config.
// todo: start the real app (not a test app) with a config file and verify that config params are loaded from the file.
//
//// test app config.
//// todo: start the real app (not a test app) with a config file and verify that config params are loaded from the file.
1 change: 1 addition & 0 deletions app/config/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## To be deprecated in favour of config
Loading

0 comments on commit 2c829e2

Please sign in to comment.