Fix loading configuration on windows (#1530)
This commit is contained in:
parent
97a5496fa4
commit
a039157587
|
@ -65,4 +65,9 @@ void version::handleCommand(std::vector<std::string> ¶meters)
|
|||
#endif
|
||||
std::cout << " c-ares: "
|
||||
<< (trantor::Resolver::isCAresUsed() ? "yes\n" : "no\n");
|
||||
#ifdef HAS_YAML_CPP
|
||||
std::cout << " yaml-cpp: yes\n";
|
||||
#else
|
||||
std::cout << " yaml-cpp: no\n";
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
|
||||
namespace drogon
|
||||
{
|
||||
|
@ -10,7 +11,7 @@ class ConfigAdapter
|
|||
{
|
||||
public:
|
||||
virtual ~ConfigAdapter() = default;
|
||||
virtual Json::Value getJson(const std::string &configFile) const
|
||||
virtual Json::Value getJson(const std::string &content) const
|
||||
noexcept(false) = 0;
|
||||
virtual std::vector<std::string> getExtensions() const = 0;
|
||||
};
|
||||
|
|
|
@ -20,23 +20,16 @@ ConfigAdapterManager &ConfigAdapterManager::instance()
|
|||
return instance;
|
||||
}
|
||||
|
||||
Json::Value ConfigAdapterManager::getJson(const std::string &configFile) const
|
||||
noexcept(false)
|
||||
Json::Value ConfigAdapterManager::getJson(const std::string &content,
|
||||
std::string ext) const noexcept(false)
|
||||
{
|
||||
auto pos = configFile.find_last_of('.');
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
throw std::runtime_error("Invalid config file name!");
|
||||
}
|
||||
auto ext = configFile.substr(pos + 1);
|
||||
// convert ext to lower case
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
||||
auto it = adapters_.find(ext);
|
||||
if (it == adapters_.end())
|
||||
{
|
||||
throw std::runtime_error("No valid parser for this config file!");
|
||||
}
|
||||
return it->second->getJson(configFile);
|
||||
return it->second->getJson(content);
|
||||
}
|
||||
|
||||
ConfigAdapterManager::ConfigAdapterManager()
|
||||
|
|
|
@ -8,7 +8,8 @@ class ConfigAdapterManager
|
|||
{
|
||||
public:
|
||||
static ConfigAdapterManager &instance();
|
||||
Json::Value getJson(const std::string &configFile) const noexcept(false);
|
||||
Json::Value getJson(const std::string &content, std::string ext) const
|
||||
noexcept(false);
|
||||
|
||||
private:
|
||||
ConfigAdapterManager();
|
||||
|
|
|
@ -117,10 +117,21 @@ ConfigLoader::ConfigLoader(const std::string &configFile)
|
|||
configFile);
|
||||
}
|
||||
configFile_ = configFile;
|
||||
auto pos = configFile.find_last_of('.');
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
throw std::runtime_error("Invalid config file name!");
|
||||
}
|
||||
auto ext = configFile.substr(pos + 1);
|
||||
std::ifstream infile(drogon::utils::toNativePath(configFile).c_str(),
|
||||
std::ifstream::in);
|
||||
// get the content of the infile
|
||||
std::string content((std::istreambuf_iterator<char>(infile)),
|
||||
std::istreambuf_iterator<char>());
|
||||
try
|
||||
{
|
||||
auto filename = drogon::utils::toNativePath(configFile);
|
||||
configJsonRoot_ = ConfigAdapterManager::instance().getJson(configFile);
|
||||
configJsonRoot_ =
|
||||
ConfigAdapterManager::instance().getJson(content, std::move(ext));
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
|
|
|
@ -2,17 +2,15 @@
|
|||
#include <fstream>
|
||||
|
||||
using namespace drogon;
|
||||
Json::Value JsonConfigAdapter::getJson(const std::string &configFile) const
|
||||
Json::Value JsonConfigAdapter::getJson(const std::string &content) const
|
||||
noexcept(false)
|
||||
{
|
||||
Json::Value root;
|
||||
Json::Reader reader;
|
||||
std::ifstream in(configFile, std::ios::binary);
|
||||
if (!in.is_open())
|
||||
if (!reader.parse(content, root))
|
||||
{
|
||||
throw std::runtime_error("Cannot open config file!");
|
||||
throw std::runtime_error("Failed to parse JSON");
|
||||
}
|
||||
in >> root;
|
||||
return root;
|
||||
}
|
||||
std::vector<std::string> JsonConfigAdapter::getExtensions() const
|
||||
|
|
|
@ -7,7 +7,7 @@ class JsonConfigAdapter : public ConfigAdapter
|
|||
public:
|
||||
JsonConfigAdapter() = default;
|
||||
~JsonConfigAdapter() override = default;
|
||||
Json::Value getJson(const std::string &configFile) const
|
||||
Json::Value getJson(const std::string &content) const
|
||||
noexcept(false) override;
|
||||
std::vector<std::string> getExtensions() const override;
|
||||
};
|
||||
|
|
|
@ -95,12 +95,12 @@ struct convert<Json::Value>
|
|||
} // namespace YAML
|
||||
|
||||
#endif
|
||||
Json::Value YamlConfigAdapter::getJson(const std::string &configFile) const
|
||||
Json::Value YamlConfigAdapter::getJson(const std::string &content) const
|
||||
noexcept(false)
|
||||
{
|
||||
#if HAS_YAML_CPP
|
||||
// parse yaml file
|
||||
YAML::Node config = YAML::LoadFile(configFile);
|
||||
YAML::Node config = YAML::Load(content);
|
||||
if (!config.IsNull())
|
||||
{
|
||||
return config.as<Json::Value>();
|
||||
|
|
|
@ -7,7 +7,7 @@ class YamlConfigAdapter : public ConfigAdapter
|
|||
public:
|
||||
YamlConfigAdapter() = default;
|
||||
~YamlConfigAdapter() override = default;
|
||||
Json::Value getJson(const std::string &configFile) const
|
||||
Json::Value getJson(const std::string &content) const
|
||||
noexcept(false) override;
|
||||
std::vector<std::string> getExtensions() const override;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue