From a741eb42abef3474961bbebfbfca020b8365ee99 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Sat, 3 Nov 2018 16:15:34 +0530 Subject: [PATCH] Issue #453 Extract the IPFS Proxy from ipfshttp We want to maintain configuration compatibility and still recognize the proxy configuration options in the ipfshttp connector configuration section. Check if there is a proxy configuration section. If not create one with the options from the ipfshttp section (if they are defined, otherwise do nothing). This implementation utilizes the fact that json object can be unmarshelled to a struct whose elements are a subset of the json object License: MIT Signed-off-by: Kishan Mohanbhai Sagathiya --- config/config.go | 60 +++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/config/config.go b/config/config.go index 2274c741..e88310ac 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,5 @@ // Package config provides interfaces and utilities for different Cluster -// components to register, read, write and validate configuration sections +// components to register, read, write and validate configuration sections // stored in a central configuration file. package config @@ -282,22 +282,30 @@ func (cfg *Manager) LoadJSON(bs []byte) error { cfg.clusterConfig.LoadJSON([]byte(*jcfg.Cluster)) } + loadCompJSON := func(name string, component ComponentConfig, jsonSection jsonSection) error { + raw, ok := jsonSection[name] + if ok { + component.SetBaseDir(dir) + err := component.LoadJSON([]byte(*raw)) + if err != nil { + return err + } + logger.Debugf("%s section configuration loaded", name) + } else { + logger.Warningf("%s section is empty, generating default", name) + component.SetBaseDir(dir) + component.Default() + } + + return nil + } // Helper function to load json from each section in the json config - loadCompJSON := func(section Section, jsonSection jsonSection) error { + loadSectionJSON := func(section Section, jsonSection jsonSection) error { for name, component := range section { - raw, ok := jsonSection[name] - if ok { - component.SetBaseDir(dir) - err := component.LoadJSON([]byte(*raw)) - if err != nil { - logger.Error(err) - return err - } - logger.Debugf("%s section configuration loaded", name) - } else { - logger.Warningf("%s section is empty, generating default", name) - component.SetBaseDir(dir) - component.Default() + err := loadCompJSON(name, component, jsonSection) + if err != nil { + logger.Error(err) + return err } } return nil @@ -306,15 +314,19 @@ func (cfg *Manager) LoadJSON(bs []byte) error { sections := cfg.sections // will skip checking errors and trust Validate() - loadCompJSON(sections[Consensus], jcfg.Consensus) - loadCompJSON(sections[API], jcfg.API) - loadCompJSON(sections[IPFSConn], jcfg.IPFSConn) - loadCompJSON(sections[State], jcfg.State) - loadCompJSON(sections[PinTracker], jcfg.PinTracker) - loadCompJSON(sections[Monitor], jcfg.Monitor) - loadCompJSON(sections[Allocator], jcfg.Allocator) - loadCompJSON(sections[Informer], jcfg.Informer) - loadCompJSON(sections[Sharder], jcfg.Informer) + loadSectionJSON(sections[Consensus], jcfg.Consensus) + loadSectionJSON(sections[API], jcfg.API) + // Should we change hardcoded "ipfsproxy" to something else + if _, ok := jcfg.API["ipfsproxy"]; !ok { + loadCompJSON("ipfsproxy", sections[API]["ipfsproxy"], jcfg.IPFSConn) + } + loadSectionJSON(sections[IPFSConn], jcfg.IPFSConn) + loadSectionJSON(sections[State], jcfg.State) + loadSectionJSON(sections[PinTracker], jcfg.PinTracker) + loadSectionJSON(sections[Monitor], jcfg.Monitor) + loadSectionJSON(sections[Allocator], jcfg.Allocator) + loadSectionJSON(sections[Informer], jcfg.Informer) + loadSectionJSON(sections[Sharder], jcfg.Informer) return cfg.Validate() }