Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dotenv config support #1185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ set(DROGON_SOURCES
lib/src/CacheFile.cc
lib/src/ConfigLoader.cc
lib/src/Cookie.cc
lib/src/DotenvParser.cc
lib/src/DrClassMap.cc
lib/src/DrTemplateBase.cc
lib/src/FiltersFunction.cc
Expand Down
10 changes: 10 additions & 0 deletions lib/inc/drogon/HttpAppFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
*/
virtual HttpAppFramework &loadConfigJson(Json::Value &&data) = 0;

/// Load the configuration file with dotenv format
/// defaults to filename ".env" in the current working directory.
virtual HttpAppFramework &loadConfigDotenv() = 0;

/// Load the configuration file with dotenv format.
/**
* @param filename the configuration file
*/
virtual HttpAppFramework &loadConfigDotenv(const std::string &fileName) = 0;

/// Register a HttpSimpleController object into the framework.
/**
* @param pathName When the path of a http request is equal to the
Expand Down
74 changes: 48 additions & 26 deletions lib/src/ConfigLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

#include "ConfigLoader.h"
#include "DotenvParser.h"
#include "HttpAppFrameworkImpl.h"
#include <drogon/config.h>
#include <fstream>
Expand Down Expand Up @@ -101,14 +102,11 @@ ConfigLoader::ConfigLoader(const std::string &configFile)
{
if (os_access(drogon::utils::toNativePath(configFile).c_str(), 0) != 0)
{
std::cerr << "Config file " << configFile << " not found!" << std::endl;
exit(1);
LOG_FATAL << "Config file " << configFile << " not found!";
}
if (os_access(drogon::utils::toNativePath(configFile).c_str(), R_OK) != 0)
{
std::cerr << "No permission to read config file " << configFile
<< std::endl;
exit(1);
LOG_FATAL << "No permission to read config file " << configFile;
}
configFile_ = configFile;
std::ifstream infile(drogon::utils::toNativePath(configFile).c_str(),
Expand All @@ -121,10 +119,43 @@ ConfigLoader::ConfigLoader(const std::string &configFile)
}
catch (const std::exception &exception)
{
std::cerr << "Configuration file format error! in " << configFile
<< ":" << std::endl;
std::cerr << exception.what() << std::endl;
exit(1);
LOG_FATAL << "Configuration file format error! in " << configFile
<< ":" << exception.what();
}
}
}
ConfigLoader::ConfigLoader(const std::string &configFile, ConfigSyntax syntax)
{
if (os_access(drogon::utils::toNativePath(configFile).c_str(), 0) != 0)
{
LOG_FATAL << "Config file " << configFile << " not found!";
}
if (os_access(drogon::utils::toNativePath(configFile).c_str(), R_OK) != 0)
{
LOG_FATAL << "No permission to read config file " << configFile;
}
configFile_ = configFile;
std::ifstream infile(drogon::utils::toNativePath(configFile).c_str(),
std::ifstream::in);
if (infile)
{
try
{
if (syntax == kDotENV)
{
DotenvParser parser;
parser.parse(infile);
configJsonRoot_ = parser.jsonValue();
}
else
{
infile >> configJsonRoot_;
}
}
catch (const std::exception &exception)
{
LOG_FATAL << "Configuration file format error! in " << configFile
<< ":" << exception.what();
}
}
}
Expand Down Expand Up @@ -269,8 +300,7 @@ static void loadApp(const Json::Value &app)
}
else
{
std::cerr << "The static_file_headers option must be an array\n";
exit(1);
LOG_FATAL << "The static_file_headers option must be an array";
}
}
// upload path
Expand All @@ -294,8 +324,7 @@ static void loadApp(const Json::Value &app)
auto &locations = app["locations"];
if (!locations.isArray())
{
std::cerr << "The locations option must be an array\n";
exit(1);
LOG_FATAL << "The locations option must be an array";
}
for (auto &location : locations)
{
Expand Down Expand Up @@ -328,9 +357,8 @@ static void loadApp(const Json::Value &app)
}
else
{
std::cerr << "the filters of location '" << uri
<< "' should be an array" << std::endl;
exit(1);
LOG_FATAL << "the filters of location '" << uri
<< "' should be an array";
}
}
else
Expand Down Expand Up @@ -442,8 +470,7 @@ static void loadApp(const Json::Value &app)
}
else
{
std::cerr << "Error format of client_max_body_size" << std::endl;
exit(1);
LOG_FATAL << "Error format of client_max_body_size";
}
auto maxMemoryBodySize =
app.get("client_max_memory_body_size", "64K").asString();
Expand All @@ -453,8 +480,7 @@ static void loadApp(const Json::Value &app)
}
else
{
std::cerr << "Error format of client_max_memory_body_size" << std::endl;
exit(1);
LOG_FATAL << "Error format of client_max_memory_body_size";
}
auto maxWsMsgSize =
app.get("client_max_websocket_message_size", "128K").asString();
Expand All @@ -464,9 +490,7 @@ static void loadApp(const Json::Value &app)
}
else
{
std::cerr << "Error format of client_max_websocket_message_size"
<< std::endl;
exit(1);
LOG_FATAL << "Error format of client_max_websocket_message_size";
}
drogon::app().enableReusePort(app.get("reuse_port", false).asBool());
drogon::app().setHomePage(app.get("home_page", "index.html").asString());
Expand Down Expand Up @@ -508,9 +532,7 @@ static void loadDbClients(const Json::Value &dbClients)
auto dbname = client.get("dbname", "").asString();
if (dbname == "" && type != "sqlite3")
{
std::cerr << "Please configure dbname in the configuration file"
<< std::endl;
exit(1);
LOG_FATAL << "Please configure dbname in the configuration file";
}
auto user = client.get("user", "postgres").asString();
auto password = client.get("passwd", "").asString();
Expand Down
7 changes: 7 additions & 0 deletions lib/src/ConfigLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@

namespace drogon
{
enum ConfigSyntax
{
kJSON,
kDotENV
};

class ConfigLoader : public trantor::NonCopyable
{
public:
explicit ConfigLoader(const std::string &configFile);
explicit ConfigLoader(const std::string &configFile, ConfigSyntax syntax);
explicit ConfigLoader(const Json::Value &data);
explicit ConfigLoader(Json::Value &&data);
~ConfigLoader();
Expand Down
Loading