signal handling is my 14th reason why and also untested code go brr
This commit is contained in:
parent
d6360d207a
commit
eee382cae9
2 changed files with 16 additions and 45 deletions
59
main.cpp
59
main.cpp
|
|
@ -8,10 +8,14 @@
|
|||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <format>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using Db = std::vector<std::vector<std::string>>;
|
||||
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<std::string>& 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<std::string> parse_resp_request(char* request) {
|
||||
std::string request_str(request);
|
||||
|
|
@ -97,11 +62,18 @@ std::vector<std::string> 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<std::string> parsed = parse_resp_request(buffer);
|
||||
|
||||
if (parsed[0] == "GET") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue