Mantis App v0.2.8
Loading...
Searching...
No Matches
http.h
Go to the documentation of this file.
1
9#ifndef HTTPSERVER_H
10#define HTTPSERVER_H
11
12#include <httplib.h>
13#include <unordered_map>
14#include <any>
15#include <functional>
16#include <string>
17#include <vector>
18#include <nlohmann/json.hpp>
19
20#include "logging.h"
21
22#define REQUEST_HANDLED false
23#define REQUEST_PENDING true
24
25namespace mantis
26{
28 using json = nlohmann::json;
29
58 class Context
59 {
60 std::unordered_map<std::string, std::any> data;
61 std::string __class_name__ = "mantis::Context";
62
63 public:
64 Context() = default;
68 void dump();
69
77 template <typename T>
78 void set(const std::string& key, T value)
79 {
80 data[key] = std::move(value);
81 }
82
90 template <typename T>
91 std::optional<T*> get(const std::string& key)
92 {
93 const auto it = data.find(key);
94 if (it != data.end()) return std::any_cast<T>(&it->second);
95 return std::nullopt;
96 }
97
106 template <typename T>
107 T& get_or(const std::string& key, T default_value)
108 {
109 if (const auto it = data.find(key); it == data.end())
110 {
111 data[key] = std::move(default_value);
112 }
113 return std::any_cast<T&>(data.at(key));
114 }
115 };
116
118 using Request = httplib::Request;
119
121 using Response = httplib::Response;
122
124 using ContentReader = httplib::ContentReader;
125
127 using Middleware = std::function<bool(const Request&, Response&, Context&)>;
128
130 using RouteHandlerFunc = std::function<void(const httplib::Request&, httplib::Response&, Context&)>;
131
133 using RouteHandlerFuncWithContentReader = std::function<void(const httplib::Request&, httplib::Response&, const httplib::ContentReader&, Context&)>;
134
136 using Method = std::string;
137
139 using Path = std::string;
140
142 using RouteKey = std::pair<Method, Path>;
143
148 {
154 size_t operator()(const RouteKey& k) const;
155 };
156
161 {
162 std::vector<Middleware> middlewares;
163 std::variant<RouteHandlerFunc, RouteHandlerFuncWithContentReader> handler;
164 };
165
170 {
172 std::unordered_map<RouteKey, RouteHandler, RouteKeyHash> routes;
173
174 public:
183 void add(const std::string& method,
184 const std::string& path,
185 RouteHandlerFunc handler,
186 const std::vector<Middleware>& middlewares);
195 void add(const std::string& method,
196 const std::string& path,
198 const std::vector<Middleware>& middlewares);
206 const RouteHandler* find(const std::string& method, const std::string& path) const;
207
215 json remove(const std::string& method, const std::string& path);
216
217 const std::string __class_name__ = "mantis::RouteRegistry";
218 };
219
228 {
229 public:
230 HttpUnit();
231 ~HttpUnit();
232
233 void Get(const std::string& path,
234 const RouteHandlerFunc& handler,
235 std::initializer_list<Middleware> middlewares = {});
236
237 void Post(const std::string& path,
238 const RouteHandlerFunc& handler,
239 std::initializer_list<Middleware> middlewares = {});
240
241 void Post(const std::string& path,
243 std::initializer_list<Middleware> middlewares = {});
244
245 void Patch(const std::string& path,
246 const RouteHandlerFunc& handler,
247 std::initializer_list<Middleware> middlewares = {});
248
249 void Patch(const std::string& path,
251 std::initializer_list<Middleware> middlewares = {});
252
253 void Delete(const std::string& path,
254 const RouteHandlerFunc& handler,
255 std::initializer_list<Middleware> middlewares = {});
256
264 bool listen(const std::string& host, const int& port);
265
269 void close();
270
271 static Context& context();
272
278
284 httplib::Server& server();
285
291 static std::string hashMultipartMetadata(const httplib::FormData& data);
292
293 const std::string _class_ = "mantis::HttpUnit";
294
295 private:
304 std::string decompressResponseBody(const std::string& body, const std::string& encoding);
305
306 using Method = void (httplib::Server::*)(const std::string&, const httplib::Server::Handler&);
307
308 template <typename HandlerType>
309 using MethodBinder = std::function<void(const std::string&, HandlerType)>;
310
311 void route(const MethodBinder<httplib::Server::Handler>& bind_method,
312 const std::string& method,
313 const std::string& path,
314 const RouteHandlerFunc& handler,
315 std::initializer_list<Middleware> middlewares);
316
317 void route(const MethodBinder<httplib::Server::HandlerWithContentReader>& bind_method,
318 const std::string& method,
319 const std::string& path,
321 std::initializer_list<Middleware> middlewares);
322
323 template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
324 template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
325
326 httplib::Server svr;
327 RouteRegistry registry;
328 thread_local static Context current_context;
329 };
330}
331
332#endif // HTTPSERVER_H
Definition http.h:59
void dump()
Convenience method for dumping context data for debugging.
Definition http.cpp:20
Context()=default
std::optional< T * > get(const std::string &key)
Get context value given the key.
Definition http.h:91
void set(const std::string &key, T value)
Store a key-value data in the context.
Definition http.h:78
T & get_or(const std::string &key, T default_value)
Get context value given the key.
Definition http.h:107
Definition http.h:228
void close()
Close the HTTP server connection.
Definition http.cpp:285
static std::string hashMultipartMetadata(const httplib::FormData &data)
Generate hash for the file metadata.
Definition http.cpp:309
static Context & context()
Definition http.cpp:294
void Patch(const std::string &path, const RouteHandlerFunc &handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:217
const std::string _class_
Definition http.h:293
void Post(const std::string &path, const RouteHandlerFunc &handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:199
~HttpUnit()
Definition http.cpp:185
RouteRegistry & routeRegistry()
Fetch the underlying route registry, check.
Definition http.cpp:299
httplib::Server & server()
Get a reference to the httplib::Server instance.
Definition http.cpp:304
bool listen(const std::string &host, const int &port)
Bind to a port and start listening for requests.
Definition http.cpp:244
void Get(const std::string &path, const RouteHandlerFunc &handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:190
HttpUnit()
Definition http.cpp:106
void Delete(const std::string &path, const RouteHandlerFunc &handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:235
Definition http.h:170
json remove(const std::string &method, const std::string &path)
Remove find and remove existing route + path pair from the registry.
Definition http.cpp:85
void add(const std::string &method, const std::string &path, RouteHandlerFunc handler, const std::vector< Middleware > &middlewares)
Add new route to the registry.
Definition http.cpp:65
const std::string __class_name__
Definition http.h:217
const RouteHandler * find(const std::string &method, const std::string &path) const
Find a route in the registry matching given method and route.
Definition http.cpp:79
Wrapper around spdlog's functionality.
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:31
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
std::function< void(const httplib::Request &, httplib::Response &, const httplib::ContentReader &, Context &)> RouteHandlerFuncWithContentReader
‍Route Handler function with content reader shorthand
Definition http.h:133
std::function< void(const httplib::Request &, httplib::Response &, Context &)> RouteHandlerFunc
‍Route Handler function shorthand
Definition http.h:130
std::string Path
‍Syntactic sugar for request path which is a std::string
Definition http.h:139
std::string Method
‍Syntactic sugar for request method which is a std::string
Definition http.h:136
httplib::ContentReader ContentReader
‍Shorthand for httplib::ContentReader
Definition http.h:124
httplib::Response Response
‍Shorthand for httplib::Response
Definition http.h:121
httplib::Request Request
‍Shorthand for httplib::Request
Definition http.h:118
std::function< bool(const Request &, Response &, Context &)> Middleware
‍Middleware shorthand for the function
Definition http.h:127
std::pair< Method, Path > RouteKey
‍Shorthand notation for the request's method, path pair.
Definition http.h:142
Struct encompassing the list of middlewares and the handler function registered to a specific route.
Definition http.h:161
std::variant< RouteHandlerFunc, RouteHandlerFuncWithContentReader > handler
‍List of
Definition http.h:163
std::vector< Middleware > middlewares
Definition http.h:162
Definition http.h:148
size_t operator()(const RouteKey &k) const
Operator function called when hashing RouteKey is required.
Definition http.cpp:60