added CORS

This commit is contained in:
2026-02-16 23:24:31 +01:00
parent 14b8234e77
commit 58d37b51b7
27 changed files with 80 additions and 32 deletions

67
source/main.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include <crow.h>
#include <string>
#include "json_settings.h"
#include "utils.hpp"
#include "cors.h"
#include "login.hpp"
#include "ShadowrunApi.hpp"
#include "databasepool.h"
using namespace std;
AppSettings::Settings settings = AppSettings::load();
int main() {
crow::App<CORS> app;
// create global database
dbpool = std::make_unique<DatabasePool>(settings.db_path);
if (!dbpool->init(std::thread::hardware_concurrency())){
CROW_LOG_ERROR << "Failed to create database at : " << settings.db_path;
return 1;
}
auto opt_isPortOpen = utils::isLocalPortOpen(settings.http_port);
if (opt_isPortOpen.has_value()){
if (opt_isPortOpen.value()){
CROW_LOG_ERROR << "Local port : " << settings.http_port << " is already open";
return 1;
}
}
else {
CROW_LOG_ERROR << "failed to check if local port is open : " << opt_isPortOpen.error();
return 1;
}
// Root route
CROW_ROUTE(app, "/")([&]() {
auto data = utils::read_file(utils::build_dir / "index.html");
return crow::response(200, "text/html", data);
});
shadowrun::initApi(app);
login::initLogin(app);
// asssets is not svelte generated
CROW_ROUTE(app, "/assets/<path>")
([&](const std::string& p) {
const filesystem::path assets_dir = "assets/"; // <-- set your build folder
filesystem::path file_path = assets_dir / p;
return utils::getFile(file_path);
});
// Catch-all route for static files and SPA fallback
CROW_ROUTE(app, "/<path>")
([&](const std::string& p) {
filesystem::path file_path = utils::build_dir / p;
// If path is a directory, serve index.html inside it
if (filesystem::is_directory(file_path))
file_path /= "index.html";
return utils::getFile(file_path);
});
app.loglevel(crow::LogLevel::INFO);
app.bindaddr("0.0.0.0").port(settings.http_port).multithreaded().run();
}