71 lines
2.3 KiB
C++
71 lines
2.3 KiB
C++
#include <drogon/drogon.h>
|
|
|
|
#include "common/Cors.h"
|
|
#include "plugins/RequestLogger.h"
|
|
#include "repositories/MigrationRunner.h"
|
|
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <cstdlib>
|
|
|
|
int main()
|
|
{
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
(void)RequestLogger::classTypeName();
|
|
|
|
Json::Value config;
|
|
std::ifstream configFile("config.json");
|
|
if (configFile.is_open())
|
|
{
|
|
configFile >> config;
|
|
|
|
if (const char* envHost = std::getenv("DB_HOST"))
|
|
config["db_clients"][0]["host"] = envHost;
|
|
if (const char* envPort = std::getenv("DB_PORT"))
|
|
config["db_clients"][0]["port"] = std::stoi(envPort);
|
|
if (const char* envUser = std::getenv("DB_USER"))
|
|
config["db_clients"][0]["user"] = envUser;
|
|
if (const char* envPass = std::getenv("DB_PASSWORD"))
|
|
config["db_clients"][0]["passwd"] = envPass;
|
|
if (const char* envDb = std::getenv("DB_NAME"))
|
|
config["db_clients"][0]["dbname"] = envDb;
|
|
|
|
if (const char* envSmtpHost = std::getenv("SMTP_HOST"))
|
|
config["custom_config"]["mail"]["smtp_host"] = envSmtpHost;
|
|
if (const char* envSmtpPort = std::getenv("SMTP_PORT"))
|
|
config["custom_config"]["mail"]["smtp_port"] = std::stoi(envSmtpPort);
|
|
if (const char* envCorsAllowedOrigin = std::getenv("CORS_ALLOWED_ORIGIN"))
|
|
config["custom_config"]["cors"]["allowed_origin"] = envCorsAllowedOrigin;
|
|
drogon::app().loadConfigJson(config);
|
|
}
|
|
else
|
|
{
|
|
drogon::app().loadConfigFile("config.json");
|
|
}
|
|
|
|
Json::Value requestLoggerConfig;
|
|
requestLoggerConfig["include_query"] = true;
|
|
|
|
drogon::app().addPlugin("RequestLogger", {}, requestLoggerConfig);
|
|
visione::cors::configure();
|
|
|
|
const auto migrationsPath = drogon::app()
|
|
.getCustomConfig()
|
|
.get("migrations_path", "migrations")
|
|
.asString();
|
|
drogon::app().getLoop()->runAfter(0.0, [migrationsPath]() {
|
|
try
|
|
{
|
|
visione::repositories::MigrationRunner::run(migrationsPath);
|
|
}
|
|
catch (const std::exception &error)
|
|
{
|
|
LOG_FATAL << "Migration failed: " << error.what();
|
|
drogon::app().quit();
|
|
}
|
|
});
|
|
|
|
drogon::app().run();
|
|
return 0;
|
|
}
|