10#include <unordered_map>
11#include <unordered_set>
33 std::shared_ptr<const ErrorScope>
parent;
40 std::shared_ptr<const ErrorScope>
errors;
79 std::vector<Middleware>
mws_;
105 void group(
const std::string& prefix, std::function<
void(
Router&)> callback);
132 void ws(
const std::string& path, std::vector<Middleware> mws,
WsHandler handler);
185 std::shared_ptr<http::ResponseWriter> response_writer,
190 std::shared_ptr<const ErrorScope>
errors;
204 std::vector<std::string>
split_path(std::string_view path)
const;
211 static int match_segments(
const std::vector<std::string>& pattern,
const std::vector<std::string>& request,
212 std::unordered_map<std::string, std::string>* params);
213 static bool is_wildcard(
const std::string& segment) {
return !segment.empty() && segment[0] ==
'*'; }
219 std::shared_ptr<openapi::OpenApiRegistry>
openapi_;
225 std::unordered_map<std::string, StaticRoute>
routes_;
Represents an active WebSocket connection.
Definition WebSocketConnection.hpp:88
Definition OpenApi.hpp:20
A builder class for fluent route configuration.
Definition Router.hpp:63
std::string path_
Definition Router.hpp:78
RouteBuilder & tag(const std::string &t)
Definition Router.hpp:70
std::vector< Middleware > mws_
Definition Router.hpp:79
RouteBuilder & res_body(int status, const std::string &schema_name)
Definition Router.hpp:72
RouteBuilder & summary(const std::string &s)
Definition Router.hpp:68
Router & router_
Definition Router.hpp:76
http::HttpMethod method_
Definition Router.hpp:77
RouteBuilder & req_body(const std::string &schema_name)
Definition Router.hpp:71
void handler(RouteHandler h)
RouteBuilder(Router &router, http::HttpMethod method, const std::string &path, std::vector< Middleware > mws={})
Definition Router.hpp:65
openapi::RouteMetadata meta_
Definition Router.hpp:80
RouteBuilder & description(const std::string &s)
Definition Router.hpp:69
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
std::unordered_map< std::string, WsRoute > ws_routes_
Definition Router.hpp:226
ErrorHandler error_handler_
Definition Router.hpp:221
void use(const std::string &prefix, Middleware m)
Runs m only for requests under prefix: the prefix itself and anything below it ("/svc" and "/svc/....
Router & post(const std::string &path, RouteHandler handler)
Router(const std::string &prefix="", Router *parent=nullptr)
Constructs a new Router.
Definition Router.hpp:53
openapi::OpenApiRegistry & openapi()
The OpenAPI registry for this router tree (shared by its groups).
Definition Router.hpp:58
std::shared_ptr< ErrorScope > error_scope_
Definition Router.hpp:222
std::unordered_map< std::string, int > registration_counts_
Definition Router.hpp:235
std::string prefix_
Definition Router.hpp:217
Router * parent_
Definition Router.hpp:218
Router * root()
Definition Router.hpp:214
std::vector< DynamicRoute > dynamic_routes_
Definition Router.hpp:227
std::shared_ptr< openapi::OpenApiRegistry > openapi_
Definition Router.hpp:219
void add_route(http::HttpMethod method, const std::string &path, RouteHandler handler)
void add_route_with_meta(http::HttpMethod method, const std::string &path, std::vector< Middleware > mws, const openapi::RouteMetadata &meta, RouteHandler handler)
void on_error(ErrorHandler handler)
std::vector< Middleware > middlewares_
Definition Router.hpp:228
std::vector< Middleware > local_middlewares_
Definition Router.hpp:220
void not_found(RouteHandler handler)
void add_route(http::HttpMethod method, const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
Router & patch(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
RouteHandler not_found_handler_
Definition Router.hpp:223
void handle_exception(std::exception_ptr error, http::HttpRequest &request, std::shared_ptr< http::ResponseWriter > response_writer, const ErrorScope *scope=nullptr) const
void add_stream_route(http::HttpMethod method, const std::string &path, RouteHandler handler)
WsHandler get_ws_route(const std::string &path) const
std::unordered_set< std::string > stream_routes_
Definition Router.hpp:230
std::vector< std::string > validate_routes() const
Finds routes registered more than once: the same HTTP method and exact path pattern (text,...
std::string make_route_key(http::HttpMethod method, std::string_view path) const
Router & get(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
Router & options(const std::string &path, RouteHandler handler)
bool is_stream_route(http::HttpMethod method, const std::string &path) const
void ws(const std::string &path, WsHandler handler)
void group(const std::string &prefix, std::function< void(Router &)> callback)
Creates a route group with a specific prefix.
Router & put(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
std::vector< DynamicRoute > dynamic_stream_routes_
Definition Router.hpp:231
Router & get(const std::string &path, RouteHandler handler)
Router & put(const std::string &path, RouteHandler handler)
static int match_segments(const std::vector< std::string > &pattern, const std::vector< std::string > &request, std::unordered_map< std::string, std::string > *params)
std::unordered_map< std::string, StaticRoute > routes_
Definition Router.hpp:225
Router & del(const std::string &path, RouteHandler handler)
const Router * root() const
Definition Router.hpp:215
bool run_ws_middlewares(const std::string &path, http::HttpRequest &request, std::shared_ptr< http::ResponseWriter > response_writer) const
Runs global, group and route middleware for a WebSocket upgrade.
void ws(const std::string &path, std::vector< Middleware > mws, WsHandler handler)
std::vector< std::string > split_path(std::string_view path) const
RouteBuilder route(const std::string &path, http::HttpMethod method=http::HttpMethod::GET)
Initiates a route builder for a specific path and method.
Definition Router.hpp:95
static bool is_wildcard(const std::string &segment)
Definition Router.hpp:213
Router & options(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
bool has_ws_route(const std::string &path) const
void mark_stream_route(http::HttpMethod method, const std::string &full_path)
Router & del(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
Router & post(const std::string &path, std::vector< Middleware > mws, RouteHandler handler)
Router & patch(const std::string &path, RouteHandler handler)
void route(http::HttpRequest &request, std::shared_ptr< http::ResponseWriter > response_writer) const
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:28
Definition legacy_namespaces.hpp:22
Definition legacy_namespaces.hpp:24
std::function< void(http::websocket::WebSocketConnection &)> WsHandler
Definition Router.hpp:20
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> Middleware
Definition Router.hpp:19
std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> RouteHandler
Definition Router.hpp:18
std::function< void(const std::exception &, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> ErrorHandler
Definition Router.hpp:21
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
std::vector< std::string > path_segments
Definition Router.hpp:38
http::HttpMethod method
Definition Router.hpp:37
RouteHandler handler
Definition Router.hpp:39
std::shared_ptr< const ErrorScope > errors
Definition Router.hpp:40
ErrorHandler handler
Definition Router.hpp:32
std::shared_ptr< const ErrorScope > parent
Definition Router.hpp:33
Definition Router.hpp:188
std::shared_ptr< const ErrorScope > errors
Definition Router.hpp:190
RouteHandler handler
Definition Router.hpp:189
std::vector< Middleware > middlewares
Definition Router.hpp:25
WsHandler handler
Definition Router.hpp:24