Change routing

This commit is contained in:
Dennis Eichhorn 2022-11-23 20:44:56 +01:00
parent d686f87fbe
commit d7b1fed424
3 changed files with 17 additions and 51 deletions

View File

@ -12,54 +12,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <regex>
#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

@ -1 +1 @@
Subproject commit 382ef60919c33d19950c105a5938ecd631ba1499
Subproject commit a6216bdb586725637e85d5b57bca4a02b388d678

View File

@ -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;