From 1efd8ab1b4d4a2b1c8236aee33f51d960964c20b Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Wed, 26 Aug 2020 12:00:21 +0200 Subject: fixes (i hope) --- src/main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 18 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 59fd6a1..8e0b442 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,27 +19,37 @@ int main() { crow::SimpleApp app; - auto createPlainDescriptionFromText = [] (std::string const &text) -> std::queue* { + auto createPlainDescriptionFromText = [](std::string const &text) -> std::queue * { auto reply_queue = new std::queue; reply_queue->emplace(Response::create_text(text)); return reply_queue; }; // command --> handler - std::unordered_map commands{ - {"wiki", {.func = Handler::wikiHandler, .description = createPlainDescriptionFromText("Sends you to Wikipedia!")}}, - {"stop", {.func = Handler::stopHandler, .description = createPlainDescriptionFromText("Stops the bot"), .payload = &app}}, - {"mensa", {.func = Handler::mensaHandler, .description = createPlainDescriptionFromText("{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")}}, - {"klinger", {.func = Handler::klingerHandler, .description = createPlainDescriptionFromText("Greats in french. Bonjour!")}}, - {"relation", {.func = Handler::relationShipHandler, .description = createPlainDescriptionFromText("[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N.")}}, - }; - - commands.insert({"help", - {.func = Handler::helpHandler, .description = createPlainDescriptionFromText("This is my holy manual."), .payload = &commands}}); + std::vector commands; + commands.emplace_back( + Handler::CommandHandler{"wiki", Handler::wikiHandler, createPlainDescriptionFromText( + "Sends you to Wikipedia!")}); + commands.emplace_back( + Handler::CommandHandler{"stop", Handler::stopHandler, createPlainDescriptionFromText( + "Stops the bot"), &app}); + commands.emplace_back( + Handler::CommandHandler{"mensa", Handler::mensaHandler, createPlainDescriptionFromText( + "{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")}); + commands.emplace_back( + Handler::CommandHandler{"klinger", Handler::klingerHandler, createPlainDescriptionFromText( + "Greats in french. Bonjour!")}); + commands.emplace_back( + Handler::CommandHandler{"relation", Handler::relationShipHandler, createPlainDescriptionFromText( + "[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N.")}); + commands.emplace_back(Handler::CommandHandler{"help", Handler::helpHandler, createPlainDescriptionFromText( + "This is my holy manual."), &commands}); CROW_ROUTE(app, "/") .methods("POST"_method) - ([&commands](crow::request const &request) { + ([&commands]( + crow::request const &request + ) { auto data = crow::json::load(request.body); if (!data) @@ -53,15 +63,28 @@ int main() { std::string arguments = (data.count("arguments") ? data["arguments"].s() : std::string("")); std::string session = (data.count("session") ? data["session"].s() : std::string("null")); - if (auto itor = commands.find(command); itor != commands.end()) { - auto handler = itor->second; - return crow::response{handler.exec(arguments, session)}; + auto itor = std::find_if( + commands.begin(), + commands.end(), + [command](Handler::CommandHandler const &handler) { + return handler.command == command; + }); + if (itor != commands.end()) { + return crow::response{itor->exec(arguments, session)}; } - return crow::response{Response::simple_response("No such command!", "null", false)}; + return crow::response{ + Response::simple_response("No such command!", "null", false)}; }); - app.port(18080).multithreaded().run(); + app.port(18080). + + multithreaded() + + . + + run(); - std::cout << "Stopped successfully" << std::endl; + std::cout << "Stopped successfully" << + std::endl; } -- cgit v1.2.3-54-g00ecf