From fc8b98f8151e235d475c75d66dddb254a3e30880 Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 28 Aug 2020 15:43:34 +0300 Subject: [PATCH] Add "configs" section in config --- config/config.go | 13 ++++++++++++- config/config_defaults.go | 7 +++++++ config/config_json.go | 15 ++++++++++++++- config/config_test.go | 9 ++++++++- config/configs.go | 39 +++++++++++++++++++++++++++++++++++++++ config/libconfig_json.go | 5 +++++ 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 config/configs.go diff --git a/config/config.go b/config/config.go index a746dd96d..fcda4932a 100755 --- a/config/config.go +++ b/config/config.go @@ -189,6 +189,7 @@ func NewDefaultCGRConfig() (cfg *CGRConfig, err error) { cfg.eesCfg.Cache = make(map[string]*CacheParamCfg) cfg.rateSCfg = new(RateSCfg) cfg.sipAgentCfg = new(SIPAgentCfg) + cfg.configSCfg = new(ConfigSCfg) cfg.ConfigReloads = make(map[string]chan struct{}) @@ -311,6 +312,7 @@ type CGRConfig struct { eesCfg *EEsCfg // EventExporter config rateSCfg *RateSCfg // RateS config sipAgentCfg *SIPAgentCfg // SIPAgent config + configSCfg *ConfigSCfg //ConfigS config } var posibleLoaderTypes = utils.NewStringSet([]string{utils.MetaAttributes, @@ -376,7 +378,7 @@ func (cfg *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { cfg.loadMailerCfg, cfg.loadSureTaxCfg, cfg.loadDispatcherSCfg, cfg.loadLoaderCgrCfg, cfg.loadMigratorCgrCfg, cfg.loadTlsCgrCfg, cfg.loadAnalyzerCgrCfg, cfg.loadApierCfg, cfg.loadErsCfg, cfg.loadEesCfg, - cfg.loadRateSCfg, cfg.loadSIPAgentCfg, cfg.loadDispatcherHCfg} { + cfg.loadRateSCfg, cfg.loadSIPAgentCfg, cfg.loadDispatcherHCfg, cfg.loadConfigSCfg} { if err = loadFunc(jsnCfg); err != nil { return } @@ -777,6 +779,14 @@ func (cfg *CGRConfig) loadTemplateSCfg(jsnCfg *CgrJsonCfg) (err error) { return } +func (cfg *CGRConfig) loadConfigSCfg(jsnCfg *CgrJsonCfg) (err error) { + var jsnConfigSCfg *ConfigSCfgJson + if jsnConfigSCfg, err = jsnCfg.ConfigSJsonCfg(); err != nil { + return + } + return cfg.configSCfg.loadFromJsonCfg(jsnConfigSCfg) +} + // SureTaxCfg use locking to retrieve the configuration, possibility later for runtime reload func (cfg *CGRConfig) SureTaxCfg() *SureTaxCfg { cfg.lks[SURETAX_JSON].Lock() @@ -1282,6 +1292,7 @@ func (cfg *CGRConfig) getLoadFunctions() map[string]func(*CgrJsonCfg) error { RateSJson: cfg.loadRateSCfg, SIPAgentJson: cfg.loadSIPAgentCfg, TemplatesJson: cfg.loadTemplateSCfg, + ConfigSJson: cfg.loadConfigSCfg, } } diff --git a/config/config_defaults.go b/config/config_defaults.go index 5028dff66..06fceada7 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -990,4 +990,11 @@ const CGRATES_CFG_JSON = ` ], }, + +"configs": { + "enabled": false, // enables the ConfigS: + "listen": "", // address where to listen for config +}, + + }` diff --git a/config/config_json.go b/config/config_json.go index 59e9bac68..6e7264748 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -63,6 +63,7 @@ const ( RPCConnsJsonName = "rpc_conns" SIPAgentJson = "sip_agent" TemplatesJson = "templates" + ConfigSJson = "configs" ) var ( @@ -70,7 +71,7 @@ var ( CACHE_JSN, FilterSjsn, RALS_JSN, CDRS_JSN, ERsJson, SessionSJson, AsteriskAgentJSN, FreeSWITCHAgentJSN, KamailioAgentJSN, DA_JSN, RA_JSN, HttpAgentJson, DNSAgentJson, ATTRIBUTE_JSN, ChargerSCfgJson, RESOURCES_JSON, STATS_JSON, THRESHOLDS_JSON, RouteSJson, LoaderJson, MAILER_JSN, SURETAX_JSON, CgrLoaderCfgJson, CgrMigratorCfgJson, DispatcherSJson, - AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson} + AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson, ConfigSJson} ) // Loads the json config out of io.Reader, eg other sources than file, maybe over http @@ -544,3 +545,15 @@ func (self CgrJsonCfg) TemplateSJsonCfg() (map[string][]*FcTemplateJsonCfg, erro } return cfg, nil } + +func (self CgrJsonCfg) ConfigSJsonCfg() (*ConfigSCfgJson, error) { + rawCfg, hasKey := self[ConfigSJson] + if !hasKey { + return nil, nil + } + cfg := new(ConfigSCfgJson) + if err := json.Unmarshal(*rawCfg, cfg); err != nil { + return nil, err + } + return cfg, nil +} diff --git a/config/config_test.go b/config/config_test.go index 22a656f86..28da009b0 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -2492,6 +2492,13 @@ func TestRpcConnsDefaults(t *testing.T) { } } -func TestTemplateConnsDefaults(t *testing.T) { +func TestCgrCfgJSONDefaultsConfigS(t *testing.T) { + eCfg := &ConfigSCfg{ + Enabled: false, + Listen: utils.EmptyString, + } + if !reflect.DeepEqual(cgrCfg.configSCfg, eCfg) { + t.Errorf("received: %+v, expecting: %+v", utils.ToJSON(cgrCfg.configSCfg), utils.ToJSON(eCfg)) + } } diff --git a/config/configs.go b/config/configs.go new file mode 100644 index 000000000..6e7697d59 --- /dev/null +++ b/config/configs.go @@ -0,0 +1,39 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package config + +// ConfigSCfg config for listening over http +type ConfigSCfg struct { + Enabled bool + Listen string +} + +//loadFromJsonCfg loads Database config from JsonCfg +func (cScfg *ConfigSCfg) loadFromJsonCfg(jsnCfg *ConfigSCfgJson) (err error) { + if jsnCfg == nil { + return nil + } + if jsnCfg.Enabled != nil { + cScfg.Enabled = *jsnCfg.Enabled + } + if jsnCfg.Listen != nil { + cScfg.Listen = *jsnCfg.Listen + } + return +} diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 0c9060e2c..3ce5ea78b 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -642,3 +642,8 @@ type SIPAgentJsonCfg struct { Templates map[string][]*FcTemplateJsonCfg Request_processors *[]*ReqProcessorJsnCfg } + +type ConfigSCfgJson struct { + Enabled *bool + Listen *string +}