added CORS
This commit is contained in:
128
source/shadowrun/ShadowrunApi.cpp
Normal file
128
source/shadowrun/ShadowrunApi.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
|
||||
|
||||
#include "ShadowrunApi.hpp"
|
||||
#include "ShadowrunDb.hpp"
|
||||
#include "login.hpp"
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
namespace shadowrun
|
||||
{
|
||||
|
||||
static std::unordered_map<std::string, std::string> parse_query_string(const std::string& query) {
|
||||
std::unordered_map<std::string, std::string> params;
|
||||
std::istringstream stream(query);
|
||||
std::string pair;
|
||||
|
||||
while (std::getline(stream, pair, '&')) {
|
||||
auto pos = pair.find('=');
|
||||
if (pos != std::string::npos) {
|
||||
std::string key = pair.substr(0, pos);
|
||||
std::string value = pair.substr(pos + 1);
|
||||
params[key] = value; // You may want to URL-decode here
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
void initApi(crow::App<CORS>& app){
|
||||
|
||||
CROW_ROUTE(app, "/assets/shadowrun/<path>")
|
||||
([&](const crow::request& req, const std::string& p) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
|
||||
const filesystem::path assets_dir = "assets/shadowrun/";
|
||||
filesystem::path file_path = assets_dir / p;
|
||||
return utils::getFile(file_path);
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters")
|
||||
([&](const crow::request& req) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
|
||||
auto characters = getCharacters();
|
||||
auto res =
|
||||
crow::response(200, utils::toJsonArray(characters));
|
||||
res.set_header("Content-Type", "application/json");
|
||||
return res;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters").methods("POST"_method)
|
||||
([](const crow::request& req) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
|
||||
nlohmann::json data = nlohmann::json::parse(req.body); // parse JSON from HTTP body
|
||||
auto name = data["name"];
|
||||
int id = createCharacter(name);
|
||||
if(id > 0){
|
||||
auto character = getChracter(id);
|
||||
if (character.has_value()){
|
||||
auto res = crow::response(200, nlohmann::json(character.value()).dump());
|
||||
res.set_header("Content-Type", "application/json");
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return crow::response(405, "Failed to create character");
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters/<int>")
|
||||
([&](const crow::request& req, int id) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
|
||||
auto optCharacter = getChracter(id);
|
||||
if (!optCharacter.has_value())
|
||||
return crow::response(404, "Character not found");
|
||||
|
||||
auto res = crow::response(200, nlohmann::json(optCharacter.value()).dump());
|
||||
res.set_header("Content-Type", "application/json");
|
||||
return res;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>")
|
||||
([&](const crow::request& req, int id) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
|
||||
nlohmann::json j;
|
||||
const auto characterData = getChracterData(id);
|
||||
|
||||
if(characterData.empty())
|
||||
return crow::response(405, "Character not found");
|
||||
|
||||
for(const auto& data : characterData ){
|
||||
const auto& key = magic_enum::enum_cast<Type>(data.type);
|
||||
if(key.has_value()){
|
||||
auto res = utils::parseJson(data.json);
|
||||
if(res){
|
||||
j[magic_enum::enum_name(key.value())] = res.value();
|
||||
} else {
|
||||
CROW_LOG_ERROR << "Failed to parse json: " << res.error();
|
||||
}
|
||||
} else {
|
||||
CROW_LOG_ERROR << "Read invalid type from database: " << data.type;
|
||||
}
|
||||
}
|
||||
|
||||
auto res = crow::response(200, j.dump());
|
||||
res.set_header("Content-Type", "application/json");
|
||||
return res;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>").methods("POST"_method)
|
||||
([&](const crow::request& req, int id) {
|
||||
LOGGIN_REQUIERED(req);
|
||||
nlohmann::json j = nlohmann::json::parse(req.body);
|
||||
|
||||
for (auto type : magic_enum::enum_values<Type>()) {
|
||||
const auto& key = magic_enum::enum_name(type);
|
||||
if (j.contains(key)){
|
||||
storeCharacterData(id, type, j[key].dump());
|
||||
}
|
||||
}
|
||||
|
||||
auto res = crow::response(200, "Saved Character data");
|
||||
return res;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user