mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🎨 changed how API versions are handled
This commit is contained in:
parent
539df650ea
commit
d9986db962
4 changed files with 158 additions and 163 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <drogon/HttpTypes.h>
|
#include <drogon/HttpTypes.h>
|
||||||
#include "error_responses.hpp"
|
#include "error_responses.hpp" //temporary
|
||||||
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,57 +7,56 @@ 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
|
||||||
|
|
||||||
namespace v1
|
#if V1_API
|
||||||
|
class api : public HttpController<api, false>
|
||||||
{
|
{
|
||||||
class api : public HttpController<api, false>
|
Bank &bank;
|
||||||
{
|
|
||||||
Bank &bank;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
api(Bank &b);
|
api(Bank &b);
|
||||||
void GetBal(req_args, const std::string &name) const;
|
void GetBal(req_args, const std::string &name) const;
|
||||||
void GetLog(req_args);
|
void GetLog(req_args);
|
||||||
void SendFunds(req_args) const;
|
void SendFunds(req_args) const;
|
||||||
void VerifyPassword(req_args) const;
|
void VerifyPassword(req_args) const;
|
||||||
|
|
||||||
void ChangePassword(req_args) const;
|
void ChangePassword(req_args) const;
|
||||||
|
|
||||||
void Help(req_args) const;
|
void Help(req_args) const;
|
||||||
void Ping(req_args) const;
|
void Ping(req_args) const;
|
||||||
void Close(req_args) const;
|
void Close(req_args) const;
|
||||||
void AddUser(req_args, const std::string &name) const;
|
void AddUser(req_args, const std::string &name) const;
|
||||||
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
||||||
void DelUser(req_args, const std::string &name) const;
|
void DelUser(req_args, const std::string &name) const;
|
||||||
void AdminDelUser(req_args, const std::string &name) const;
|
void AdminDelUser(req_args, const std::string &name) const;
|
||||||
void Contains(req_args, const std::string &name) const;
|
void Contains(req_args, const std::string &name) const;
|
||||||
void SetBal(req_args, const std::string &name, uint32_t amount) const;
|
void SetBal(req_args, const std::string &name, uint32_t amount) const;
|
||||||
void AdminVerifyPass(req_args);
|
void AdminVerifyPass(req_args);
|
||||||
|
|
||||||
METHOD_LIST_BEGIN
|
METHOD_LIST_BEGIN
|
||||||
|
|
||||||
//Usage
|
//Usage
|
||||||
METHOD_ADD(api::GetBal, "/user/bal?name={name}", Get, Options); //done
|
METHOD_ADD(api::GetBal, "/v1/user/bal?name={name}", Get, Options); //done
|
||||||
METHOD_ADD(api::GetLog, "/user/log", Get, Options, "UserFilter"); //snapshot not implemented
|
METHOD_ADD(api::GetLog, "/v1/user/log", Get, Options, "UserFilter"); //snapshot not implemented
|
||||||
METHOD_ADD(api::SendFunds, "/user/transfer", Post, Options, "UserFilter"); //responses incomplete
|
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "UserFilter"); //responses incomplete
|
||||||
METHOD_ADD(api::VerifyPassword, "/user/verify_password", Get, Options, "UserFilter"); //done
|
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter"); //done
|
||||||
|
|
||||||
//Meta Usage
|
//Meta Usage
|
||||||
METHOD_ADD(api::ChangePassword, "/user/change_password", Patch, Options, "UserFilter"); //done
|
METHOD_ADD(api::ChangePassword, "/v1/user/change_password", Patch, Options, "UserFilter"); //done
|
||||||
METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options);
|
METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options);
|
||||||
|
|
||||||
//System Usage
|
//System Usage
|
||||||
METHOD_ADD(api::Help, "/help", Get, Options);
|
METHOD_ADD(api::Help, "/help", Get, Options);
|
||||||
METHOD_ADD(api::Ping, "/ping", Get, Options);
|
METHOD_ADD(api::Ping, "/ping", Get, Options);
|
||||||
METHOD_ADD(api::Close, "/admin/close", Post, Options);
|
METHOD_ADD(api::Close, "/admin/close", Post, Options);
|
||||||
METHOD_ADD(api::Contains, "/contains/{name}", Get, Options);
|
METHOD_ADD(api::Contains, "/contains/{name}", Get, Options);
|
||||||
METHOD_ADD(api::AdminVerifyPass, "/admin/verify", Get, Options);
|
METHOD_ADD(api::AdminVerifyPass, "/admin/verify", Get, Options);
|
||||||
|
|
||||||
//User Managment
|
//User Managment
|
||||||
METHOD_ADD(api::AddUser, "/user/{name}", Post, Options);
|
METHOD_ADD(api::AddUser, "/user/{name}", Post, Options);
|
||||||
METHOD_ADD(api::AdminAddUser, "/admin/user/{name}?init_bal={init_bal}", Post, Options);
|
METHOD_ADD(api::AdminAddUser, "/admin/user/{name}?init_bal={init_bal}", Post, Options);
|
||||||
METHOD_ADD(api::DelUser, "/user/{name}", Delete, Options);
|
METHOD_ADD(api::DelUser, "/user/{name}", Delete, Options);
|
||||||
METHOD_ADD(api::AdminDelUser, "/admin/user/{name}", Delete, Options);
|
METHOD_ADD(api::AdminDelUser, "/admin/user/{name}", Delete, Options);
|
||||||
|
|
||||||
METHOD_LIST_END
|
METHOD_LIST_END
|
||||||
};
|
};
|
||||||
};
|
#endif
|
||||||
8
main.cpp
8
main.cpp
|
|
@ -84,9 +84,7 @@ int main(int argc, char **argv)
|
||||||
}).detach();
|
}).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if V1_API
|
auto API = std::make_shared<api>(bank);
|
||||||
auto APIv1 = std::make_shared<v1::api>(bank); //v1
|
|
||||||
#endif
|
|
||||||
auto user_filter = std::make_shared<UserFilter>(bank);
|
auto user_filter = std::make_shared<UserFilter>(bank);
|
||||||
auto admin_filter = std::make_shared<AdminFilter>(bank);
|
auto admin_filter = std::make_shared<AdminFilter>(bank);
|
||||||
|
|
||||||
|
|
@ -98,9 +96,7 @@ int main(int argc, char **argv)
|
||||||
.loadConfigFile(config_location)
|
.loadConfigFile(config_location)
|
||||||
.registerFilter(user_filter)
|
.registerFilter(user_filter)
|
||||||
.registerFilter(admin_filter)
|
.registerFilter(admin_filter)
|
||||||
#if V1_API
|
.registerController(API)
|
||||||
.registerController(APIv1)
|
|
||||||
#endif
|
|
||||||
.setThreadNum(get_nprocs())
|
.setThreadNum(get_nprocs())
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
|
|
|
||||||
224
src/bank_api.cpp
224
src/bank_api.cpp
|
|
@ -8,8 +8,10 @@
|
||||||
const auto body = temp_req ? *temp_req : Json::Value();
|
const auto body = temp_req ? *temp_req : Json::Value();
|
||||||
|
|
||||||
#define RESPONSE_PARSE(R) \
|
#define RESPONSE_PARSE(R) \
|
||||||
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(std::move(R.second))); \
|
\
|
||||||
resp->setStatusCode(R.first); \
|
auto r = R; \
|
||||||
|
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(std::move(r.second))); \
|
||||||
|
resp->setStatusCode(r.first); \
|
||||||
callback(resp);
|
callback(resp);
|
||||||
|
|
||||||
#define RESPOND_TRUE \
|
#define RESPOND_TRUE \
|
||||||
|
|
@ -20,125 +22,123 @@
|
||||||
|
|
||||||
#define NAME_PARAM req->getBody()
|
#define NAME_PARAM req->getBody()
|
||||||
|
|
||||||
//#define NAME_CHECK(suc) BankResponse b = (name == NAME_PARAM ? suc : BankResponse(k400BadRequest, "Requests name must match Auth's name"));
|
template <typename T>
|
||||||
|
constexpr Json::Value JsonCast(T &&val)
|
||||||
namespace v1
|
|
||||||
{
|
{
|
||||||
template <typename T>
|
if constexpr (std::is_same_v<T, int_fast8_t>)
|
||||||
constexpr Json::Value JsonCast(T &&val)
|
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same_v<T, int_fast8_t>)
|
return (int)val; //becuase of json lib interpreting 67 as 'A' for example
|
||||||
{
|
|
||||||
return (int)val; //becuase of json lib interpreting 67 as 'A' for example
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<T, uint64_t>)
|
|
||||||
{
|
|
||||||
return (Json::Int64)val;
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<T, int64_t>)
|
|
||||||
{
|
|
||||||
return (Json::Int64)val;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, uint64_t>)
|
||||||
|
{
|
||||||
|
return (Json::Int64)val;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, int64_t>)
|
||||||
|
{
|
||||||
|
return (Json::Int64)val;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
api::api(Bank &b) : bank(b) {}
|
#if V1_API
|
||||||
|
api::api(Bank &b) : bank(b)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void api::GetBal(req_args, const std::string &name) const
|
void api::GetBal(req_args, const std::string &name) const
|
||||||
|
{
|
||||||
|
RESPONSE_PARSE(bank.GetBal(name));
|
||||||
|
}
|
||||||
|
void api::GetLog(req_args)
|
||||||
|
{
|
||||||
|
if constexpr (max_log_size > 0)
|
||||||
{
|
{
|
||||||
RESPONSE_PARSE(bank.GetBal(name));
|
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM.data()));
|
||||||
}
|
}
|
||||||
void api::GetLog(req_args)
|
else
|
||||||
{
|
{
|
||||||
if constexpr (max_log_size > 0)
|
auto resp = HttpResponse::newHttpJsonResponse("Logs are Disabled");
|
||||||
{
|
resp->setStatusCode(k404NotFound);
|
||||||
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM.data()));
|
resp->setExpiredTime(0); //cached forever
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto resp = HttpResponse::newHttpJsonResponse("Logs are Disabled");
|
|
||||||
resp->setStatusCode(k404NotFound);
|
|
||||||
resp->setExpiredTime(0); //cached forever
|
|
||||||
callback(resp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void api::SendFunds(req_args) const
|
|
||||||
{
|
|
||||||
GEN_BODY
|
|
||||||
RESPONSE_PARSE(bank.SendFunds(NAME_PARAM.data(), body["to"].asCString(), body["amount"].asUInt()));
|
|
||||||
}
|
|
||||||
void api::VerifyPassword(req_args) const
|
|
||||||
{
|
|
||||||
RESPOND_TRUE
|
|
||||||
}
|
|
||||||
|
|
||||||
void api::ChangePassword(req_args) const
|
|
||||||
{
|
|
||||||
GEN_BODY
|
|
||||||
bank.ChangePassword(NAME_PARAM.data(), std::move(body["new_pass"].asCString())); //may make asString()
|
|
||||||
RESPOND_TRUE
|
|
||||||
}
|
|
||||||
|
|
||||||
void api::Help(req_args) const
|
|
||||||
{
|
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
|
||||||
resp->setBody(""); //will be filled in with docs
|
|
||||||
resp->setExpiredTime(0);
|
|
||||||
callback(resp);
|
callback(resp);
|
||||||
}
|
}
|
||||||
void api::Ping(req_args) const
|
}
|
||||||
{
|
void api::SendFunds(req_args) const
|
||||||
auto resp = HttpResponse::newHttpResponse();
|
{
|
||||||
resp->setBody("pong");
|
GEN_BODY
|
||||||
resp->setExpiredTime(0);
|
RESPONSE_PARSE(bank.SendFunds(NAME_PARAM.data(), body["to"].asCString(), body["amount"].asUInt()));
|
||||||
callback(resp);
|
}
|
||||||
}
|
void api::VerifyPassword(req_args) const
|
||||||
void api::Close(req_args) const
|
{
|
||||||
{
|
RESPOND_TRUE
|
||||||
bool res;
|
}
|
||||||
if (PASS_HEADER == bank.admin_pass)
|
|
||||||
{
|
|
||||||
bank.Save();
|
|
||||||
|
|
||||||
res = true;
|
void api::ChangePassword(req_args) const
|
||||||
app().quit();
|
{
|
||||||
}
|
GEN_BODY
|
||||||
else
|
bank.ChangePassword(NAME_PARAM.data(), std::move(body["new_pass"].asCString())); //may make asString()
|
||||||
{
|
RESPOND_TRUE
|
||||||
res = false;
|
}
|
||||||
}
|
|
||||||
JSON(res);
|
|
||||||
}
|
|
||||||
void api::AddUser(req_args, const std::string &name) const
|
|
||||||
{
|
|
||||||
JSON(bank.AddUser(std::move(name), PASS_HEADER));
|
|
||||||
}
|
|
||||||
void api::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const
|
|
||||||
{
|
|
||||||
JSON(bank.AdminAddUser(PASS_HEADER, std::move(name), init_bal, std::string(req->getBody())));
|
|
||||||
}
|
|
||||||
void api::DelUser(req_args, const std::string &name) const
|
|
||||||
{
|
|
||||||
JSON(bank.DelUser(name, PASS_HEADER));
|
|
||||||
}
|
|
||||||
void api::AdminDelUser(req_args, const std::string &name) const
|
|
||||||
{
|
|
||||||
JSON(bank.AdminDelUser(name, PASS_HEADER));
|
|
||||||
}
|
|
||||||
void api::Contains(req_args, const std::string &name) const
|
|
||||||
{
|
|
||||||
JSON(bank.Contains(name));
|
|
||||||
}
|
|
||||||
void api::SetBal(req_args, const std::string &name, uint32_t amount) const
|
|
||||||
{
|
|
||||||
JSON(bank.SetBal(name, PASS_HEADER, amount));
|
|
||||||
}
|
|
||||||
void api::AdminVerifyPass(req_args)
|
|
||||||
{
|
|
||||||
JSON(bank.AdminVerifyPass(PASS_HEADER));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
void api::Help(req_args) const
|
||||||
|
{
|
||||||
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
resp->setBody(""); //will be filled in with docs
|
||||||
|
resp->setExpiredTime(0);
|
||||||
|
callback(resp);
|
||||||
|
}
|
||||||
|
void api::Ping(req_args) const
|
||||||
|
{
|
||||||
|
auto resp = HttpResponse::newHttpResponse();
|
||||||
|
resp->setBody("pong");
|
||||||
|
resp->setExpiredTime(0);
|
||||||
|
callback(resp);
|
||||||
|
}
|
||||||
|
void api::Close(req_args) const
|
||||||
|
{
|
||||||
|
bool res;
|
||||||
|
if (PASS_HEADER == bank.admin_pass)
|
||||||
|
{
|
||||||
|
bank.Save();
|
||||||
|
|
||||||
|
res = true;
|
||||||
|
app().quit();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = false;
|
||||||
|
}
|
||||||
|
JSON(res);
|
||||||
|
}
|
||||||
|
void api::AddUser(req_args, const std::string &name) const
|
||||||
|
{
|
||||||
|
JSON(bank.AddUser(std::move(name), PASS_HEADER));
|
||||||
|
}
|
||||||
|
void api::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const
|
||||||
|
{
|
||||||
|
JSON(bank.AdminAddUser(PASS_HEADER, std::move(name), init_bal, std::string(req->getBody())));
|
||||||
|
}
|
||||||
|
void api::DelUser(req_args, const std::string &name) const
|
||||||
|
{
|
||||||
|
JSON(bank.DelUser(name, PASS_HEADER));
|
||||||
|
}
|
||||||
|
void api::AdminDelUser(req_args, const std::string &name) const
|
||||||
|
{
|
||||||
|
JSON(bank.AdminDelUser(name, PASS_HEADER));
|
||||||
|
}
|
||||||
|
void api::Contains(req_args, const std::string &name) const
|
||||||
|
{
|
||||||
|
JSON(bank.Contains(name));
|
||||||
|
}
|
||||||
|
void api::SetBal(req_args, const std::string &name, uint32_t amount) const
|
||||||
|
{
|
||||||
|
JSON(bank.SetBal(name, PASS_HEADER, amount));
|
||||||
|
}
|
||||||
|
void api::AdminVerifyPass(req_args)
|
||||||
|
{
|
||||||
|
JSON(bank.AdminVerifyPass(PASS_HEADER));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Loading…
Reference in a new issue