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 <kishansagathiya@gmail.com>
This commit is contained in:
Kishan Sagathiya 2018-11-03 16:15:34 +05:30
parent 3a5ad6111a
commit a741eb42ab

View File

@ -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()
}