added favicon
This commit is contained in:
@@ -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__
|
||||
@@ -12,7 +12,7 @@ Settings AppSettings::deafult(){
|
||||
return Settings {
|
||||
.http_port = 3010,
|
||||
.db_path = "/var/lib/shadowrun-server/shadowrun.db",
|
||||
.url = "*"
|
||||
.domain = "*"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -45,8 +45,7 @@ std::optional<std::string> loginUser(const std::string& username, const std::str
|
||||
return {};
|
||||
}
|
||||
|
||||
void initLogin(crow::App<CORS>& app)
|
||||
{
|
||||
void initLogin(crow::App<crow::CORSHandler>& app){
|
||||
|
||||
createUser("lukas", "Trollar4928");
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <crow.h>
|
||||
#include "cors.h"
|
||||
#include "crow/middlewares/cors.h"
|
||||
|
||||
namespace login {
|
||||
|
||||
void initLogin(crow::App<CORS>& app);
|
||||
void initLogin(crow::App<crow::CORSHandler>& app);
|
||||
|
||||
std::optional<crow::response> isLoggedIn(const crow::request& req);
|
||||
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
#include <crow.h>
|
||||
#include "crow/middlewares/cors.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;
|
||||
AppSettings::Settings settings = AppSettings::load();
|
||||
crow::App<crow::CORSHandler> app;
|
||||
|
||||
// init cors
|
||||
auto& cors = app.get_middleware<crow::CORSHandler>();
|
||||
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<DatabasePool>(settings.db_path);
|
||||
@@ -33,10 +39,25 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// options path
|
||||
CROW_ROUTE(app, "/<path>") .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/<path>")
|
||||
([&](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);
|
||||
});
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ static std::unordered_map<std::string, std::string> parse_query_string(const std
|
||||
return params;
|
||||
}
|
||||
|
||||
void initApi(crow::App<CORS>& app){
|
||||
void initApi(crow::App<crow::CORSHandler>& app){
|
||||
|
||||
CROW_ROUTE(app, "/assets/shadowrun/<path>")
|
||||
([&](const crow::request& req, const std::string& p) {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#ifndef __SHADOWRUNAPI_H__
|
||||
#define __SHADOWRUNAPI_H__
|
||||
|
||||
#include "cors.h"
|
||||
#include <crow.h>
|
||||
#include "crow.h"
|
||||
#include "crow/middlewares/cors.h"
|
||||
|
||||
namespace shadowrun {
|
||||
|
||||
void initApi(crow::App<CORS>& app);
|
||||
void initApi(crow::App<crow::CORSHandler>& app);
|
||||
|
||||
}
|
||||
#endif // __SHADOWRUNAPI_H__
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
@@ -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<std::string, std::string> parseBody(const std::string& body);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user