mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
/*
|
|
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 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// ConfigSCfg config for listening over http
|
|
type ConfigSCfg struct {
|
|
Enabled bool
|
|
Url string
|
|
RootDir 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.Url != nil {
|
|
cScfg.Url = *jsnCfg.Url
|
|
}
|
|
if jsnCfg.Root_dir != nil {
|
|
cScfg.RootDir = *jsnCfg.Root_dir
|
|
}
|
|
return
|
|
}
|
|
|
|
// RegisterConfigs handler for httpServer to register the configs
|
|
func HandlerConfigS(w http.ResponseWriter, r *http.Request) {
|
|
defer r.Body.Close()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// take out the /configs prefix and use the rest of url as path
|
|
pth := strings.TrimPrefix(r.URL.Path, "/configs")
|
|
pth = path.Join(CgrConfig().ConfigSCfg().RootDir, pth)
|
|
fi, err := os.Stat(pth)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
w.WriteHeader(404)
|
|
} else {
|
|
w.WriteHeader(500)
|
|
}
|
|
fmt.Fprintf(w, err.Error())
|
|
return
|
|
}
|
|
switch mode := fi.Mode(); {
|
|
case mode.IsDir():
|
|
handleConfigSFolder(pth, w)
|
|
case mode.IsRegular():
|
|
handleConfigSFile(pth, w)
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleConfigSFolder(path string, w http.ResponseWriter) {
|
|
// if the path is a directory, read the directory, construct the config and load it in memory
|
|
cfg, err := NewCGRConfigFromPath(path)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
fmt.Fprintf(w, err.Error())
|
|
return
|
|
}
|
|
// convert the config into a json and send it
|
|
if _, err := w.Write([]byte(utils.ToJSON(cfg.AsMapInterface(cfg.generalCfg.RSRSep)))); err != nil {
|
|
utils.Logger.Warning(fmt.Sprintf("<%s> Failed to write resonse because: %s",
|
|
utils.ConfigSv1, err))
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleConfigSFile(path string, w http.ResponseWriter) {
|
|
// if the config is a file read the file and send it directly
|
|
dat, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
fmt.Fprintf(w, err.Error())
|
|
return
|
|
}
|
|
if _, err := w.Write(dat); err != nil {
|
|
utils.Logger.Warning(fmt.Sprintf("<%s> Failed to write resonse because: %s",
|
|
utils.ConfigSv1, err))
|
|
}
|
|
return
|
|
}
|