MantisBase v0.3.4
Loading...
Searching...
No Matches
router.h
Go to the documentation of this file.
1
6#ifndef MANTIS_SERVER_H
7#define MANTIS_SERVER_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 static std::function<void(const MantisRequest &, MantisResponse &)> fileServingHandler();
195
196 static std::function<void(const MantisRequest &, MantisResponse &)> healthCheckHandler();
197
198 std::function<HandlerResponse(const httplib::Request &, httplib::Response &)> preRoutingHandler();
199
200 std::function<void(const httplib::Request &, httplib::Response &)> postRoutingHandler();
201
202 std::function<void(const httplib::Request &, httplib::Response &)> optionsHandler();
203
204 std::function<void(const httplib::Request &, const httplib::Response &)> routingLogger();
205
206 std::function<void(const httplib::Request &, httplib::Response &)> routingErrorHandler();
207
208 std::function<void(MantisRequest &, MantisResponse &)> handleAuthLogin();
209
210 std::function<void(MantisRequest &, MantisResponse &)> handleAuthRefresh();
211
212 std::function<void(MantisRequest &, MantisResponse &)> handleAuthLogout();
213
214 std::function<void(MantisRequest &, MantisResponse &)> handleSetupAdmin();
215
216 static std::function<void(const MantisRequest &, MantisResponse &)> handleLogs();
217
218
219 // Member Variables
220 MantisBase &mApp;
221 httplib::Server svr;
222 RouteRegistry m_routeRegistry;
223 std::unique_ptr<SSEMgr> m_sseMgr;
224 // std::vector<nlohmann::json> m_schemas;
225 std::vector<MiddlewareFn> m_preRoutingMiddlewares;
226 std::vector<HandlerFn> m_postRoutingMiddlewares;
227 std::unique_ptr<EntitySchema> m_entitySchema;
228 std::unordered_map<std::string, Entity> m_entityMap;
229 };
230} // mb
231
232#endif // MANTIS_SERVER_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:42
Wrapper around httplib::Response for consistent API.
Definition http.h:318
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:338
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:15
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.