diff --git a/frontend/src/lib/config.ts b/frontend/src/lib/config.ts index d420860..44caf62 100644 --- a/frontend/src/lib/config.ts +++ b/frontend/src/lib/config.ts @@ -1 +1 @@ -export const API_BASE = "http://127.0.0.1:3010"; +export const API_BASE = `http://${window.location.hostname}:3010`; \ No newline at end of file diff --git a/frontend/src/lib/shadorwun/character.svelte b/frontend/src/lib/shadorwun/character.svelte index 3f3ed42..3cec880 100644 --- a/frontend/src/lib/shadorwun/character.svelte +++ b/frontend/src/lib/shadorwun/character.svelte @@ -85,48 +85,39 @@

Attributes

-
- - - - + + + + + + + + + + + + + + + + + + - - - - + + + + + + +
- - - - - - - - - - - - AgilityBody CharismaEdge
EssenceInitiative IntuitionLogic
- - - - - - - - - - - - ReactionStrengthWillpower
-

Skills

diff --git a/frontend/src/routes/+page.svelte b/frontend/src/routes/+page.svelte index cc88df0..83a2db6 100644 --- a/frontend/src/routes/+page.svelte +++ b/frontend/src/routes/+page.svelte @@ -1,2 +1,8 @@ -

Welcome to SvelteKit

-

Visit svelte.dev/docs/kit to read the documentation

+ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bc1f106..0b7c212 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,74 +37,6 @@ std::string get_mime_type(const std::string& path) { int main() { crow::SimpleApp app; -/* - CROW_ROUTE(app, "/")([] { - auto data = utils::loadFile("templates/index.html"); - if (data.empty()) - return crow::response(404); - return crow::response(data); - }); - - CROW_ROUTE(app, "/static/")([](const std::string& file) { - auto data = utils::loadFile("static/" + file); - if (data.empty()) - return crow::response(404); - return crow::response(data); - }); - - CROW_ROUTE(app, "/templates/")([](const std::string& file) { - auto data = utils::loadFile("templates/" + file); - if (data.empty()) - return crow::response(404); - return crow::response(data); - }); - - // Static file redirector - CROW_ROUTE(app, "/redirect") - (login::login_required([](const crow::request& req) { - auto file_param = req.url_params.get("file"); - if (!file_param) { - return crow::response(400, "Missing 'file' parameter"); - } - - std::string filepath = "/templates/"; - filepath += utils::urlDecode(file_param); // Optional: decode %20 etc. - - crow::response res; - res.code = 204; - res.add_header("HX-Redirect", filepath); - return res; - })); - - CROW_ROUTE(app, "/status")(login::login_required([](const crow::request& req) { - auto table = create_service_table(); - return crow::response{table.htmx()}; - })); - - CROW_ROUTE(app, "/toggle-service").methods(crow::HTTPMethod::Post) - (login::login_required([](const crow::request& req) { - auto body = utils::getBodyName(req.body); - if (!body.has_value()) - return crow::response(400); - - const string& serviceName = body.value(); - - auto opt_settings = AppSettings::loadAppSettings(); - - HtmxTableRow row; - if (opt_settings.has_value()) { - auto& settings = opt_settings.value(); - const auto& service_id = settings.getId(serviceName).value_or(serviceName); - - systemd::toggle_service(service_id); - row = create_service_table_row(serviceName, service_id); - } - else { - row = create_error_table_row(opt_settings.error()); - } - return crow::response{row.htmx()}; - })); -*/ const filesystem::path build_dir = "frontend/build/"; // <-- set your build folder // Root route @@ -184,5 +116,5 @@ int main() { } */ app.loglevel(crow::LogLevel::INFO); - app.port(httpPort).multithreaded().run(); + app.bindaddr("0.0.0.0").port(httpPort).multithreaded().run(); } diff --git a/src/shadowrun/ShadowrunApi.cpp b/src/shadowrun/ShadowrunApi.cpp index fa53e03..7f8fce6 100644 --- a/src/shadowrun/ShadowrunApi.cpp +++ b/src/shadowrun/ShadowrunApi.cpp @@ -34,80 +34,6 @@ static crow::response rsp(const std::string& msg){ void initApi(crow::SimpleApp& app) { -/* - CROW_ROUTE(app, "/api/shadowrun/submit-character").methods("POST"_method) - (login::login_required([](const crow::request& req) { - auto params = parse_query_string(req.body); - - auto name_data = params["Character-Info_Name"]; - if (name_data.empty()){ - CROW_LOG_WARNING << "Character without name submited, will not be saved"; - return rsp("Failed : Character without name submited, will not be saved"); - } - - auto key = getKeyOfCharacter(name_data); - if (key < 0){ - CROW_LOG_ERROR << "Failed to create id of character : " << name_data; - return rsp("Failed to create id of character"); - } - - vector> idValues; - idValues.reserve(ShadowrunCharacterForm::m_formIds.size()); - - auto checkboxes = std::set(ShadowrunCharacterForm::m_checkboxIds.begin(), ShadowrunCharacterForm::m_checkboxIds.end()); - for (auto& id : ShadowrunCharacterForm::m_formIds) { - auto data = params[id]; - if(!data.empty()){ - idValues.push_back(make_pair(id, data)); - if (checkboxes.contains(id)){ - checkboxes.erase(id); - } - } - } - - // append the checkboxes - for (auto& checkbox : checkboxes){ - idValues.push_back(make_pair(checkbox, "0")); - } - - if (!storeCharacterData(key, idValues)){ - CROW_LOG_ERROR << "Failed to store character data of " << name_data; - return rsp("Failed to store character data"); - }; - return rsp(format("Character {} submitted successfully", name_data)); - })); - - CROW_ROUTE(app, "/api/shadowrun/character-form") - (login::login_required([](const crow::request& req) { - auto query = crow::query_string(req.url_params); - std::string name = query.get("name") ? query.get("name") : ""; - - auto data = getCharacterDataMap(getKeyOfCharacter(name)); - - return crow::response{ShadowrunCharacterForm(data).htmx()}; - })); -*/ - CROW_ROUTE(app, "/api/shadowrun/character-list") - (login::login_required([](const crow::request& req) { - std::ostringstream html; - - // Simulated character database - auto characters = getCharacters(); - html << "
" - << "" - << "" - << ""; - - return crow::response{html.str()}; - })); - CROW_ROUTE(app, "/api/shadowrun/characters") ([&]() { auto characters = getCharacters(); diff --git a/src/shadowrun/ShadowrunDb.cpp b/src/shadowrun/ShadowrunDb.cpp index 8f5f15c..f2f867b 100644 --- a/src/shadowrun/ShadowrunDb.cpp +++ b/src/shadowrun/ShadowrunDb.cpp @@ -12,40 +12,36 @@ int64_t createCharacter(const string& name){ if (name.empty()) return -1; + int64_t id; auto db = dbpool.acquire(); - auto character = db->get_all( - where(c(&ShadowrunCharacter::name) == name), - limit(1) + auto character = db->get_optional( + where(c(&ShadowrunCharacter::name) == name) ); - if (!character.empty()) { - return character[0].id; + if (character.has_value()) { + id = character.value().id; } else { auto c = newShadowrunCharacter(name); - return db->insert(c); + id = db->insert(c); } + dbpool.release(db); + return id; } std::vector getCharacters(){ auto db = dbpool.acquire(); - return db->get_all(); + auto characters = db->get_all(); + dbpool.release(db); + return characters; } optional getChracter(int id) { auto db = dbpool.acquire(); - auto character = db->get_all( - where(c(&ShadowrunCharacter::id) == id), - limit(1) - ); - if (character.empty()) - { - return {}; - } - else { - return character[0]; - } + optional character = db->get_optional(id); + dbpool.release(db); + return character; } vector getChracterData(int character_id) @@ -54,10 +50,12 @@ vector getChracterData(int character_id) auto characterData = db->get_all( where(c(&ShadowrunCharacterData::character_id) == character_id) ); + dbpool.release(db); return characterData; } int storeCharacterData(int characterId, const Type type, const string& json){ + int id; auto db = dbpool.acquire(); auto characterData = db->get_all( where( @@ -67,7 +65,7 @@ int storeCharacterData(int characterId, const Type type, const string& json){ if(characterData.empty()){ ShadowrunCharacterData character = newShadowrunCharacterData(characterId, type, json); - return db->insert(character); + id = db->insert(character); } else { if (characterData.size() > 1){ @@ -77,21 +75,26 @@ int storeCharacterData(int characterId, const Type type, const string& json){ character.json = json; character.updated_at = utils::currentTime(); db->update(character); - return character.id; + id = character.id; } + dbpool.release(db); + return id; } int storeCharacterData(const ShadowrunCharacterData& data){ + int id; auto db = dbpool.acquire(); auto characterData = db->get_optional(data.id); if(!characterData.has_value()){ - return db->insert(data); + id = db->insert(data); } else { db->update(data); - return data.id; + id = data.id; } + dbpool.release(db); + return id; } } \ No newline at end of file