Add ConfigLoader::ConfigLoader(const Json::Value &data) (#579)

This commit is contained in:
L0ric0 2020-09-18 15:12:46 +02:00 committed by GitHub
parent c320527f9d
commit 6fca7067da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 1 deletions

View File

@ -340,6 +340,23 @@ class HttpAppFramework : public trantor::NonCopyable
*/
virtual HttpAppFramework &loadConfigFile(const std::string &fileName) = 0;
/// Load the configuration from a Json::Value Object.
/**
* @param Json::Value Object containing the configuration.
* @note Please refer to the configuration file for the content of the json
* object.
*/
virtual HttpAppFramework &loadConfigJson(const Json::Value &data) = 0;
/// Load the configuration from a Json::Value Object.
/**
* @param rvalue reference to a Json::Value object containing the
* configuration.
* @note Please refer to the configuration file for the content of the json
* object.
*/
virtual HttpAppFramework &loadConfigJson(Json::Value &&data) = 0;
/// Register a HttpSimpleController object into the framework.
/**
* @param pathName When the path of a http request is equal to the

View File

@ -130,6 +130,13 @@ ConfigLoader::ConfigLoader(const std::string &configFile)
}
}
}
ConfigLoader::ConfigLoader(const Json::Value &data) : configJsonRoot_(data)
{
}
ConfigLoader::ConfigLoader(Json::Value &&data)
: configJsonRoot_(std::move(data))
{
}
ConfigLoader::~ConfigLoader()
{
}

View File

@ -24,6 +24,8 @@ class ConfigLoader : public trantor::NonCopyable
{
public:
explicit ConfigLoader(const std::string &configFile);
explicit ConfigLoader(const Json::Value &data);
explicit ConfigLoader(Json::Value &&data);
~ConfigLoader();
const Json::Value &jsonValue() const
{

View File

@ -333,6 +333,20 @@ HttpAppFramework &HttpAppFrameworkImpl::loadConfigFile(
jsonConfig_ = loader.jsonValue();
return *this;
}
HttpAppFramework &HttpAppFrameworkImpl::loadConfigJson(const Json::Value &data)
{
ConfigLoader loader(data);
loader.load();
jsonConfig_ = loader.jsonValue();
return *this;
}
HttpAppFramework &HttpAppFrameworkImpl::loadConfigJson(Json::Value &&data)
{
ConfigLoader loader(std::move(data));
loader.load();
jsonConfig_ = loader.jsonValue();
return *this;
}
HttpAppFramework &HttpAppFrameworkImpl::setLogPath(
const std::string &logPath,
const std::string &logfileBaseName,
@ -999,4 +1013,4 @@ const std::function<HttpResponsePtr(HttpStatusCode)>
std::vector<trantor::InetAddress> HttpAppFrameworkImpl::getListeners() const
{
return listenerManagerPtr_->getListeners();
}
}

View File

@ -17,6 +17,7 @@
#include "impl_forwards.h"
#include <drogon/HttpAppFramework.h>
#include <drogon/config.h>
#include <json/json.h>
#include <memory>
#include <mutex>
#include <regex>
@ -248,6 +249,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework
size_t maxConnectionsPerIP) override;
virtual HttpAppFramework &loadConfigFile(
const std::string &fileName) override;
virtual HttpAppFramework &loadConfigJson(const Json::Value &data) override;
virtual HttpAppFramework &loadConfigJson(Json::Value &&data) override;
virtual HttpAppFramework &enableRunAsDaemon() override
{
runAsDaemon_ = true;