From eee382cae9da540c25a70244294a694009e02c0c Mon Sep 17 00:00:00 2001 From: Xory Date: Tue, 7 Oct 2025 20:42:40 +0300 Subject: [PATCH] signal handling is my 14th reason why and also untested code go brr --- README.md | 2 +- main.cpp | 59 ++++++++++++++----------------------------------------- 2 files changed, 16 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 7463556..2186ca8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ And also my first serious C++ project. - [X] Remove/add entries - [X] Serialisation and saving - [X] FUCKING NETWORKING (probably minimal RESP) -- [ ] Proper startup/shutdown w/ load & save +- [X] Proper startup/shutdown w/ load & save - [ ] Multithreading - [ ] More types - [ ] Async diff --git a/main.cpp b/main.cpp index 7ceaab8..2a30251 100644 --- a/main.cpp +++ b/main.cpp @@ -8,10 +8,14 @@ #include #include #include +#include +#include +#include using Db = std::vector>; +Db database; -void save_database(Db& database, std::string file_name) { +void save_database(std::string file_name) { nlohmann::json json_data = database; std::string serialised_json_data = json_data.dump(); std::ofstream db_file(file_name); @@ -19,7 +23,7 @@ void save_database(Db& database, std::string file_name) { db_file.close(); } -void load_database(Db& database, std::string file_name) { +void load_database(std::string file_name) { std::ifstream db_file(file_name); std::stringstream raw_file_text; raw_file_text << db_file.rdbuf(); @@ -27,45 +31,6 @@ void load_database(Db& database, std::string file_name) { database = nlohmann::json::parse(raw_file_text.str()); } -void add_entry(Db& database, std::string key, std::string value) { - database.push_back({key, value}); -} - -void remove_entry(Db& database, std::string key) { - int index = 0; - for (std::vector& entry : database) { - index++; - if (entry[0] == key) { - database.erase(database.begin() + index); - } - } -} - -// void prompt_user(Db& database) { - // std::string mode; - // std::cin >> mode; - // if (mode == "add") { - // std::string key; - // std::string value; - // std::cin >> key; - // std::cin >> value; - // add_entry(database, key, value); - // } else if (mode == "rem") { - // std::string key; - // std::cin >> key; - // remove_entry(database, key); - // } else if (mode == "save") { - // std::string file; - // std::cin >> file; - // save_database(database, file); - // } else if (mode == "load") { - // std::string file; - // std::cin >> file; - // load_database(database, file); - // } else { - // prompt_user(database); - // } -// } std::vector parse_resp_request(char* request) { std::string request_str(request); @@ -97,11 +62,18 @@ std::vector parse_resp_request(char* request) { return split_request; } +void safe_exit(int signum) { + std::cout << "[W] Exiting..." << std::endl; + save_database("db.json"); + exit(signum); +} int main() { std::cout << "[i] Starting blueis server..." << std::endl; - Db database; - load_database(database, "db.json"); + + signal(SIGINT, safe_exit); + + load_database("db.json"); int listening_socket = socket(AF_INET, SOCK_STREAM, 0 ); if (listening_socket < 0) { std::cerr << "[E] Failed to create socket." << std::endl; @@ -149,7 +121,6 @@ int main() { if (bytes_received > 0) { buffer[bytes_received] = '\0'; - std::cout << "[i] Received: " << buffer << std::endl; std::vector parsed = parse_resp_request(buffer); if (parsed[0] == "GET") {