13#include <unordered_map>
18#include <nlohmann/json.hpp>
22#define REQUEST_HANDLED false
23#define REQUEST_PENDING true
28 using json = nlohmann::json;
60 std::unordered_map<std::string, std::any> data;
61 std::string __class_name__ =
"mantis::Context";
78 void set(
const std::string& key, T value)
80 data[key] = std::move(value);
91 std::optional<T*>
get(
const std::string& key)
93 const auto it = data.find(key);
94 if (it != data.end())
return std::any_cast<T>(&it->second);
106 template <
typename T>
107 T&
get_or(
const std::string& key, T default_value)
109 if (
const auto it = data.find(key); it == data.end())
111 data[key] = std::move(default_value);
113 return std::any_cast<T&>(data.at(key));
163 std::variant<RouteHandlerFunc, RouteHandlerFuncWithContentReader>
handler;
172 std::unordered_map<RouteKey, RouteHandler, RouteKeyHash> routes;
183 void add(
const std::string& method,
184 const std::string& path,
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;
215 json remove(
const std::string& method,
const std::string& path);
233 void Get(
const std::string& path,
235 std::initializer_list<Middleware> middlewares = {});
237 void Post(
const std::string& path,
239 std::initializer_list<Middleware> middlewares = {});
241 void Post(
const std::string& path,
243 std::initializer_list<Middleware> middlewares = {});
245 void Patch(
const std::string& path,
247 std::initializer_list<Middleware> middlewares = {});
249 void Patch(
const std::string& path,
251 std::initializer_list<Middleware> middlewares = {});
253 void Delete(
const std::string& path,
255 std::initializer_list<Middleware> middlewares = {});
264 bool listen(
const std::string& host,
const int& port);
284 httplib::Server&
server();
293 const std::string
_class_ =
"mantis::HttpUnit";
304 std::string decompressResponseBody(
const std::string& body,
const std::string& encoding);
306 using Method = void (httplib::Server::*)(const std::string&, const httplib::Server::Handler&);
308 template <
typename HandlerType>
309 using MethodBinder = std::function<void(
const std::string&, HandlerType)>;
311 void route(
const MethodBinder<httplib::Server::Handler>& bind_method,
312 const std::string& method,
313 const std::string& path,
315 std::initializer_list<Middleware> middlewares);
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);
323 template<
class... Ts>
struct overloaded : Ts... {
using Ts::operator()...; };
324 template<
class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
328 thread_local static Context current_context;
void dump()
Convenience method for dumping context data for debugging.
Definition http.cpp:20
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
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
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
size_t operator()(const RouteKey &k) const
Operator function called when hashing RouteKey is required.
Definition http.cpp:60