105 lines
3.9 KiB
C++
105 lines
3.9 KiB
C++
|
|
#include <drogon/drogon.h>
|
||
|
|
#include <drogon/HttpAppFramework.h>
|
||
|
|
#include "controllers/StreamController.h"
|
||
|
|
#include "controllers/UserController.h"
|
||
|
|
#include "controllers/AdminController.h"
|
||
|
|
#include "controllers/RealmController.h"
|
||
|
|
#include "services/DatabaseService.h"
|
||
|
|
#include "services/StatsService.h"
|
||
|
|
#include "services/AuthService.h"
|
||
|
|
#include <exception>
|
||
|
|
#include <csignal>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
|
||
|
|
using namespace drogon;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
// Simplified signal handlers
|
||
|
|
signal(SIGSEGV, [](int s){ LOG_ERROR << "Signal " << s; exit(s); });
|
||
|
|
signal(SIGABRT, [](int s){ LOG_ERROR << "Signal " << s; exit(s); });
|
||
|
|
|
||
|
|
try {
|
||
|
|
LOG_INFO << "Starting streaming backend server...";
|
||
|
|
|
||
|
|
// Create upload directories
|
||
|
|
mkdir("./uploads", 0755);
|
||
|
|
mkdir("./uploads/avatars", 0755);
|
||
|
|
|
||
|
|
// Initialize DatabaseService
|
||
|
|
LOG_INFO << "Initializing DatabaseService...";
|
||
|
|
DatabaseService::getInstance().initialize();
|
||
|
|
|
||
|
|
// Load config
|
||
|
|
LOG_INFO << "Loading configuration...";
|
||
|
|
app().loadConfigFile("config.json");
|
||
|
|
|
||
|
|
// Register a pre-routing advice to handle CORS
|
||
|
|
app().registerPreRoutingAdvice([](const HttpRequestPtr &req,
|
||
|
|
AdviceCallback &&acb,
|
||
|
|
AdviceChainCallback &&accb) {
|
||
|
|
// Handle CORS preflight requests
|
||
|
|
if (req->getMethod() == Options) {
|
||
|
|
auto resp = HttpResponse::newHttpResponse();
|
||
|
|
resp->setStatusCode(k204NoContent);
|
||
|
|
|
||
|
|
// Get origin from request
|
||
|
|
std::string origin = req->getHeader("Origin");
|
||
|
|
if (origin.empty()) {
|
||
|
|
origin = "*";
|
||
|
|
}
|
||
|
|
|
||
|
|
resp->addHeader("Access-Control-Allow-Origin", origin);
|
||
|
|
resp->addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||
|
|
resp->addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||
|
|
resp->addHeader("Access-Control-Allow-Credentials", "true");
|
||
|
|
resp->addHeader("Access-Control-Max-Age", "86400");
|
||
|
|
acb(resp);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
accb();
|
||
|
|
});
|
||
|
|
|
||
|
|
// Register post-handling advice to add CORS headers to all responses
|
||
|
|
app().registerPostHandlingAdvice([](const HttpRequestPtr &req,
|
||
|
|
const HttpResponsePtr &resp) {
|
||
|
|
// Get origin from request
|
||
|
|
std::string origin = req->getHeader("Origin");
|
||
|
|
if (origin.empty()) {
|
||
|
|
origin = "*";
|
||
|
|
}
|
||
|
|
|
||
|
|
resp->addHeader("Access-Control-Allow-Origin", origin);
|
||
|
|
resp->addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||
|
|
resp->addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||
|
|
resp->addHeader("Access-Control-Allow-Credentials", "true");
|
||
|
|
});
|
||
|
|
|
||
|
|
// Register beginning advice to initialize StatsService after app starts
|
||
|
|
app().registerBeginningAdvice([]() {
|
||
|
|
LOG_INFO << "Application started successfully";
|
||
|
|
|
||
|
|
// Initialize StatsService after app is running
|
||
|
|
LOG_INFO << "Initializing StatsService...";
|
||
|
|
StatsService::getInstance().initialize();
|
||
|
|
});
|
||
|
|
|
||
|
|
app().setTermSignalHandler([]() {
|
||
|
|
LOG_INFO << "Received termination signal, shutting down...";
|
||
|
|
StatsService::getInstance().shutdown();
|
||
|
|
app().quit();
|
||
|
|
});
|
||
|
|
|
||
|
|
// Start the application
|
||
|
|
LOG_INFO << "Starting Drogon framework...";
|
||
|
|
app().run();
|
||
|
|
|
||
|
|
} catch (const std::exception& e) {
|
||
|
|
LOG_ERROR << "Exception caught in main: " << e.what();
|
||
|
|
return 1;
|
||
|
|
} catch (...) {
|
||
|
|
LOG_ERROR << "Unknown exception caught in main";
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|