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(
modules/cpp-libraries/include/
src
src/shadowrun
src/database
src/login
source
source/shadowrun
source/database
source/login
)
add_executable(${TARGET_NAME}
modules/cpp-libraries/src/sqlite3.c
modules/cpp-libraries/src/monocypher.c
src/main.cpp
src/utils.hpp
src/utils.cpp
src/json_settings.cpp
src/json_settings.h
source/main.cpp
source/utils.hpp
source/utils.cpp
source/json_settings.cpp
source/json_settings.h
src/database/database.cpp
src/database/database.hpp
source/database/database.cpp
source/database/database.hpp
# Shadowrun
src/shadowrun/ShadowrunApi.cpp
src/shadowrun/ShadowrunApi.hpp
src/shadowrun/ShadowrunDb.cpp
src/shadowrun/ShadowrunDb.hpp
source/shadowrun/ShadowrunApi.cpp
source/shadowrun/ShadowrunApi.hpp
source/shadowrun/ShadowrunDb.cpp
source/shadowrun/ShadowrunDb.hpp
# login
src/login/login.cpp
src/login/login.hpp
src/login/loginDb.cpp
src/login/Session.cpp
src/login/SessionHandler.cpp
source/login/login.cpp
source/login/login.hpp
source/login/loginDb.cpp
source/login/Session.cpp
source/login/SessionHandler.cpp
)
# warnings to ignore

View File

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

View File

@@ -32,7 +32,7 @@ npm run build
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
@@ -41,6 +41,11 @@ tar czf pkg/shadowrun-server-0.1.1.tar.gz src/ modules/ assets/ frontend/build/
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
### Attributes

View File

@@ -1,4 +1,5 @@
{
"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
[Service]
User=shadowrun
Group=shadowrun
StateDirectory=shadowrun-server
Type=simple
WorkingDirectory=/usr/share/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(){
return Settings {
.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 {
int http_port;
std::string db_path;
std::string url;
};
Settings load();
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

View File

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

View File

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

View File

@@ -2,16 +2,17 @@
#include <string>
#include "json_settings.h"
#include "utils.hpp"
#include "cors.h"
#include "login.hpp"
#include "ShadowrunApi.hpp"
#include "databasepool.h"
using namespace std;
int main() {
crow::SimpleApp app;
AppSettings::Settings settings = AppSettings::load();
auto settings = AppSettings::load();
int main() {
crow::App<CORS> app;
// create global database
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;
}
void initApi(crow::SimpleApp& app){
void initApi(crow::App<CORS>& app){
CROW_ROUTE(app, "/assets/shadowrun/<path>")
([&](const crow::request& req, const std::string& p) {

View File

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