From ac6a1c0597f298e73a974b93bca853490e6d1175 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 14 Mar 2018 10:32:05 -0400 Subject: [PATCH] Add HTTP config option freeswiwtch_cdrs_url --- cmd/cgr-engine/cgr-engine.go | 1 + config/config.go | 4 ++++ config/config_defaults.go | 11 ++++++----- config/config_json_test.go | 9 +++++---- config/config_test.go | 3 +++ config/libconfig_json.go | 9 +++++---- utils/server.go | 15 ++++++++++++++- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 54bc0b398..86836127c 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -728,6 +728,7 @@ func startRpc(server *utils.Server, internalRaterChan, cfg.HTTPWSURL, cfg.HTTPUseBasicAuth, cfg.HTTPAuthUsers, + cfg.HTTPFreeswitchCDRSURL, ) } diff --git a/config/config.go b/config/config.go index 1e2c8af8d..6e74d30ba 100755 --- a/config/config.go +++ b/config/config.go @@ -275,6 +275,7 @@ type CGRConfig struct { HTTPWSURL string // WebSocket relative URL ("" to disable) HTTPUseBasicAuth bool // Use basic auth for HTTP API HTTPAuthUsers map[string]string // Basic auth user:password map (base64 passwords) + HTTPFreeswitchCDRSURL string // Freeswitch CDRS relative URL ("" to disable) DefaultReqType string // Use this request type if not defined on top DefaultCategory string // set default type of record DefaultTenant string // set default tenant @@ -945,6 +946,9 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { if jsnHttpCfg.Auth_users != nil { self.HTTPAuthUsers = *jsnHttpCfg.Auth_users } + if jsnHttpCfg.Freeswitch_cdrs_url != nil { + self.HTTPFreeswitchCDRSURL = *jsnHttpCfg.Freeswitch_cdrs_url + } } if jsnFilterSCfg != nil { diff --git a/config/config_defaults.go b/config/config_defaults.go index fbb79a980..1e8bbd431 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -83,11 +83,12 @@ const CGRATES_CFG_JSON = ` }, -"http": { // HTTP server configuration - "json_rpc_url": "/jsonrpc", // JSON RPC relative URL ("" to disable) - "ws_url": "/ws", // WebSockets relative URL ("" to disable) - "use_basic_auth": false, // use basic authentication - "auth_users": {} // basic authentication usernames and base64-encoded passwords (eg: { "username1": "cGFzc3dvcmQ=", "username2": "cGFzc3dvcmQy "}) +"http": { // HTTP server configuration + "json_rpc_url": "/jsonrpc", // JSON RPC relative URL ("" to disable) + "ws_url": "/ws", // WebSockets relative URL ("" to disable) + "use_basic_auth": false, // use basic authentication + "auth_users": {}, // basic authentication usernames and base64-encoded passwords (eg: { "username1": "cGFzc3dvcmQ=", "username2": "cGFzc3dvcmQy "}) + "freeswitch_cdrs_url": "/freeswitchcdrs", // Freeswitch CDRS relative URL ("" to disable) }, diff --git a/config/config_json_test.go b/config/config_json_test.go index e1131cafc..7390de63f 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -946,10 +946,11 @@ func TestNewCgrJsonCfgFromFile(t *testing.T) { func TestDfHttpJsonCfg(t *testing.T) { eCfg := &HTTPJsonCfg{ - Json_rpc_url: utils.StringPointer("/jsonrpc"), - Ws_url: utils.StringPointer("/ws"), - Use_basic_auth: utils.BoolPointer(false), - Auth_users: utils.MapStringStringPointer(map[string]string{})} + Json_rpc_url: utils.StringPointer("/jsonrpc"), + Ws_url: utils.StringPointer("/ws"), + Use_basic_auth: utils.BoolPointer(false), + Auth_users: utils.MapStringStringPointer(map[string]string{}), + Freeswitch_cdrs_url: utils.StringPointer("/freeswitchcdrs")} if cfg, err := dfCgrJsonCfg.HttpJsonCfg(); err != nil { t.Error(err) } else if !reflect.DeepEqual(eCfg, cfg) { diff --git a/config/config_test.go b/config/config_test.go index 315d953b6..c37c44793 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -831,6 +831,9 @@ func TestCgrCfgJSONDefaultsHTTP(t *testing.T) { if !reflect.DeepEqual(cgrCfg.HTTPAuthUsers, map[string]string{}) { t.Error(cgrCfg.HTTPAuthUsers) } + if cgrCfg.HTTPFreeswitchCDRSURL != "/freeswitchcdrs" { + t.Error(cgrCfg.HTTPFreeswitchCDRSURL) + } } func TestRadiusAgentCfg(t *testing.T) { diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 3e7fcb4cb..0864b5aea 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -51,10 +51,11 @@ type ListenJsonCfg struct { // HTTP config section type HTTPJsonCfg struct { - Json_rpc_url *string - Ws_url *string - Use_basic_auth *bool - Auth_users *map[string]string + Json_rpc_url *string + Ws_url *string + Use_basic_auth *bool + Auth_users *map[string]string + Freeswitch_cdrs_url *string } // Database config diff --git a/utils/server.go b/utils/server.go index 9f3d5b9fd..df341981a 100644 --- a/utils/server.go +++ b/utils/server.go @@ -173,7 +173,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { io.Copy(w, res) } -func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string, useBasicAuth bool, userList map[string]string) { +func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string, + useBasicAuth bool, userList map[string]string, freeswitchCDRSURL string) { s.RLock() enabled := s.rpcEnabled s.RUnlock() @@ -215,6 +216,18 @@ func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string, useB if useBasicAuth { Logger.Info(" enabling basic auth") } + if enabled && freeswitchCDRSURL != "" { + s.Lock() + s.httpEnabled = true + s.Unlock() + Logger.Info(" enabling handler for FreeswitchCDRS-URL") + if useBasicAuth { + http.HandleFunc(freeswitchCDRSURL, use(handleRequest, basicAuth(userList))) + } else { + http.HandleFunc(freeswitchCDRSURL, handleRequest) + } + } + Logger.Info(fmt.Sprintf(" start listening at <%s>", addr)) http.ListenAndServe(addr, nil) }