MantisBase v0.3.5
Loading...
Searching...
No Matches
router.h
Go to the documentation of this file.
1
6#ifndef MB_ROUTER_H
7#define MB_ROUTER_H
8
9#include <memory>
10#include <vector>
11#include <nlohmann/json.hpp>
12#include <httplib.h>
13
14#include "route_registry.h"
15#include "models/entity.h"
16#include "../utils/utils.h"
17#include "types.h"
18
19namespace mb {
20 class SSEMgr;
21
38 class Router {
39 public:
43 Router();
44
48 ~Router();
49
54 bool init();
55
60 bool listen();
61
65 void close();
66
71 httplib::Server &server();
72
73 SSEMgr& sseMgr() const;
74
75 // ----------- HTTP METHODS ----------- //
88 void Get(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares = {});
89
96 void Post(const std::string &path, const HandlerWithContentReaderFn &handler,
97 const Middlewares &middlewares = {});
98
105 void Post(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares = {});
106
113 void Patch(const std::string &path, const HandlerWithContentReaderFn &handler,
114 const Middlewares &middlewares = {});
115
122 void Patch(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares = {});
123
130 void Delete(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares = {});
131
132 // ----------- SCHEMA CACHE METHODS ----------- //
138 const json &schemaCache(const std::string &table_name) const;
139
145 bool hasSchemaCache(const std::string &table_name) const;
146
152 Entity schemaCacheEntity(const std::string &table_name) const;
153
158 void addSchemaCache(const nlohmann::json &entity_schema);
159
165 void updateSchemaCache(const std::string &old_entity_name, const json &new_schema);
166
171 void removeSchemaCache(const std::string &entity_name);
172
173 // ----------- UTILS METHODS ----------- //
180 std::string decompressResponseBody(const std::string &body, const std::string &encoding);
181
182 private:
183 void globalRouteHandler(const std::string &method, const std::string &path);
184
185 void globalRouteHandlerWithReader(const std::string &method, const std::string &path);
186
187 void generateMiscEndpoints();
188
189 static std::string getMimeType(const std::string &path);
190
191 // ----------- REQ/RES METHODS ----------- //
192 static std::function<void(const MantisRequest &, MantisResponse &)> handleAdminDashboardRoute() ;
193
194 // Admin routes
195 static std::function<void(const MantisRequest &, MantisResponse &)> handleListAdmins() ;
196 static std::function<void(const MantisRequest &, MantisResponse &)> handleGetAdmin() ;
197 static std::function<void(const MantisRequest &, MantisResponse &)> handleCreateAdmin() ;
198 static std::function<void(const MantisRequest &, MantisResponse &)> handleUpdateAdmin() ;
199 static std::function<void(const MantisRequest &, MantisResponse &)> handleDeleteAdmin() ;
200
201 static std::function<void(const MantisRequest &, MantisResponse &)> fileServingHandler();
202
203 static std::function<void(const MantisRequest &, MantisResponse &)> healthCheckHandler();
204
205 std::function<HandlerResponse(const httplib::Request &, httplib::Response &)> preRoutingHandler();
206
207 std::function<void(const httplib::Request &, httplib::Response &)> postRoutingHandler();
208
209 std::function<void(const httplib::Request &, httplib::Response &)> optionsHandler();
210
211 std::function<void(const httplib::Request &, const httplib::Response &)> routingLogger();
212
213 std::function<void(const httplib::Request &, httplib::Response &)> routingErrorHandler();
214
215 std::function<void(MantisRequest &, MantisResponse &)> handleAuthLogin();
216
217 std::function<void(MantisRequest &, MantisResponse &)> handleAuthRefresh();
218
219 std::function<void(MantisRequest &, MantisResponse &)> handleAuthLogout();
220
221 std::function<void(MantisRequest &, MantisResponse &)> handleSetupAdmin();
222
223 static std::function<void(const MantisRequest &, MantisResponse &)> handleLogs();
224
225
226 // Member Variables
227 MantisBase &mApp;
228 httplib::Server svr;
229 RouteRegistry m_routeRegistry;
230 std::unique_ptr<SSEMgr> m_sseMgr;
231 // std::vector<nlohmann::json> m_schemas;
232 std::vector<MiddlewareFn> m_preRoutingMiddlewares;
233 std::vector<HandlerFn> m_postRoutingMiddlewares;
234 std::unique_ptr<EntitySchema> m_entitySchema;
235 std::unordered_map<std::string, Entity> m_entityMap;
236 };
237} // mb
238
239#endif // MB_ROUTER_H
Represents a database table/entity with schema and CRUD operations.
Definition entity.h:39
MantisBase entry point.
Definition mantisbase.h:40
A wrapper class around httplib::Request offering a consistent API and allowing for easy wrapper metho...
Definition http.h:39
Wrapper around httplib::Response for consistent API.
Definition http.h:315
Definition route_registry.h:51
HTTP router for managing routes and request handling.
Definition router.h:38
Entity schemaCacheEntity(const std::string &table_name) const
Get cached entity by table name.
Definition router.cpp:225
void removeSchemaCache(const std::string &entity_name)
Remove schema from cache.
Definition router.cpp:261
void Patch(const std::string &path, const HandlerWithContentReaderFn &handler, const Middlewares &middlewares={})
Register PATCH route with content reader (for file uploads).
Definition router.cpp:196
std::string decompressResponseBody(const std::string &body, const std::string &encoding)
Decompress response body based on encoding.
Definition router_httplib_internals.cpp:339
void close()
Close HTTP server and stop listening.
Definition router.cpp:155
void updateSchemaCache(const std::string &old_entity_name, const json &new_schema)
Update cached schema.
Definition router.cpp:246
const json & schemaCache(const std::string &table_name) const
Get cached schema JSON by table name.
Definition router.cpp:213
void Get(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares={})
Register GET route.
Definition router.cpp:176
bool listen()
Start HTTP server and begin listening for connections.
Definition router.cpp:97
bool hasSchemaCache(const std::string &table_name) const
Check whether schema cache for given name exists.
Definition router.cpp:221
void Post(const std::string &path, const HandlerWithContentReaderFn &handler, const Middlewares &middlewares={})
Register POST route with content reader (for file uploads).
Definition router.cpp:182
Router()
Construct router instance.
Definition router.cpp:28
SSEMgr & sseMgr() const
Definition router.cpp:172
~Router()
Destructor.
Definition router.cpp:51
void Delete(const std::string &path, const HandlerFn &handler, const Middlewares &middlewares={})
Register DELETE route.
Definition router.cpp:208
httplib::Server & server()
Get underlying httplib::Server instance.
Definition router.cpp:168
bool init()
Initialize router: create system tables and admin routes.
Definition router.cpp:56
void addSchemaCache(const nlohmann::json &entity_schema)
Add schema to cache.
Definition router.cpp:233
Definition sse.h:85
Entity class for database table operations and CRUD functionality.
router.h
Definition auth.h:16
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
httplib::Server::HandlerResponse HandlerResponse
Definition types.h:33
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
Route registry.
Type definitions and aliases for MantisBase.