mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 08:32:13 +11:00
🐎🔥 made BankAPI static
This commit is contained in:
parent
4086a1d324
commit
0cae801ba3
3 changed files with 48 additions and 52 deletions
|
|
@ -8,34 +8,34 @@ using namespace drogon;
|
||||||
|
|
||||||
#define req_args const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback
|
#define req_args const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback
|
||||||
|
|
||||||
class api : public HttpController<api, false>
|
class api : public HttpController<api>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void JsonCpp(req_args) const;
|
static void JsonCpp(req_args);
|
||||||
void Json(req_args) const;
|
static void Json(req_args);
|
||||||
|
|
||||||
#if API_VERSION >= 1
|
#if API_VERSION >= 1
|
||||||
void GetBal(req_args, const std::string &name) const;
|
static void GetBal(req_args, const std::string &name);
|
||||||
void GetLogs(req_args);
|
static void GetLogs(req_args);
|
||||||
void SendFunds(req_args) const;
|
static void SendFunds(req_args);
|
||||||
void VerifyPassword(req_args) const;
|
static void VerifyPassword(req_args);
|
||||||
|
|
||||||
void ChangePassword(req_args) const;
|
static void ChangePassword(req_args);
|
||||||
void AdminChangePassword(req_args) const;
|
static void AdminChangePassword(req_args);
|
||||||
void SetBal(req_args) const;
|
static void SetBal(req_args);
|
||||||
void ImpactBal(req_args) const;
|
static void ImpactBal(req_args);
|
||||||
|
|
||||||
void Help(req_args) const;
|
static void Help(req_args);
|
||||||
void Close(req_args) const;
|
static void Close(req_args);
|
||||||
void Contains(req_args, const std::string &name) const;
|
static void Contains(req_args, const std::string &name);
|
||||||
void AdminVerifyAccount(req_args) const;
|
static void AdminVerifyAccount(req_args);
|
||||||
void PruneUsers(req_args) const;
|
static void PruneUsers(req_args);
|
||||||
void ApiProperties(req_args) const;
|
static void ApiProperties(req_args);
|
||||||
|
|
||||||
void AddUser(req_args) const;
|
static void AddUser(req_args);
|
||||||
void AdminAddUser(req_args) const;
|
static void AdminAddUser(req_args);
|
||||||
void DelSelf(req_args) const;
|
static void DelSelf(req_args);
|
||||||
void AdminDelUser(req_args) const;
|
static void AdminDelUser(req_args);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
24
main.cpp
24
main.cpp
|
|
@ -17,12 +17,10 @@
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
|
||||||
static Bank bank;
|
|
||||||
|
|
||||||
void SaveSig(int s)
|
void SaveSig(int s)
|
||||||
{
|
{
|
||||||
std::cout << "\nSaving on close...\n"
|
std::cout << "\nSaving on close...\n"
|
||||||
<< bank.Save();
|
<< Bank::Save();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,12 +71,12 @@ int main(int argc, char **argv)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Loading users from users.json
|
//Loading users from users.json
|
||||||
bank.Load();
|
Bank::Load();
|
||||||
size_t num_of_logs = bank.NumOfLogs();
|
size_t num_of_logs = Bank::NumOfLogs();
|
||||||
size_t num_of_users = bank.NumOfUsers();
|
size_t num_of_users = Bank::NumOfUsers();
|
||||||
std::cout << "\n\nLoaded " << num_of_users << " Users ~" << (float)(sizeof(User) * num_of_users) / 1048576 << "Mb"
|
std::cout << "\n\nLoaded " << num_of_users << " Users ~" << (float)(sizeof(User) * num_of_users) / 1048576 << "Mb"
|
||||||
<< "\nLoaded " << num_of_logs << " Logs ~" << (float)(num_of_logs * (90 + 80 + (max_name_size * 2))) / 1048576 << "Mb" //90:string representation(heap), sizeof(Transaction), max_name_size*2:filled to&from(heap)
|
<< "\nLoaded " << num_of_logs << " Logs ~" << (float)(num_of_logs * (90 + 80 + (max_name_size * 2))) / 1048576 << "Mb" //90:string representation(heap), sizeof(Transaction), max_name_size*2:filled to&from(heap)
|
||||||
<< "\nLoaded " << bank.SumBal() << " CSH"
|
<< "\nLoaded " << Bank::SumBal() << " CSH"
|
||||||
<< std::endl; //flushing before EventLoop
|
<< std::endl; //flushing before EventLoop
|
||||||
|
|
||||||
//Sig handling
|
//Sig handling
|
||||||
|
|
@ -91,7 +89,7 @@ int main(int argc, char **argv)
|
||||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||||
|
|
||||||
//Admin account
|
//Admin account
|
||||||
bank.admin_account = argv[1];
|
Bank::admin_account = argv[1];
|
||||||
|
|
||||||
//Auto Saving
|
//Auto Saving
|
||||||
const unsigned long saving_freq = std::stoul(std::string(argv[2]));
|
const unsigned long saving_freq = std::stoul(std::string(argv[2]));
|
||||||
|
|
@ -102,16 +100,15 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||||
std::cout << "Saving " << std::time(0) << "...\n"
|
std::cout << "Saving " << std::time(0) << "...\n"
|
||||||
<< bank.Save();
|
<< Bank::Save();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
} //destroying setup variables
|
} //destroying setup variables
|
||||||
static auto API = std::make_shared<api>(bank);
|
static auto user_filter_default = std::make_shared<UserFilter<true, false>>();
|
||||||
static auto user_filter_default = std::make_shared<UserFilter<true, false>>(bank);
|
static auto user_filter_sparse = std::make_shared<UserFilter<false, false>>();
|
||||||
static auto user_filter_sparse = std::make_shared<UserFilter<false, false>>(bank);
|
static auto admin_filter = std::make_shared<UserFilter<false, true>>();
|
||||||
static auto admin_filter = std::make_shared<UserFilter<false, true>>(bank);
|
|
||||||
static auto json_resp_and_req_filter = std::make_shared<JsonFilter<true>>();
|
static auto json_resp_and_req_filter = std::make_shared<JsonFilter<true>>();
|
||||||
static auto json_resp_filter = std::make_shared<JsonFilter<false>>();
|
static auto json_resp_filter = std::make_shared<JsonFilter<false>>();
|
||||||
|
|
||||||
|
|
@ -122,7 +119,6 @@ int main(int argc, char **argv)
|
||||||
.registerFilter(admin_filter)
|
.registerFilter(admin_filter)
|
||||||
.registerFilter(json_resp_and_req_filter)
|
.registerFilter(json_resp_and_req_filter)
|
||||||
.registerFilter(json_resp_filter)
|
.registerFilter(json_resp_filter)
|
||||||
.registerController(API)
|
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
.setThreadNum(get_nprocs())
|
.setThreadNum(get_nprocs())
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ static thread_local ondemand::parser parser;
|
||||||
#if API_VERSION >= 1
|
#if API_VERSION >= 1
|
||||||
|
|
||||||
//Usage
|
//Usage
|
||||||
void api::GetBal(req_args, const std::string &name) const
|
void api::GetBal(req_args, const std::string &name)
|
||||||
{
|
{
|
||||||
RESPONSE_PARSE(Bank::GetBal(name));
|
RESPONSE_PARSE(Bank::GetBal(name));
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +64,7 @@ void api::GetLogs(req_args)
|
||||||
callback(resp);
|
callback(resp);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void api::SendFunds(req_args) const
|
void api::SendFunds(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -88,10 +88,10 @@ void api::SendFunds(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::VerifyPassword(req_args) const { RESPOND_TRUE; }
|
void api::VerifyPassword(req_args) { RESPOND_TRUE; }
|
||||||
|
|
||||||
//Meta Usage
|
//Meta Usage
|
||||||
void api::ChangePassword(req_args) const
|
void api::ChangePassword(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -115,7 +115,7 @@ void api::ChangePassword(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::AdminChangePassword(req_args) const
|
void api::AdminChangePassword(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -148,7 +148,7 @@ void api::AdminChangePassword(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::SetBal(req_args) const
|
void api::SetBal(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -172,7 +172,7 @@ void api::SetBal(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::ImpactBal(req_args) const
|
void api::ImpactBal(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -198,19 +198,19 @@ void api::ImpactBal(req_args) const
|
||||||
}
|
}
|
||||||
|
|
||||||
//System Usage
|
//System Usage
|
||||||
void api::Help(req_args) const
|
void api::Help(req_args)
|
||||||
{
|
{
|
||||||
static thread_local auto resp = HttpResponse::newRedirectionResponse("https://github.com/EntireTwix/CCash/blob/README.md", k301MovedPermanently);
|
static thread_local auto resp = HttpResponse::newRedirectionResponse("https://github.com/EntireTwix/CCash/blob/README.md", k301MovedPermanently);
|
||||||
CACHE_FOREVER;
|
CACHE_FOREVER;
|
||||||
callback(resp);
|
callback(resp);
|
||||||
}
|
}
|
||||||
void api::Close(req_args) const
|
void api::Close(req_args)
|
||||||
{
|
{
|
||||||
Bank::Save();
|
Bank::Save();
|
||||||
RESPOND_TRUE; //filter handles admin creds
|
RESPOND_TRUE; //filter handles admin creds
|
||||||
app().quit();
|
app().quit();
|
||||||
}
|
}
|
||||||
void api::Contains(req_args, const std::string &name) const
|
void api::Contains(req_args, const std::string &name)
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (Bank::Contains(name))
|
if (Bank::Contains(name))
|
||||||
|
|
@ -223,11 +223,11 @@ void api::Contains(req_args, const std::string &name) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::AdminVerifyAccount(req_args) const
|
void api::AdminVerifyAccount(req_args)
|
||||||
{
|
{
|
||||||
RESPOND_TRUE; //filter handles admin creds
|
RESPOND_TRUE; //filter handles admin creds
|
||||||
}
|
}
|
||||||
void api::ApiProperties(req_args) const
|
void api::ApiProperties(req_args)
|
||||||
{
|
{
|
||||||
std::string info = "{\"version\":" + std::to_string(API_VERSION) + ",\"max_log\":" + std::to_string(MAX_LOG_SIZE);
|
std::string info = "{\"version\":" + std::to_string(API_VERSION) + ",\"max_log\":" + std::to_string(MAX_LOG_SIZE);
|
||||||
if constexpr (RETURN_ON_DEL)
|
if constexpr (RETURN_ON_DEL)
|
||||||
|
|
@ -243,7 +243,7 @@ void api::ApiProperties(req_args) const
|
||||||
CACHE_FOREVER;
|
CACHE_FOREVER;
|
||||||
callback(resp);
|
callback(resp);
|
||||||
}
|
}
|
||||||
void api::PruneUsers(req_args) const
|
void api::PruneUsers(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -279,7 +279,7 @@ void api::PruneUsers(req_args) const
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
void api::AddUser(req_args) const
|
void api::AddUser(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -304,7 +304,7 @@ void api::AddUser(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::AdminAddUser(req_args) const
|
void api::AdminAddUser(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
@ -330,12 +330,12 @@ void api::AdminAddUser(req_args) const
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::DelSelf(req_args) const
|
void api::DelSelf(req_args)
|
||||||
{
|
{
|
||||||
Bank::DelSelf(NAME_PARAM);
|
Bank::DelSelf(NAME_PARAM);
|
||||||
RESPOND_TRUE;
|
RESPOND_TRUE;
|
||||||
}
|
}
|
||||||
void api::AdminDelUser(req_args) const
|
void api::AdminDelUser(req_args)
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue