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

View File

@@ -35,37 +35,37 @@ endforeach()
include_directories( include_directories(
modules/cpp-libraries/include/ modules/cpp-libraries/include/
src source
src/shadowrun source/shadowrun
src/database source/database
src/login source/login
) )
add_executable(${TARGET_NAME} add_executable(${TARGET_NAME}
modules/cpp-libraries/src/sqlite3.c modules/cpp-libraries/src/sqlite3.c
modules/cpp-libraries/src/monocypher.c modules/cpp-libraries/src/monocypher.c
src/main.cpp source/main.cpp
src/utils.hpp source/utils.hpp
src/utils.cpp source/utils.cpp
src/json_settings.cpp source/json_settings.cpp
src/json_settings.h source/json_settings.h
src/database/database.cpp source/database/database.cpp
src/database/database.hpp source/database/database.hpp
# Shadowrun # Shadowrun
src/shadowrun/ShadowrunApi.cpp source/shadowrun/ShadowrunApi.cpp
src/shadowrun/ShadowrunApi.hpp source/shadowrun/ShadowrunApi.hpp
src/shadowrun/ShadowrunDb.cpp source/shadowrun/ShadowrunDb.cpp
src/shadowrun/ShadowrunDb.hpp source/shadowrun/ShadowrunDb.hpp
# login # login
src/login/login.cpp source/login/login.cpp
src/login/login.hpp source/login/login.hpp
src/login/loginDb.cpp source/login/loginDb.cpp
src/login/Session.cpp source/login/Session.cpp
src/login/SessionHandler.cpp source/login/SessionHandler.cpp
) )
# warnings to ignore # warnings to ignore

View File

@@ -11,6 +11,7 @@ makedepends=('cmake' 'gcc')
source=( source=(
${pkgname}-${pkgver}.tar.gz ${pkgname}-${pkgver}.tar.gz
) )
install=shadowrun-server.install
md5sums=('SKIP') # SKIP if local files md5sums=('SKIP') # SKIP if local files
build() { build() {
cmake -S "${srcdir}" \ cmake -S "${srcdir}" \

View File

@@ -32,7 +32,7 @@ npm run build
1. tar the source files to make it cleaner 1. tar the source files to make it cleaner
``` ```
tar czf pkg/shadowrun-server-0.1.1.tar.gz src/ modules/ assets/ frontend/build/ shadowrun-server.service CMakeLists.txt tar czf shadowrun-server-0.1.1.tar.gz source/ modules/ assets/ frontend/build/ shadowrun-server.service CMakeLists.txt shadowrun-server.install
``` ```
2. create the package 2. create the package
@@ -41,6 +41,11 @@ tar czf pkg/shadowrun-server-0.1.1.tar.gz src/ modules/ assets/ frontend/build/
makepkg -f makepkg -f
``` ```
3. place the package in archrepo on server
```
scp shadowrun-server-0.1.1-1-x86_64.pkg.tar.zst lukas@192.168.1.101:/home/lukas/Drive/archrepo/x86_64/
```
## Database ## Database
### Attributes ### Attributes

View File

@@ -1,4 +1,5 @@
{ {
"http_port" : 3010, "http_port" : 3010,
"db_path": "/var/lib/shadowrun-server/shadowrun.db" "db_path": "/var/lib/shadowrun-server/shadowrun.db",
"url": "*"
} }

4
shadowrun-server.install Normal file
View File

@@ -0,0 +1,4 @@
post_install() {
getent group shadowrun >/dev/null || groupadd -r shadowrun
getent passwd shadowrun >/dev/null || useradd -r -g shadowrun -d /var/lib/shadowrun-server -s /usr/bin/nologin shadowrun
}

View File

@@ -3,6 +3,9 @@ Description=shadowrun server Service
After=network.target After=network.target
[Service] [Service]
User=shadowrun
Group=shadowrun
StateDirectory=shadowrun-server
Type=simple Type=simple
WorkingDirectory=/usr/share/shadowrun-server WorkingDirectory=/usr/share/shadowrun-server
ExecStart=/usr/bin/shadowrun-server ExecStart=/usr/bin/shadowrun-server

29
source/cors.h Normal file
View File

@@ -0,0 +1,29 @@
#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__

View File

@@ -11,7 +11,8 @@ using namespace::AppSettings;
Settings AppSettings::deafult(){ Settings AppSettings::deafult(){
return Settings { return Settings {
.http_port = 3010, .http_port = 3010,
.db_path = "/var/lib/shadowrun-server/shadowrun.db" .db_path = "/var/lib/shadowrun-server/shadowrun.db",
.url = "*"
}; };
} }

View File

@@ -10,11 +10,12 @@ static constexpr char settingsFile[] = "assets/settings.json";
struct Settings { struct Settings {
int http_port; int http_port;
std::string db_path; std::string db_path;
std::string url;
}; };
Settings load(); Settings load();
Settings deafult(); Settings deafult();
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Settings, http_port, db_path); NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Settings, http_port, db_path, url);
} }
#endif // JSON_SETTINGS_H #endif // JSON_SETTINGS_H

View File

@@ -45,7 +45,7 @@ std::optional<std::string> loginUser(const std::string& username, const std::str
return {}; return {};
} }
void initLogin(crow::SimpleApp& app) void initLogin(crow::App<CORS>& app)
{ {
createUser("lukas", "Trollar4928"); createUser("lukas", "Trollar4928");

View File

@@ -4,10 +4,11 @@
#pragma once #pragma once
#include <crow.h> #include <crow.h>
#include "cors.h"
namespace login { namespace login {
void initLogin(crow::SimpleApp& app); void initLogin(crow::App<CORS>& app);
std::optional<crow::response> isLoggedIn(const crow::request& req); std::optional<crow::response> isLoggedIn(const crow::request& req);

View File

@@ -2,16 +2,17 @@
#include <string> #include <string>
#include "json_settings.h" #include "json_settings.h"
#include "utils.hpp" #include "utils.hpp"
#include "cors.h"
#include "login.hpp" #include "login.hpp"
#include "ShadowrunApi.hpp" #include "ShadowrunApi.hpp"
#include "databasepool.h" #include "databasepool.h"
using namespace std; using namespace std;
int main() { AppSettings::Settings settings = AppSettings::load();
crow::SimpleApp app;
auto settings = AppSettings::load(); int main() {
crow::App<CORS> app;
// create global database // create global database
dbpool = std::make_unique<DatabasePool>(settings.db_path); dbpool = std::make_unique<DatabasePool>(settings.db_path);

View File

@@ -25,7 +25,7 @@ static std::unordered_map<std::string, std::string> parse_query_string(const std
return params; return params;
} }
void initApi(crow::SimpleApp& app){ void initApi(crow::App<CORS>& app){
CROW_ROUTE(app, "/assets/shadowrun/<path>") CROW_ROUTE(app, "/assets/shadowrun/<path>")
([&](const crow::request& req, const std::string& p) { ([&](const crow::request& req, const std::string& p) {

View File

@@ -1,11 +1,12 @@
#ifndef __SHADOWRUNAPI_H__ #ifndef __SHADOWRUNAPI_H__
#define __SHADOWRUNAPI_H__ #define __SHADOWRUNAPI_H__
#include "cors.h"
#include <crow.h> #include <crow.h>
namespace shadowrun { namespace shadowrun {
void initApi(crow::SimpleApp& app); void initApi(crow::App<CORS>& app);
} }
#endif // __SHADOWRUNAPI_H__ #endif // __SHADOWRUNAPI_H__