got web framework working

This commit is contained in:
EntireTwix 2021-04-04 21:04:05 -07:00
parent 01bcd2d947
commit 769c0762f3
3 changed files with 25 additions and 10 deletions

View file

@ -15,6 +15,7 @@ add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PUBLIC third_party) target_include_directories(${PROJECT_NAME} PUBLIC third_party)
target_include_directories(${PROJECT_NAME} PUBLIC third_party/drogon/lib/inc)
add_subdirectory(third_party/drogon) add_subdirectory(third_party/drogon)
target_link_libraries(${PROJECT_NAME} PRIVATE drogon) target_link_libraries(${PROJECT_NAME} PRIVATE drogon)

View file

@ -16,6 +16,12 @@ private:
std::mutex> std::mutex>
users; users;
/**
* @brief any function that WRITES to users in a way that CHANGES its SIZE OR takes MORE THEN ONE OPERATION must share lock this lock
* as to be stopped when saving, as saving data must not be corrupted. one operation writes that done modify size aswell as reads are
* permitted while saving so they do not have to share this lock
*
*/
std::shared_mutex save_lock; std::shared_mutex save_lock;
public: public:
@ -54,7 +60,7 @@ public:
{ {
//if A exists, A can afford it, and A's password matches //if A exists, A can afford it, and A's password matches
bool state = false; bool state = false;
std::shared_lock<std::shared_mutex> lock{save_lock}; //because SendFunds is non-atomic, it requires 3 locking operations std::shared_lock<std::shared_mutex> lock{save_lock}; //because SendFunds requires 3 locking operations
users.modify_if(a_name, [&state, amount, &attempt](User &a) { users.modify_if(a_name, [&state, amount, &attempt](User &a) {
if (state = (a.balance >= amount) && (a.password == attempt), state) if (state = (a.balance >= amount) && (a.password == attempt), state)
{ {

View file

@ -3,7 +3,9 @@
#include <thread> #include <thread>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "bank.hpp" #include "bank_f.hpp"
using namespace drogon;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -25,14 +27,20 @@ int main(int argc, char **argv)
Bank.admin_pass = argv[1]; Bank.admin_pass = argv[1];
//Auto Saving //Auto Saving
volatile bool saving_flag = true; const unsigned long saving_freq = std::stoul(argv[2]);
std::thread([&argv, &saving_flag]() { if (saving_freq) //if saving frequency is 0 then auto saving is turned off
while (saving_flag) {
{ std::thread([&argv, saving_freq]() {
std::this_thread::sleep_for(std::chrono::minutes(std::stoi(argv[2]))); while (1)
Bank.Save(); {
} std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
}).detach(); Bank.Save();
}
}).detach();
}
auto API = std::make_shared<BankFrontend>();
app().addListener("0.0.0.0", 80).registerController(API).run();
return 0; return 0;
} }