2020-03-25 15:30:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-yaml/yaml"
|
2020-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-25 15:30:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const userAgentFormat = "/NEO-GO:%s/"
|
|
|
|
|
|
|
|
// Version the version of the node, set at build time.
|
|
|
|
var Version string
|
|
|
|
|
|
|
|
// Config top level struct representing the config
|
|
|
|
// for the node.
|
|
|
|
type Config struct {
|
|
|
|
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
|
|
|
|
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateUserAgent creates user agent string based on build time environment.
|
|
|
|
func (c Config) GenerateUserAgent() string {
|
|
|
|
return fmt.Sprintf(userAgentFormat, Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load attempts to load the config from the given
|
|
|
|
// path for the given netMode.
|
2020-06-14 07:34:50 +00:00
|
|
|
func Load(path string, netMode netmode.Magic) (Config, error) {
|
2020-03-25 15:30:21 +00:00
|
|
|
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
|
2020-07-03 08:40:40 +00:00
|
|
|
return LoadFile(configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFile loads config from the provided path.
|
|
|
|
func LoadFile(configPath string) (Config, error) {
|
2020-03-25 15:30:21 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("config '%s' doesn't exist", configPath)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("unable to read config: %w", err)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
ApplicationConfiguration: ApplicationConfiguration{
|
|
|
|
PingInterval: 30,
|
|
|
|
PingTimeout: 90,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(configData, &config)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
2020-03-25 15:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|