From d7b1fed424a6c9cfc9bcf53670ee35ec38a6111b Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 23 Nov 2022 20:44:56 +0100 Subject: [PATCH] Change routing --- server/Routes.h | 49 ++++++++----------------------------------------- server/cOMS | 2 +- server/main.cpp | 17 ++++++++--------- 3 files changed, 17 insertions(+), 51 deletions(-) diff --git a/server/Routes.h b/server/Routes.h index 58b24c2..859fafe 100755 --- a/server/Routes.h +++ b/server/Routes.h @@ -12,54 +12,21 @@ #include #include -#include -#include #include "Controller/ApiController.h" #include "Controller/InstallController.h" -#include "cOMS/Stdlib/HashTable.h" +#include "cOMS/Router/Router.h" -typedef void (*Fptr)(int, char **); - -Stdlib::HashTable::ht *generate_routes() +Router generate_routes() { - Stdlib::HashTable::ht *table = Stdlib::HashTable::create_table(4, true); - if (table == NULL) { - return NULL; - } + Router router = Router::create_router(4); - Stdlib::HashTable::set_entry(table, "^.*?\\-h *.*$", (void *) &Controller::ApiController::printHelp); - Stdlib::HashTable::set_entry(table, "^.*?\\-v *.*$", (void *) &Controller::ApiController::printVersion); - Stdlib::HashTable::set_entry(table, "^.*?\\-r *.*$", (void *) &Controller::ApiController::checkResources); + router.set("^.*?\\-h *.*$", (void *) &Controller::ApiController::printHelp); + router.set("^.*?\\-v *.*$", (void *) &Controller::ApiController::printVersion); + router.set("^.*?\\-r *.*$", (void *) &Controller::ApiController::checkResources); + router.set("^.*?\\-\\-install *.*$", (void *) &Controller::InstallController::installApplication); - Stdlib::HashTable::set_entry(table, "^.*?\\-\\-install *.*$", (void *) &Controller::InstallController::installApplication); - - return table; -} - -Fptr match_route(Stdlib::HashTable::ht *routes, char *uri) -{ - Fptr ptr = NULL; - Stdlib::HashTable::it itr = Stdlib::HashTable::table_iterator(routes); - - std::regex regex; - std::cmatch match; - - while (Stdlib::HashTable::next(&itr)) { - regex = std::regex(itr.key); - - bool status = std::regex_search(uri, match, regex); - if (status) { - ptr = (Fptr) itr.value; - } - } - - // No endpoint found - if (ptr == NULL) { - ptr = &Controller::ApiController::printHelp; - } - - return ptr; + return router; } #endif \ No newline at end of file diff --git a/server/cOMS b/server/cOMS index 382ef60..a6216bd 160000 --- a/server/cOMS +++ b/server/cOMS @@ -1 +1 @@ -Subproject commit 382ef60919c33d19950c105a5938ecd631ba1499 +Subproject commit a6216bdb586725637e85d5b57bca4a02b388d678 diff --git a/server/main.cpp b/server/main.cpp index 2719d0a..0d6766a 100755 --- a/server/main.cpp +++ b/server/main.cpp @@ -14,7 +14,7 @@ #include "cOMS/Utils/ApplicationUtils.h" #include "cOMS/DataStorage/Database/Connection/ConnectionAbstract.h" #include "cOMS/Utils/Parser/Json.h" -#include "cOMS/Stdlib/HashTable.h" +#include "cOMS/Router/Router.h" #include "Routes.h" @@ -85,12 +85,13 @@ int main(int argc, char **argv) /* --------------- Handle request --------------- */ // Handle routes - Stdlib::HashTable::ht *routes = generate_routes(); - if (routes == NULL) { - return -1; - } + Router router = generate_routes(); + Fptr ptr = Router::match_route(&router, arg); - Fptr ptr = match_route(routes, (char *) arg); + // No endpoint found + if (ptr == NULL) { + ptr = &Controller::ApiController::printHelp; + } // Dispatch found endpoint (*ptr)(argc, argv); @@ -100,9 +101,7 @@ int main(int argc, char **argv) app.db->close(); app.db = NULL; - Stdlib::HashTable::free_table(routes); - free(routes); - routes = NULL; + Router::free_router(&router); free((char *) arg); arg = NULL;