From b76601e11c564fc17ed955b6492916e936f93c9b Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sat, 5 Feb 2022 21:05:23 +0100 Subject: [PATCH] Support custom client config in Init. Now saved on backend.SaveConfig. --- Config.go | 28 ++++++++++++++++++++++++++++ Peernet.go | 11 +++++++++-- go.mod | 8 ++++---- go.sum | 20 +++++++++++--------- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Config.go b/Config.go index 4bc10f6..17b4994 100644 --- a/Config.go +++ b/Config.go @@ -104,8 +104,36 @@ func SaveConfig(Filename string, Config interface{}) (err error) { return ioutil.WriteFile(Filename, data, 0666) } +// SaveConfigs stores the multiple configurations into a single file. They are essentially merged. +// It is the callers responsibility to make sure no field collision ensue. +func SaveConfigs(Filename string, Configs ...interface{}) (err error) { + var dataOut []byte + + for n, config := range Configs { + if n > 0 { + dataOut = append(dataOut, []byte("\n\n")...) + } + + data, err := yaml.Marshal(config) + if err != nil { + return err + } + + dataOut = append(dataOut, data...) + } + + return ioutil.WriteFile(Filename, dataOut, 0666) +} + // SaveConfig stores the current runtime config to file. Any foreign settings not present in the Config structure will be deleted. func (backend *Backend) SaveConfig() { + if backend.ConfigClient != nil { + if err := SaveConfigs(backend.ConfigFilename, *backend.Config, backend.ConfigClient); err != nil { + backend.LogError("SaveConfig", "writing config '%s': %v\n", backend.ConfigFilename, err.Error()) + } + return + } + if err := SaveConfig(backend.ConfigFilename, *backend.Config); err != nil { backend.LogError("SaveConfig", "writing config '%s': %v\n", backend.ConfigFilename, err.Error()) } diff --git a/Peernet.go b/Peernet.go index aba62d5..2cf05bb 100644 --- a/Peernet.go +++ b/Peernet.go @@ -21,7 +21,7 @@ import ( // Init initializes the client. If the config file does not exist or is empty, a default one will be created. // The User Agent must be provided in the form "Application Name/1.0". // The returned status is of type ExitX. Anything other than ExitSuccess indicates a fatal failure. -func Init(UserAgent string, ConfigFilename string, Filters *Filters) (backend *Backend, status int, err error) { +func Init(UserAgent string, ConfigFilename string, Filters *Filters, ConfigOut interface{}) (backend *Backend, status int, err error) { if UserAgent == "" { return } @@ -40,6 +40,12 @@ func Init(UserAgent string, ConfigFilename string, Filters *Filters) (backend *B if status, err = LoadConfig(ConfigFilename, &backend.Config); status != ExitSuccess { return nil, status, err } + if ConfigOut != nil { + if status, err = LoadConfig(ConfigFilename, ConfigOut); status != ExitSuccess { + return nil, status, err + } + backend.ConfigClient = ConfigOut + } if err = backend.initLog(); err != nil { return nil, ExitErrorLogInit, err @@ -82,7 +88,8 @@ func (backend *Backend) Connect() { // Global variables and init functions are to be merged. type Backend struct { ConfigFilename string // Filename of the configuration file. - Config *Config // Config + Config *Config // Core configuration + ConfigClient interface{} // Custom configuration from the client Filters Filters // Filters allow to install hooks. userAgent string // User Agent GlobalBlockchainCache *BlockchainCache // Caches blockchains of other peers. diff --git a/go.mod b/go.mod index a8fdedc..ce3db04 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,15 @@ module github.com/PeernetOfficial/core go 1.16 require ( - github.com/IncSW/geoip2 v0.1.1 + github.com/IncSW/geoip2 v0.1.2 github.com/akrylysov/pogreb v0.10.1 github.com/enfipy/locker v1.1.0 github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 github.com/gorilla/websocket v1.4.2 - golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa - golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 - golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 + golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 + golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd + golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b lukechampine.com/blake3 v1.1.7 ) diff --git a/go.sum b/go.sum index 20a3b4b..fb53e45 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/IncSW/geoip2 v0.1.1 h1:afzzYF7n9JbdcPy8aiBSgBJuXi4mTWXZ3z6V3o6Vg34= -github.com/IncSW/geoip2 v0.1.1/go.mod h1:adcasR40vXiUBjtzdaTTKL/6wSf+fgO4M8Gve/XzPUk= +github.com/IncSW/geoip2 v0.1.2 h1:v7iAyDiNZjHES45P1JPM3SMvkw0VNeJtz0XSVxkRwOY= +github.com/IncSW/geoip2 v0.1.2/go.mod h1:adcasR40vXiUBjtzdaTTKL/6wSf+fgO4M8Gve/XzPUk= github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w= github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI= github.com/enfipy/locker v1.1.0 h1:2zVJ0ky7cS1Vjs0x6OQWFiT2dSEiHrI5/O2KCz1fgGc= @@ -12,19 +12,21 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4= -golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 h1:7NCfEGl0sfUojmX78nK9pBJuUlSZWEJA/TwASvfiPLo= -golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE= +golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=