Mantis App v0.1.13
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 Middleware = std::function<bool(const Request&, Response&, Context&)>;
125
127 using RouteHandlerFunc = std::function<void(const Request&, Response&, Context&)>;
128
130 using Method = std::string;
131
133 using Path = std::string;
134
136 using RouteKey = std::pair<Method, Path>;
137
142 {
148 size_t operator()(const RouteKey& k) const;
149 };
150
155 {
156 std::vector<Middleware> middlewares;
158 };
159
164 {
166 std::unordered_map<RouteKey, RouteHandler, RouteKeyHash> routes;
167
168 public:
177 void add(const std::string& method,
178 const std::string& path,
179 RouteHandlerFunc handler,
180 const std::vector<Middleware>& middlewares);
188 const RouteHandler* find(const std::string& method, const std::string& path) const;
189
197 json remove(const std::string& method, const std::string& path);
198
199 const std::string __class_name__ = "mantis::RouteRegistry";
200 };
201
210 {
211 public:
212 HttpUnit();
213
214 void Get(const std::string& path,
215 RouteHandlerFunc handler,
216 std::initializer_list<Middleware> middlewares = {});
217
218 void Post(const std::string& path,
219 RouteHandlerFunc handler,
220 std::initializer_list<Middleware> middlewares = {});
221
222 void Patch(const std::string& path,
223 RouteHandlerFunc handler,
224 std::initializer_list<Middleware> middlewares = {});
225
226 void Delete(const std::string& path,
227 RouteHandlerFunc handler,
228 std::initializer_list<Middleware> middlewares = {});
229
237 bool listen(const std::string& host, const int& port);
238
242 void close();
243
244 static Context& context();
245
251
252 httplib::Server& server();
253
254 const std::string _class_ = "mantis::HttpUnit";
255
256 private:
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)>;
259
260 void route(MethodBinder bind_method,
261 const std::string& method,
262 const std::string& path,
263 RouteHandlerFunc handler,
264 std::initializer_list<Middleware> middlewares);
265
266 httplib::Server svr;
267 RouteRegistry registry;
268 thread_local static Context current_context;
269 };
270}
271
272#endif // HTTPSERVER_H
Definition http.h:59
void dump()
Convenience method for dumping context data for debugging.
Definition http.cpp:15
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:210
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
Definition http.h:164
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
Definition http.h:142
size_t operator()(const RouteKey &k) const
Operator function called when hashing RouteKey is required.
Definition http.cpp:55