124 lines
5.2 KiB
C++
124 lines
5.2 KiB
C++
#include <drogon/drogon.h>
|
|
|
|
#include "common/Cors.h"
|
|
#include "plugins/RequestLogger.h"
|
|
#include "repositories/MigrationRunner.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
#include <string_view>
|
|
|
|
namespace
|
|
{
|
|
bool parseEnvBool(const char *value)
|
|
{
|
|
if (value == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const std::string_view normalized(value);
|
|
return normalized == "1" || normalized == "true" || normalized == "TRUE" ||
|
|
normalized == "yes" || normalized == "on";
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
(void)RequestLogger::classTypeName();
|
|
|
|
Json::Value config;
|
|
std::ifstream configFile("config.json");
|
|
if (configFile.is_open())
|
|
{
|
|
configFile >> config;
|
|
|
|
// DB config from env
|
|
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* envTimeout = std::getenv("DB_TIMEOUT"))
|
|
config["db_clients"][0]["timeout"] = std::stod(envTimeout);
|
|
|
|
// Mail config from env
|
|
if (const char* envMailProvider = std::getenv("MAIL_PROVIDER"))
|
|
config["custom_config"]["mail"]["provider"] = envMailProvider;
|
|
if (const char* envSmtpHost = std::getenv("SMTP_HOST"))
|
|
config["custom_config"]["mail"]["host"] = envSmtpHost;
|
|
if (const char* envSmtpPort = std::getenv("SMTP_PORT"))
|
|
config["custom_config"]["mail"]["port"] = std::stoi(envSmtpPort);
|
|
if (const char* envSmtpUsername = std::getenv("SMTP_USERNAME"))
|
|
config["custom_config"]["mail"]["username"] = envSmtpUsername;
|
|
if (const char* envSmtpPassword = std::getenv("SMTP_PASSWORD"))
|
|
config["custom_config"]["mail"]["password"] = envSmtpPassword;
|
|
else if (const char* envResendApiKey = std::getenv("RESEND_API_KEY"))
|
|
config["custom_config"]["mail"]["password"] = envResendApiKey;
|
|
if (const char* envSmtpUseTls = std::getenv("SMTP_USE_TLS"))
|
|
config["custom_config"]["mail"]["use_tls"] = parseEnvBool(envSmtpUseTls);
|
|
if (const char* envSmtpUseStartTls = std::getenv("SMTP_USE_STARTTLS"))
|
|
config["custom_config"]["mail"]["use_starttls"] = parseEnvBool(envSmtpUseStartTls);
|
|
if (const char* envSmtpVerifyPeer = std::getenv("SMTP_VERIFY_PEER"))
|
|
config["custom_config"]["mail"]["verify_peer"] = parseEnvBool(envSmtpVerifyPeer);
|
|
if (const char* envSmtpVerifyHost = std::getenv("SMTP_VERIFY_HOST"))
|
|
config["custom_config"]["mail"]["verify_host"] = parseEnvBool(envSmtpVerifyHost);
|
|
if (const char* envMailFrom = std::getenv("MAIL_FROM"))
|
|
config["custom_config"]["mail"]["from"] = envMailFrom;
|
|
|
|
// CORS config from env
|
|
if (const char* envCorsAllowedOrigin = std::getenv("CORS_ALLOWED_ORIGIN"))
|
|
config["custom_config"]["cors"]["allowed_origin"] = envCorsAllowedOrigin;
|
|
|
|
// CIE auth config from env
|
|
if (const char* envCieServiceBaseUrl = std::getenv("CIE_AUTH_SERVICE_BASE_URL"))
|
|
config["custom_config"]["cie_auth"]["service_base_url"] = envCieServiceBaseUrl;
|
|
if (const char* envCieStartPath = std::getenv("CIE_AUTH_START_PATH"))
|
|
config["custom_config"]["cie_auth"]["start_path"] = envCieStartPath;
|
|
if (const char* envCieSharedSecret = std::getenv("CIE_AUTH_SHARED_SECRET"))
|
|
config["custom_config"]["cie_auth"]["shared_secret"] = envCieSharedSecret;
|
|
if (const char* envCieStateTtl = std::getenv("CIE_AUTH_STATE_TTL_MINUTES"))
|
|
config["custom_config"]["cie_auth"]["state_ttl_minutes"] = std::stoi(envCieStateTtl);
|
|
if (const char* envCieFrontendRedirect = std::getenv("CIE_AUTH_DEFAULT_FRONTEND_REDIRECT_URI"))
|
|
config["custom_config"]["cie_auth"]["default_frontend_redirect_uri"] = envCieFrontendRedirect;
|
|
|
|
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;
|
|
}
|