first commit to move to svelte
This commit is contained in:
@@ -12,14 +12,6 @@ Database::~Database() {
|
||||
sqlite3_close(m_db);
|
||||
}
|
||||
|
||||
string Database::currentTime(){
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(std::gmtime(&t), "%Y-%m-%d %H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
sqlite3_stmt* Database::prepareStmt(const string& sql){
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
|
||||
@@ -30,8 +30,6 @@ public:
|
||||
|
||||
std::set<std::string> getStrSet(const std::string& sql);
|
||||
|
||||
static std::string currentTime();
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
||||
{
|
||||
@@ -85,21 +83,22 @@ private:
|
||||
|
||||
|
||||
inline auto make_database() {
|
||||
return sqlite_orm::make_storage(Database::dbFile,
|
||||
auto storage = sqlite_orm::make_storage(Database::dbFile,
|
||||
sqlite_orm::make_table("shadowrun_characters",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunCharacter::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("name", &shadowrun::ShadowrunCharacter::name, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunCharacter::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP"))
|
||||
),
|
||||
sqlite_orm::make_table("shadowrun_data",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunData::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("character_id", &shadowrun::ShadowrunData::character_id, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("name", &shadowrun::ShadowrunData::name, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("value", &shadowrun::ShadowrunData::value),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunData::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::make_column("updated_at", &shadowrun::ShadowrunData::updated_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::foreign_key(&shadowrun::ShadowrunData::character_id).references(&shadowrun::ShadowrunCharacter::id).on_delete.cascade()
|
||||
sqlite_orm::make_table("shadowrun_characters_data",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunCharacterData::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("character_id", &shadowrun::ShadowrunCharacterData::character_id, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("type", &shadowrun::ShadowrunCharacterData::type, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("json", &shadowrun::ShadowrunCharacterData::json),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunCharacterData::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::make_column("updated_at", &shadowrun::ShadowrunCharacterData::updated_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::foreign_key(&shadowrun::ShadowrunCharacterData::character_id).references(&shadowrun::ShadowrunCharacter::id).on_delete.cascade()
|
||||
));
|
||||
return storage;
|
||||
}
|
||||
|
||||
#endif // __DATABASE_H__
|
||||
61
src/main.cpp
61
src/main.cpp
@@ -10,9 +10,33 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::string read_file(const std::string& path) {
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file) {
|
||||
throw std::runtime_error("Cannot open file: " + path);
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << file.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// Simple MIME type detection
|
||||
std::string get_mime_type(const std::string& path) {
|
||||
if (path.ends_with(".html")) return "text/html";
|
||||
if (path.ends_with(".js")) return "text/javascript";
|
||||
if (path.ends_with(".css")) return "text/css";
|
||||
if (path.ends_with(".json")) return "application/json";
|
||||
if (path.ends_with(".svg")) return "image/svg+xml";
|
||||
if (path.ends_with(".png")) return "image/png";
|
||||
if (path.ends_with(".jpg") || path.ends_with(".jpeg")) return "image/jpeg";
|
||||
if (path.ends_with(".woff")) return "font/woff";
|
||||
if (path.ends_with(".woff2")) return "font/woff2";
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
int main() {
|
||||
crow::SimpleApp app;
|
||||
|
||||
/*
|
||||
CROW_ROUTE(app, "/")([] {
|
||||
auto data = utils::loadFile("templates/index.html");
|
||||
if (data.empty())
|
||||
@@ -79,6 +103,14 @@ int main() {
|
||||
}
|
||||
return crow::response{row.htmx()};
|
||||
}));
|
||||
*/
|
||||
const filesystem::path build_dir = "frontend/build/"; // <-- set your build folder
|
||||
|
||||
// Root route
|
||||
CROW_ROUTE(app, "/")([&]() {
|
||||
auto data = read_file(build_dir / "index.html");
|
||||
return crow::response(200, "text/html", data);
|
||||
});
|
||||
|
||||
const uint16_t defaultPort = 3010;
|
||||
uint16_t httpPort = defaultPort;
|
||||
@@ -103,11 +135,34 @@ int main() {
|
||||
}
|
||||
|
||||
shadowrun::initApi(app);
|
||||
if(!login::initLogin(app))
|
||||
|
||||
// Catch-all route for static files and SPA fallback
|
||||
CROW_ROUTE(app, "/<path>")
|
||||
([&](const std::string& p) {
|
||||
filesystem::path file_path = build_dir / p;
|
||||
|
||||
// If path is a directory, serve index.html inside it
|
||||
if (filesystem::is_directory(file_path))
|
||||
file_path /= "index.html";
|
||||
|
||||
// If file exists, serve it
|
||||
if (filesystem::exists(file_path)) {
|
||||
auto data = read_file(file_path);
|
||||
auto mime = get_mime_type(file_path.string());
|
||||
return crow::response(200, mime.c_str(), data);
|
||||
}
|
||||
|
||||
// SPA fallback: serve root index.html for unknown routes
|
||||
filesystem::path fallback = build_dir / "index.html";
|
||||
auto data = read_file(fallback);
|
||||
return crow::response(404, "text/html", data);
|
||||
});
|
||||
|
||||
/* if(!login::initLogin(app))
|
||||
{
|
||||
CROW_LOG_ERROR << "Failed to init Login API";
|
||||
}
|
||||
|
||||
*/
|
||||
app.loglevel(crow::LogLevel::INFO);
|
||||
app.port(httpPort).multithreaded().run();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ static std::unordered_map<std::string, std::string> parse_query_string(const std
|
||||
params[key] = value; // You may want to URL-decode here
|
||||
}
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@@ -37,6 +36,7 @@ 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);
|
||||
@@ -88,7 +88,7 @@ void initApi(crow::SimpleApp& app)
|
||||
|
||||
return crow::response{ShadowrunCharacterForm(data).htmx()};
|
||||
}));
|
||||
|
||||
*/
|
||||
CROW_ROUTE(app, "/api/shadowrun/character-list")
|
||||
(login::login_required([](const crow::request& req) {
|
||||
std::ostringstream html;
|
||||
@@ -110,6 +110,42 @@ void initApi(crow::SimpleApp& app)
|
||||
return crow::response{html.str()};
|
||||
}));
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters")
|
||||
([&]() {
|
||||
auto characters = getCharacters();
|
||||
return utils::toJsonArray(characters);
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters").methods("POST"_method)
|
||||
([](const crow::request& 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()){
|
||||
return crow::response(200, nlohmann::json(character.value()).dump());
|
||||
}
|
||||
}
|
||||
return crow::response(405, "Failed to create character");
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters/<int>")
|
||||
([&](int id) {
|
||||
auto optCharacter = getChracter(id);
|
||||
if (!optCharacter.has_value())
|
||||
return crow::response(404, "Character not found");
|
||||
return crow::response(200, nlohmann::json(optCharacter.value()).dump());
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>")
|
||||
([&](int id) {
|
||||
auto character_data = getChracterData(id);
|
||||
if (!character_data.empty())
|
||||
return crow::response(405, "No character data found");
|
||||
return crow::response(200, utils::toJsonArray(character_data));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include "database.hpp"
|
||||
#include "databasepool.h"
|
||||
#include "crow.h"
|
||||
#include "utils.hpp"
|
||||
#include "sqlite_orm.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -12,7 +14,10 @@ using namespace sqlite_orm;
|
||||
|
||||
namespace shadowrun {
|
||||
|
||||
int64_t getKeyOfCharacter(const string& name){
|
||||
int64_t createCharacter(const string& name){
|
||||
if (name.empty())
|
||||
return -1;
|
||||
|
||||
auto db = dbpool.acquire();
|
||||
|
||||
auto character = db->get_all<ShadowrunCharacter>(
|
||||
@@ -23,13 +28,15 @@ int64_t getKeyOfCharacter(const string& name){
|
||||
if (!character.empty()) {
|
||||
return character[0].id;
|
||||
} else {
|
||||
return db->insert(ShadowrunCharacter{-1, name, Database::currentTime()});
|
||||
auto c = createShadowrunCharacter(name);
|
||||
return db->insert(c);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
bool storeCharacterData(int64_t characterKey, vector<pair<const string, const string>>& idValues){
|
||||
auto characterData = getCharacterData(characterKey);
|
||||
std::map<string, ShadowrunData*> dataMap;
|
||||
std::map<string, ShadowrunCharacterData*> dataMap;
|
||||
for(auto& data : characterData) {
|
||||
dataMap[data.name] = &data;
|
||||
}
|
||||
@@ -45,7 +52,7 @@ bool storeCharacterData(int64_t characterKey, vector<pair<const string, const st
|
||||
db->update_all(
|
||||
sqlite_orm::set(
|
||||
assign(&ShadowrunData::value, idValue.second),
|
||||
assign(&ShadowrunData::updated_at, Database::currentTime())
|
||||
assign(&ShadowrunData::updated_at, utils::currentTime())
|
||||
),
|
||||
where(
|
||||
c(&ShadowrunData::name) == name and
|
||||
@@ -58,7 +65,7 @@ bool storeCharacterData(int64_t characterKey, vector<pair<const string, const st
|
||||
.character_id = static_cast<int>(characterKey),
|
||||
.name = name,
|
||||
.value = value,
|
||||
.created_at = Database::currentTime(),
|
||||
.created_at = utils::currentTime(),
|
||||
.updated_at = "",
|
||||
};
|
||||
db->insert(entry);
|
||||
@@ -66,26 +73,59 @@ bool storeCharacterData(int64_t characterKey, vector<pair<const string, const st
|
||||
}
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
std::vector<ShadowrunCharacter> getCharacters(){
|
||||
auto db = dbpool.acquire();
|
||||
return db->get_all<ShadowrunCharacter>();
|
||||
}
|
||||
|
||||
std::map<string, string> getCharacterDataMap(int64_t characterKey){
|
||||
auto characterData = getCharacterData(characterKey);
|
||||
std::map<string, string> dataMap;
|
||||
for(auto& data : characterData) {
|
||||
dataMap[data.name] = data.value;
|
||||
optional<ShadowrunCharacter> getChracter(int id)
|
||||
{
|
||||
auto db = dbpool.acquire();
|
||||
auto character = db->get_all<ShadowrunCharacter>(
|
||||
where(c(&ShadowrunCharacter::id) == id),
|
||||
limit(1)
|
||||
);
|
||||
if (character.empty())
|
||||
{
|
||||
return {};
|
||||
}
|
||||
else {
|
||||
return character[0];
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
vector<ShadowrunCharacterData> getChracterData(int character_id)
|
||||
{
|
||||
auto db = dbpool.acquire();
|
||||
auto characterData = db->get_all<ShadowrunCharacterData>(
|
||||
where(c(&ShadowrunCharacterData::character_id) == character_id)
|
||||
);
|
||||
return characterData;
|
||||
}
|
||||
|
||||
int storeCharacterData(const ShadowrunCharacterData& data)
|
||||
{
|
||||
auto db = dbpool.acquire();
|
||||
auto characterData = db->get_optional<ShadowrunCharacterData>(data.id);
|
||||
|
||||
if(!characterData.has_value()){
|
||||
return db->insert(data);
|
||||
}
|
||||
else {
|
||||
db->update(data);
|
||||
return data.id;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
vector<ShadowrunData> getCharacterData(int64_t characterKey) {
|
||||
auto db = dbpool.acquire();
|
||||
return db->get_all<ShadowrunData>(
|
||||
where(c(&ShadowrunData::character_id) == characterKey)
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -4,8 +4,10 @@
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include "json.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace shadowrun {
|
||||
struct ShadowrunCharacter {
|
||||
int id;
|
||||
@@ -13,21 +15,28 @@ namespace shadowrun {
|
||||
std::string created_at; // SQLite stores DATETIME as TEXT
|
||||
};
|
||||
|
||||
struct ShadowrunData {
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ShadowrunCharacter, id, name, created_at)
|
||||
|
||||
inline ShadowrunCharacter createShadowrunCharacter(const std::string& name){
|
||||
return ShadowrunCharacter {-1, name, utils::currentTime()};
|
||||
}
|
||||
struct ShadowrunCharacterData {
|
||||
int id;
|
||||
int character_id;
|
||||
std::string name;
|
||||
std::string value;
|
||||
std::string type;
|
||||
std::string json;
|
||||
std::string created_at;
|
||||
std::string updated_at;
|
||||
};
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ShadowrunCharacterData, id, character_id, type, json, created_at, updated_at);
|
||||
|
||||
int64_t getKeyOfCharacter(const std::string& name);
|
||||
int64_t createCharacter(const std::string& name);
|
||||
bool storeCharacterData(int64_t characterKey, std::vector<std::pair<const std::string, const std::string>>& idValues);
|
||||
std::vector<ShadowrunCharacter> getCharacters();
|
||||
std::optional<ShadowrunCharacter> getChracter(int id);
|
||||
|
||||
std::vector<ShadowrunData> getCharacterData(int64_t characterKey);
|
||||
std::map<std::string, std::string> getCharacterDataMap(int64_t characterKey);
|
||||
std::vector<ShadowrunCharacterData> getChracterData(int character_id);
|
||||
int storeCharacterData(const ShadowrunCharacterData& data);
|
||||
}
|
||||
|
||||
#endif // __SHADOWRUNDB_H__
|
||||
@@ -134,4 +134,13 @@ std::string urlDecode(const std::string& str) {
|
||||
return decoded.str();
|
||||
}
|
||||
|
||||
|
||||
string currentTime(){
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(std::gmtime(&t), "%Y-%m-%d %H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include "json.hpp"
|
||||
|
||||
namespace utils {
|
||||
std::map<std::string, std::string> parseBody(const std::string& body);
|
||||
@@ -21,6 +22,17 @@ namespace utils {
|
||||
std::filesystem::path getDataDir();
|
||||
|
||||
std::string urlDecode(const std::string& str);
|
||||
|
||||
std::string currentTime();
|
||||
|
||||
template<typename T>
|
||||
std::string toJsonArray(const std::vector<T>& data){
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (auto& c : data) {
|
||||
arr.push_back(nlohmann::json(c));
|
||||
}
|
||||
return arr.dump();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user