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));
166 std::unordered_map<RouteKey, RouteHandler, RouteKeyHash> routes;
177 void add(
const std::string& method,
178 const std::string& path,
180 const std::vector<Middleware>& middlewares);
188 const RouteHandler*
find(
const std::string& method,
const std::string& path)
const;
197 json remove(
const std::string& method,
const std::string& path);
214 void Get(
const std::string& path,
216 std::initializer_list<Middleware> middlewares = {});
218 void Post(
const std::string& path,
220 std::initializer_list<Middleware> middlewares = {});
222 void Patch(
const std::string& path,
224 std::initializer_list<Middleware> middlewares = {});
226 void Delete(
const std::string& path,
228 std::initializer_list<Middleware> middlewares = {});
237 bool listen(
const std::string& host,
const int& port);
252 httplib::Server&
server();
254 const std::string
_class_ =
"mantis::HttpUnit";
257 using Method = void (httplib::Server::*)(const std::string&, const httplib::Server::Handler&);
258 using MethodBinder = std::function<void(
const std::string&, httplib::Server::Handler)>;
260 void route(MethodBinder bind_method,
261 const std::string& method,
262 const std::string& path,
264 std::initializer_list<Middleware> middlewares);
268 thread_local static Context current_context;
void dump()
Convenience method for dumping context data for debugging.
Definition http.cpp:15
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:233
void Patch(const std::string &path, RouteHandlerFunc handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:179
void Delete(const std::string &path, RouteHandlerFunc handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:188
void Post(const std::string &path, RouteHandlerFunc handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:170
static Context & context()
Definition http.cpp:240
const std::string _class_
Definition http.h:254
RouteRegistry & routeRegistry()
Fetch the underlying route registry, check.
Definition http.cpp:245
httplib::Server & server()
Definition http.cpp:250
bool listen(const std::string &host, const int &port)
Bind to a port and start listening for requests.
Definition http.cpp:197
HttpUnit()
Definition http.cpp:94
void Get(const std::string &path, RouteHandlerFunc handler, std::initializer_list< Middleware > middlewares={})
Definition http.cpp:161
json remove(const std::string &method, const std::string &path)
Remove find and remove existing route + path pair from the registry.
Definition http.cpp:74
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:60
const std::string __class_name__
Definition http.h:199
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:68
Wrapper around spdlog's functionality.
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:30
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
std::function< void(const Request &, Response &, Context &)> RouteHandlerFunc
Route Handler function shorthand
Definition http.h:127
std::string Path
Syntactic sugar for request path which is a std::string
Definition http.h:133
std::string Method
Syntactic sugar for request method which is a std::string
Definition http.h:130
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:124
std::pair< Method, Path > RouteKey
Shorthand notation for the request's method, path pair.
Definition http.h:136
Struct encompassing the list of middlewares and the handler function registered to a specific route.
Definition http.h:155
std::vector< Middleware > middlewares
Definition http.h:156
RouteHandlerFunc handler
List of
Definition http.h:157
size_t operator()(const RouteKey &k) const
Operator function called when hashing RouteKey is required.
Definition http.cpp:55