diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 2c7326f..c1b806a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "build", "type": "shell", - "command": "cmake -S . -B build && cmake --build build", + "command": "cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && cmake --build build && cp build/compile_commands.json compile_commands.json", "group": "build", "problemMatcher": [ "$gcc" @@ -13,7 +13,7 @@ { "label": "build release", "type": "shell", - "command": "cmake -S . -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build", + "command": "cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && cmake --build build && cp build/compile_commands.json compile_commands.json ", "group": "build", "problemMatcher": [ "$gcc" @@ -26,6 +26,12 @@ "options": { "cwd": "frontend" } - } + }, + { + "label": "clean", + "type": "shell", + "command": "rm -rf build/*", + "group": "build", + }, ] } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 49e7a73..4c0032d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ endif() file(GLOB_RECURSE ASSETS_FILES "${CMAKE_SOURCE_DIR}/assets/*") file(GLOB_RECURSE FRONTEND_FILES "${CMAKE_SOURCE_DIR}/frontend/build/*") -foreach(file IN LISTS STATIC_FILES) +foreach(file IN LISTS ASSETS_FILES) file(RELATIVE_PATH rel_path "${CMAKE_SOURCE_DIR}" "${file}") configure_file("${file}" "${CMAKE_BINARY_DIR}/${rel_path}" COPYONLY) endforeach() diff --git a/assets/favicon.ico b/assets/favicon.ico new file mode 100644 index 0000000..ac282d3 Binary files /dev/null and b/assets/favicon.ico differ diff --git a/assets/favicon.png b/assets/favicon.png new file mode 100644 index 0000000..351ea12 Binary files /dev/null and b/assets/favicon.png differ diff --git a/assets/settings.json b/assets/settings.json index 5c01519..4f0edd5 100644 --- a/assets/settings.json +++ b/assets/settings.json @@ -1,5 +1,5 @@ { "http_port" : 3010, - "db_path": "/var/lib/shadowrun-server/shadowrun.db", - "url": "*" + "db_path": "shadowrun.db", + "domain": "*" } \ No newline at end of file diff --git a/frontend/src/app.html b/frontend/src/app.html index f273cc5..b9c45d8 100644 --- a/frontend/src/app.html +++ b/frontend/src/app.html @@ -3,6 +3,7 @@ + %sveltekit.head% diff --git a/frontend/static/favicon.ico b/frontend/static/favicon.ico new file mode 100644 index 0000000..ac282d3 Binary files /dev/null and b/frontend/static/favicon.ico differ diff --git a/source/cors.h b/source/cors.h deleted file mode 100644 index 351111e..0000000 --- a/source/cors.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __CORS_H__ -#define __CORS_H__ - -#include "crow.h" -#include "json_settings.h" - -extern AppSettings::Settings settings; - -struct CORS { - // required inner type for Crow middleware - struct context {}; - - void before_handle(crow::request& req, crow::response& res, context& /*ctx*/) { - // allow all origins (for dev); replace "*" with your frontend URL in production - res.add_header("Access-Control-Allow-Origin", settings.url); - res.add_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); - res.add_header("Access-Control-Allow-Headers", "Content-Type"); - - // automatically handle preflight - if (req.method == crow::HTTPMethod::OPTIONS) { - res.end(); // stop here — no routing needed - } - } - - // run after handler (not needed for CORS, but must be present) - void after_handle(crow::request&, crow::response&, context&) {} -}; - -#endif // __CORS_H__ \ No newline at end of file diff --git a/source/json_settings.cpp b/source/json_settings.cpp index 4104251..313f19a 100644 --- a/source/json_settings.cpp +++ b/source/json_settings.cpp @@ -12,7 +12,7 @@ Settings AppSettings::deafult(){ return Settings { .http_port = 3010, .db_path = "/var/lib/shadowrun-server/shadowrun.db", - .url = "*" + .domain = "*" }; } diff --git a/source/json_settings.h b/source/json_settings.h index 9be49cb..48fd866 100644 --- a/source/json_settings.h +++ b/source/json_settings.h @@ -10,12 +10,12 @@ static constexpr char settingsFile[] = "assets/settings.json"; struct Settings { int http_port; std::string db_path; - std::string url; + std::string domain; }; Settings load(); Settings deafult(); -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Settings, http_port, db_path, url); +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Settings, http_port, db_path, domain); } #endif // JSON_SETTINGS_H \ No newline at end of file diff --git a/source/login/login.cpp b/source/login/login.cpp index 6974fbe..89413d0 100644 --- a/source/login/login.cpp +++ b/source/login/login.cpp @@ -45,8 +45,7 @@ std::optional loginUser(const std::string& username, const std::str return {}; } -void initLogin(crow::App& app) -{ +void initLogin(crow::App& app){ createUser("lukas", "Trollar4928"); diff --git a/source/login/login.hpp b/source/login/login.hpp index 690ffa7..a7ea584 100644 --- a/source/login/login.hpp +++ b/source/login/login.hpp @@ -4,11 +4,11 @@ #pragma once #include -#include "cors.h" +#include "crow/middlewares/cors.h" namespace login { -void initLogin(crow::App& app); +void initLogin(crow::App& app); std::optional isLoggedIn(const crow::request& req); diff --git a/source/main.cpp b/source/main.cpp index 92ec944..a06625a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,18 +1,24 @@ #include +#include "crow/middlewares/cors.h" #include #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 app; + AppSettings::Settings settings = AppSettings::load(); + crow::App app; + + // init cors + auto& cors = app.get_middleware(); + cors.global() + .headers("Content-Type", "Authorization") + .methods("GET"_method, "POST"_method, "PUT"_method, "DELETE"_method, "OPTIONS"_method) + .origin(settings.domain); // create global database dbpool = std::make_unique(settings.db_path); @@ -33,10 +39,25 @@ int main() { return 1; } + // options path + CROW_ROUTE(app, "/") .methods("OPTIONS"_method) + ([&settings](const crow::request&, crow::response& res, const std::string&) { + res.add_header("Access-Control-Allow-Origin", settings.domain); + res.add_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); + res.add_header("Access-Control-Allow-Headers", "Content-Type, Authorization"); + res.add_header("Access-Control-Max-Age", "86400"); // Cache preflight for 24h + res.code = 204; + res.end(); + }); + // Root route CROW_ROUTE(app, "/")([&]() { - auto data = utils::read_file(utils::build_dir / "index.html"); - return crow::response(200, "text/html", data); + return utils::getFile(utils::build_dir / "index.html"); + }); + + CROW_ROUTE(app, "/favicon.ico").methods(crow::HTTPMethod::Get) + ([&]() { + return utils::getFile(utils::assets_dir / "favicon.ico"); }); shadowrun::initApi(app); @@ -45,8 +66,7 @@ int main() { // asssets is not svelte generated CROW_ROUTE(app, "/assets/") ([&](const std::string& p) { - const filesystem::path assets_dir = "assets/"; // <-- set your build folder - filesystem::path file_path = assets_dir / p; + filesystem::path file_path = utils::assets_dir / p; return utils::getFile(file_path); }); diff --git a/source/shadowrun/ShadowrunApi.cpp b/source/shadowrun/ShadowrunApi.cpp index be46955..d30c2ee 100644 --- a/source/shadowrun/ShadowrunApi.cpp +++ b/source/shadowrun/ShadowrunApi.cpp @@ -25,7 +25,7 @@ static std::unordered_map parse_query_string(const std return params; } -void initApi(crow::App& app){ +void initApi(crow::App& app){ CROW_ROUTE(app, "/assets/shadowrun/") ([&](const crow::request& req, const std::string& p) { diff --git a/source/shadowrun/ShadowrunApi.hpp b/source/shadowrun/ShadowrunApi.hpp index ede7333..db8870c 100644 --- a/source/shadowrun/ShadowrunApi.hpp +++ b/source/shadowrun/ShadowrunApi.hpp @@ -1,12 +1,12 @@ #ifndef __SHADOWRUNAPI_H__ #define __SHADOWRUNAPI_H__ -#include "cors.h" -#include +#include "crow.h" +#include "crow/middlewares/cors.h" namespace shadowrun { -void initApi(crow::App& app); +void initApi(crow::App& app); } #endif // __SHADOWRUNAPI_H__ \ No newline at end of file diff --git a/source/utils.cpp b/source/utils.cpp index 554c46c..d515edf 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -164,6 +164,7 @@ std::string get_mime_type(const std::string& path) { if (path.ends_with(".woff")) return "font/woff"; if (path.ends_with(".woff2")) return "font/woff2"; if (path.ends_with(".pdf")) return "application/pdf"; + if (path.ends_with(".ico")) return "image/x-icon"; return "application/octet-stream"; } diff --git a/source/utils.hpp b/source/utils.hpp index 0b558e8..50f37ee 100644 --- a/source/utils.hpp +++ b/source/utils.hpp @@ -10,7 +10,10 @@ #include "crow.h" namespace utils { - const std::filesystem::path build_dir = "frontend/build/"; // <-- set your build folder + // Svelte genereated files + const std::filesystem::path build_dir = "frontend/build/"; + // Non-autogenerated assets folder + const std::filesystem::path assets_dir = "assets/"; std::map parseBody(const std::string& body);