mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 16:12:14 +11:00
renamed bank_f->bank_api
This commit is contained in:
parent
85cbff7a62
commit
9be3333efb
3 changed files with 72 additions and 62 deletions
65
include/bank_api.h
Normal file
65
include/bank_api.h
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
#include <drogon/HttpController.h>
|
||||
#include "bank.h"
|
||||
|
||||
using namespace drogon;
|
||||
|
||||
#define req_args const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback
|
||||
#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonCast(V)));
|
||||
#define PASS_HEADER req->getHeader("Password") //temporary
|
||||
#define GEN_BODY \
|
||||
const auto temp_req = req->getJsonObject(); \
|
||||
const auto body = temp_req ? *temp_req : Json::Value();
|
||||
|
||||
namespace v1
|
||||
{
|
||||
class api : public HttpController<api, false>
|
||||
{
|
||||
Bank &bank;
|
||||
|
||||
public:
|
||||
api(Bank &b);
|
||||
void Help(req_args) const;
|
||||
void Ping(req_args) const;
|
||||
void Close(req_args) const;
|
||||
void AddUser(req_args, const std::string &name) const;
|
||||
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
||||
void DelUser(req_args, const std::string &name) const;
|
||||
void AdminDelUser(req_args, const std::string &name) const;
|
||||
void SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const;
|
||||
void ChangePassword(req_args, const std::string &name) const;
|
||||
void Contains(req_args, const std::string &name) const;
|
||||
void GetBal(req_args, const std::string &name) const;
|
||||
void VerifyPassword(req_args, const std::string &name) const;
|
||||
void SetBal(req_args, const std::string &name, uint32_t amount) const;
|
||||
void AdminVerifyPass(req_args);
|
||||
void GetLog(req_args, const std::string &name);
|
||||
|
||||
METHOD_LIST_BEGIN
|
||||
|
||||
//Usage
|
||||
METHOD_ADD(api::GetBal, "/{name}/bal", Get, Options);
|
||||
METHOD_ADD(api::GetLog, "/{name}/log", Get, Options);
|
||||
METHOD_ADD(api::SendFunds, "/{name}/send/{to}?amount={amount}", Post, Options);
|
||||
METHOD_ADD(api::VerifyPassword, "/{name}/pass/verify", Get, Options);
|
||||
|
||||
//Meta Usage
|
||||
METHOD_ADD(api::ChangePassword, "/{name}/pass/change", Patch, Options);
|
||||
METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options);
|
||||
|
||||
//System Usage
|
||||
METHOD_ADD(api::Help, "/help", Get, Options);
|
||||
METHOD_ADD(api::Ping, "/ping", Get, Options);
|
||||
METHOD_ADD(api::Close, "/admin/close", Post, Options);
|
||||
METHOD_ADD(api::Contains, "/contains/{name}", Get, Options);
|
||||
METHOD_ADD(api::AdminVerifyPass, "/admin/verify", Get, Options);
|
||||
|
||||
//User Managment
|
||||
METHOD_ADD(api::AddUser, "/user/{name}", 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::AdminDelUser, "/admin/user/{name}", Delete, Options);
|
||||
|
||||
METHOD_LIST_END
|
||||
};
|
||||
};
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
#pragma once
|
||||
#include <drogon/HttpController.h>
|
||||
#include "bank.h"
|
||||
|
||||
using namespace drogon;
|
||||
|
||||
#define req_args const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback
|
||||
|
||||
class api : public HttpController<api, false>
|
||||
{
|
||||
Bank &bank;
|
||||
|
||||
public:
|
||||
api(Bank &b);
|
||||
void Help(req_args) const;
|
||||
void Ping(req_args) const;
|
||||
void Close(req_args) const;
|
||||
void AddUser(req_args, const std::string &name) const;
|
||||
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
||||
void DelUser(req_args, const std::string &name) const;
|
||||
void AdminDelUser(req_args, const std::string &name) const;
|
||||
void SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const;
|
||||
void ChangePassword(req_args, const std::string &name) const;
|
||||
void Contains(req_args, const std::string &name) const;
|
||||
void GetBal(req_args, const std::string &name) const;
|
||||
void VerifyPassword(req_args, const std::string &name) const;
|
||||
void SetBal(req_args, const std::string &name, uint32_t amount) const;
|
||||
void AdminVerifyPass(req_args);
|
||||
void GetLog(req_args, const std::string &name);
|
||||
|
||||
METHOD_LIST_BEGIN
|
||||
|
||||
//Usage
|
||||
METHOD_ADD(api::GetBal, "/{name}/bal", Get, Options);
|
||||
METHOD_ADD(api::GetLog, "/{name}/log", Get, Options);
|
||||
METHOD_ADD(api::SendFunds, "/{name}/send/{to}?amount={amount}", Post, Options);
|
||||
METHOD_ADD(api::VerifyPassword, "/{name}/pass/verify", Get, Options);
|
||||
|
||||
//Meta Usage
|
||||
METHOD_ADD(api::ChangePassword, "/{name}/pass/change", Patch, Options);
|
||||
METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options);
|
||||
|
||||
//System Usage
|
||||
METHOD_ADD(api::Help, "/help", Get, Options);
|
||||
METHOD_ADD(api::Ping, "/ping", Get, Options);
|
||||
METHOD_ADD(api::Close, "/admin/close", Post, Options);
|
||||
METHOD_ADD(api::Contains, "/contains/{name}", Get, Options);
|
||||
METHOD_ADD(api::AdminVerifyPass, "/admin/verify", Get, Options);
|
||||
|
||||
//User Managment
|
||||
METHOD_ADD(api::AddUser, "/user/{name}", 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::AdminDelUser, "/admin/user/{name}", Delete, Options);
|
||||
|
||||
METHOD_LIST_END
|
||||
};
|
||||
12
main.cpp
12
main.cpp
|
|
@ -3,7 +3,7 @@
|
|||
#include <thread>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "bank_f.h"
|
||||
#include "bank_api.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -63,17 +63,19 @@ int main(int argc, char **argv)
|
|||
{
|
||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||
bank.Save();
|
||||
std::cout << "Saving " << duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() << '\n';
|
||||
std::cout << "Saving " << std::time(0) << '\n';
|
||||
}
|
||||
}).detach();
|
||||
}
|
||||
|
||||
auto API = std::make_shared<api>(bank);
|
||||
//endpoints
|
||||
auto APIv1 = std::make_shared<v1::api>(bank); //v1
|
||||
|
||||
app().registerPostHandlingAdvice(
|
||||
[](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) {
|
||||
resp->addHeader("Access-Control-Allow-Origin", "*");
|
||||
resp->addHeader("Access-Control-Allow-Origin", "*"); //CORS
|
||||
});
|
||||
app().loadConfigFile(config_location).registerController(API).setThreadNum(std::stoul(std::string(argv[3]))).run();
|
||||
app().loadConfigFile(config_location).registerController(APIv1).setThreadNum(std::stoul(std::string(argv[3]))).run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue