diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-01 03:35:15 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-01 03:35:15 +0100 |
commit | 1f679124bba936e7905c7f4c83186d0c961dca61 (patch) | |
tree | b096349bede8d9ca6f2ec9374a7556bc0dfead29 | |
parent | d5861096a975274fcce72b45fe4ed38c96c61610 (diff) |
Switch to SQLite storage
-rw-r--r-- | whiteboard.cpp | 55 |
1 files changed, 25 insertions, 30 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp index 6d07576..2718937 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -7,12 +7,15 @@ #include <fcgiapp.h> +#include <chrono> #include <iostream> #include <functional> #include <filesystem> +#include <mutex> #include <random> #include <regex> #include <string> +#include <thread> #include <unordered_map> #include <boost/algorithm/string/predicate.hpp> @@ -67,33 +70,6 @@ namespace { return "endofcodes"; } - void setFileById(const std::string& data, const std::string& id) - { - fs::path path{data_path / id}; - - // skip if it doesn't exists - if (data.empty() && !fs::exists(path)) - return; - - // clean up immediately, if appropriate - if (data.empty()) { - fs::remove(path); - return; - } - - File::setFile(path, data); - } - - std::string getFileById(const std::string& id) - { - fs::path path{data_path / id}; - - if (!fs::exists(path)) - return {}; - - return File::getFile(path); - } - void usage() { std::cout << "Usage: \n" @@ -105,6 +81,20 @@ namespace { "Without options, whiteboard will be started as FCGI application" << std::endl; } + + std::mutex storage_mutex; + + void storage_cleanup(Storage& storage) + { + while(true) { + { + std::lock_guard<std::mutex> lock(storage_mutex); + storage.cleanup(); + } + std::this_thread::sleep_for(std::chrono::minutes(10)); + } + } + } // namespace // the actual main() for testability @@ -125,6 +115,8 @@ int whiteboard(int argc, char* argv[]) } } + std::thread storage_cleanup_thread(storage_cleanup, std::ref(storage)); + Magick::InitializeMagick(NULL); // for qrcode.cpp int result = FCGX_Init(); @@ -148,6 +140,7 @@ int whiteboard(int argc, char* argv[]) while (FCGX_Accept_r(&request) >= 0) { try { + std::lock_guard<std::mutex> lock(storage_mutex); char* method = FCGX_GetParam("REQUEST_METHOD", request.envp); // POST for server actions, changes @@ -171,12 +164,12 @@ int whiteboard(int argc, char* argv[]) if (command == "modify") { std::string id {xml.get<std::string>("request.id")}; std::string data {xml.get<std::string>("request.data")}; - setFileById(data, id); + storage.setDocument(id, data); FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); } else if (command == "getfile") { std::string id {xml.get<std::string>("request.id")}; - std::string filedata {getFileById(id)}; + std::string filedata {storage.getDocument(id)}; if (filedata.size() > 30000000) throw std::runtime_error("File too big"); @@ -192,7 +185,7 @@ int whiteboard(int argc, char* argv[]) //std::cout << "Checksum JS: " << checksum_s << std::endl; //std::cout << "Checksum C++: " << checksum32(filedata) << std::endl; - std::string filedata {getFileById(id)}; + std::string filedata {storage.getDocument(id)}; if (checksum != checksum32(filedata)) { //std::cout << "Sending change..." << std::endl; FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); @@ -235,6 +228,8 @@ int whiteboard(int argc, char* argv[]) } } + storage_cleanup_thread.join(); + return 0; } |