MantisBase v0.3.4
Loading...
Searching...
No Matches
route_registry.h
Go to the documentation of this file.
1
8#ifndef ROUTE_REGISTRY_H
9#define ROUTE_REGISTRY_H
10
11#include <httplib.h>
12#include <unordered_map>
13#include <functional>
14#include <string>
15#include <variant>
16#include <vector>
17#include <nlohmann/json.hpp>
18
19#include "../utils/utils.h"
20#include "types.h"
21#include "logger/logger.h"
22
23namespace mb
24{
29 {
35 size_t operator()(const RouteKey& k) const;
36 };
37
42 {
43 std::vector<MiddlewareFn> middlewares;
44 std::variant<HandlerFn, HandlerWithContentReaderFn> handler;
45 };
46
51 {
53 std::unordered_map<RouteKey, RouteHandler, RouteKeyHash> routes;
54
55 public:
64 void add(const std::string& method,
65 const std::string& path,
66 HandlerFn handler,
67 const Middlewares& middlewares);
76 void add(const std::string& method,
77 const std::string& path,
79 const Middlewares& middlewares);
87 const RouteHandler* find(const std::string& method, const std::string& path) const;
88
96 json remove(const std::string& method, const std::string& path);
97
98 const std::string __class_name__ = "mb::RouteRegistry";
99 };
100}
101
102#endif // ROUTE_REGISTRY_H
Definition route_registry.h:51
void add(const std::string &method, const std::string &path, HandlerFn handler, const Middlewares &middlewares)
Add new route to the registry.
Definition route_registry.cpp:17
const RouteHandler * find(const std::string &method, const std::string &path) const
Find a route in the registry matching given method and route.
Definition route_registry.cpp:33
json remove(const std::string &method, const std::string &path)
Remove find and remove existing route + path pair from the registry.
Definition route_registry.cpp:39
const std::string __class_name__
Definition route_registry.h:98
router.h
Definition auth.h:15
std::pair< Method, Path > RouteKey
‍Shorthand notation for the request's method, path pair.
Definition types.h:55
std::vector< MiddlewareFn > Middlewares
‍Middleware function arrays
Definition types.h:46
std::function< void(MantisRequest &, MantisResponse &, MantisContentReader &)> HandlerWithContentReaderFn
‍Route Handler function with content reader shorthand
Definition types.h:40
std::function< void(MantisRequest &, MantisResponse &)> HandlerFn
‍Route Handler function shorthand
Definition types.h:36
nlohmann::json json
Shorten JSON namespace.
Definition context_store.h:18
Struct encompassing the list of middlewares and the handler function registered to a specific route.
Definition route_registry.h:42
std::vector< MiddlewareFn > middlewares
Definition route_registry.h:43
std::variant< HandlerFn, HandlerWithContentReaderFn > handler
‍List of
Definition route_registry.h:44
Definition route_registry.h:29
size_t operator()(const RouteKey &k) const
Operator function called when hashing RouteKey is required.
Definition route_registry.cpp:12
Type definitions and aliases for MantisBase.