mirror of
https://github.com/Karaka-Management/oms-OnlineResourceWatcher.git
synced 2026-02-16 07:58:41 +00:00
Change routing
This commit is contained in:
parent
d686f87fbe
commit
d7b1fed424
|
|
@ -12,54 +12,21 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <iostream>
|
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
#include "Controller/ApiController.h"
|
#include "Controller/ApiController.h"
|
||||||
#include "Controller/InstallController.h"
|
#include "Controller/InstallController.h"
|
||||||
#include "cOMS/Stdlib/HashTable.h"
|
#include "cOMS/Router/Router.h"
|
||||||
|
|
||||||
typedef void (*Fptr)(int, char **);
|
Router generate_routes()
|
||||||
|
|
||||||
Stdlib::HashTable::ht *generate_routes()
|
|
||||||
{
|
{
|
||||||
Stdlib::HashTable::ht *table = Stdlib::HashTable::create_table(4, true);
|
Router router = Router::create_router(4);
|
||||||
if (table == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stdlib::HashTable::set_entry(table, "^.*?\\-h *.*$", (void *) &Controller::ApiController::printHelp);
|
router.set("^.*?\\-h *.*$", (void *) &Controller::ApiController::printHelp);
|
||||||
Stdlib::HashTable::set_entry(table, "^.*?\\-v *.*$", (void *) &Controller::ApiController::printVersion);
|
router.set("^.*?\\-v *.*$", (void *) &Controller::ApiController::printVersion);
|
||||||
Stdlib::HashTable::set_entry(table, "^.*?\\-r *.*$", (void *) &Controller::ApiController::checkResources);
|
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 router;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit 382ef60919c33d19950c105a5938ecd631ba1499
|
Subproject commit a6216bdb586725637e85d5b57bca4a02b388d678
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
#include "cOMS/Utils/ApplicationUtils.h"
|
#include "cOMS/Utils/ApplicationUtils.h"
|
||||||
#include "cOMS/DataStorage/Database/Connection/ConnectionAbstract.h"
|
#include "cOMS/DataStorage/Database/Connection/ConnectionAbstract.h"
|
||||||
#include "cOMS/Utils/Parser/Json.h"
|
#include "cOMS/Utils/Parser/Json.h"
|
||||||
#include "cOMS/Stdlib/HashTable.h"
|
#include "cOMS/Router/Router.h"
|
||||||
|
|
||||||
#include "Routes.h"
|
#include "Routes.h"
|
||||||
|
|
||||||
|
|
@ -85,12 +85,13 @@ int main(int argc, char **argv)
|
||||||
/* --------------- Handle request --------------- */
|
/* --------------- Handle request --------------- */
|
||||||
|
|
||||||
// Handle routes
|
// Handle routes
|
||||||
Stdlib::HashTable::ht *routes = generate_routes();
|
Router router = generate_routes();
|
||||||
if (routes == NULL) {
|
Fptr ptr = Router::match_route(&router, arg);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Fptr ptr = match_route(routes, (char *) arg);
|
// No endpoint found
|
||||||
|
if (ptr == NULL) {
|
||||||
|
ptr = &Controller::ApiController::printHelp;
|
||||||
|
}
|
||||||
|
|
||||||
// Dispatch found endpoint
|
// Dispatch found endpoint
|
||||||
(*ptr)(argc, argv);
|
(*ptr)(argc, argv);
|
||||||
|
|
@ -100,9 +101,7 @@ int main(int argc, char **argv)
|
||||||
app.db->close();
|
app.db->close();
|
||||||
app.db = NULL;
|
app.db = NULL;
|
||||||
|
|
||||||
Stdlib::HashTable::free_table(routes);
|
Router::free_router(&router);
|
||||||
free(routes);
|
|
||||||
routes = NULL;
|
|
||||||
|
|
||||||
free((char *) arg);
|
free((char *) arg);
|
||||||
arg = NULL;
|
arg = NULL;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user