|
|
#include <iostream>
#include <functional>
#include <unordered_map>
#include "crow.h"
#define NULL_STRING std::string("null")
#define EMPTY_STRING std::string("")
#define NO_SUCH_COMMAND std::string("no_such_command")
typedef std::function<crow::json::wvalue(std::string const &arguments, std::string const &session,
void *payload)> handler_func;
struct CommandHandler {
[[nodiscard]] crow::json::wvalue exec(std::string const &arguments, std::string const &session) const {
return func(arguments, session, payload);
}
handler_func func;
void *payload{nullptr};
};
crow::json::wvalue stopHandler(std::string const &arguments, std::string const &session, void *payload);
crow::json::wvalue klingerHandler(std::string const &arguments, std::string const &session, void *payload);
[[maybe_unused]] void decl_me_daddy(int not_an_int) { (void) not_an_int; }
int main() {
crow::SimpleApp app;
// command --> handler
static std::unordered_map<std::string, CommandHandler> const commands{
{"stop", {stopHandler, &app}},
{"klinger", {klingerHandler}}
};
CROW_ROUTE(app, "/")
.methods("POST"_method)
([&app](crow::request const &request) {
auto data = crow::json::load(request.body);
if (!data)
return crow::response(400);
if (!data.count("command"))
return crow::response(400, "malformed request: missing `command` field\n");
std::string command = (data.count("command") ? data["command"].s() : NO_SUCH_COMMAND);
std::string arguments = (data.count("arguments") ? data["arguments"].s() : EMPTY_STRING);
std::string session = (data.count("session") ? data["session"].s() : NULL_STRING);
if (auto itor = commands.find(command); itor != commands.end()) {
auto handler = itor->second;
return crow::response{handler.exec(arguments, session)};
}
crow::json::wvalue response;
return crow::response{"no such command!\n"};
});
app.port(18080).multithreaded().run();
std::cout << "Stopped successfully" << std::endl;
}
crow::json::wvalue klingerHandler(std::string const &arguments, std::string const &session, void *payload) {
(void)payload;
using namespace crow::json;
crow::json::wvalue response(type::Object);
std::vector<wvalue> reply_vec;
std::vector<wvalue> annotation_vec;
wvalue annotation(type::Object);
annotation["type"] = "none";
annotation["extra"] = "";
annotation_vec.push_back(std::move(annotation));
wvalue reply(type::Object);
reply["text"] = "Bonjour!";
reply["annotations"] = std::move(annotation_vec);
reply_vec.push_back(std::move(reply));
response["success"] = true;
response["session"] = NULL_STRING;
response["reply"] = std::move(reply_vec);
return response;
}
crow::json::wvalue stopHandler(std::string const &arguments, std::string const &session, void *payload) {
auto app = (crow::SimpleApp*) payload;
app->stop();
using namespace crow::json;
crow::json::wvalue response(type::Object);
std::vector<wvalue> reply_vec;
std::vector<wvalue> annotation_vec;
wvalue annotation(type::Object);
annotation["type"] = "bold";
annotation["extra"] = "";
annotation_vec.push_back(std::move(annotation));
wvalue reply(type::Object);
reply["text"] = "stopped.";
reply["annotations"] = std::move(annotation_vec);
reply_vec.push_back(std::move(reply));
response["success"] = true;
response["session"] = NULL_STRING;
response["reply"] = std::move(reply_vec);
return response;
}
// curl -d '{"command":"klinger", "b":"12"}' -H "Content-Type: application/json" -X POST http://localhost:18080/
|