|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
Manages routing of HTTP requests to their appropriate handlers. More...
#include <Router.hpp>

Classes | |
| class | RouteBuilder |
| A builder class for fluent route configuration. More... | |
| struct | StaticRoute |
Public Member Functions | |
| Router (const std::string &prefix="", Router *parent=nullptr) | |
| Constructs a new Router. | |
| openapi::OpenApiRegistry & | openapi () |
| The OpenAPI registry for this router tree (shared by its groups). | |
| RouteBuilder | route (const std::string &path, http::HttpMethod method=http::HttpMethod::GET) |
| Initiates a route builder for a specific path and method. | |
| void | group (const std::string &prefix, std::function< void(Router &)> callback) |
| Creates a route group with a specific prefix. | |
| void | add_route (http::HttpMethod method, const std::string &path, RouteHandler handler) |
| void | add_route (http::HttpMethod method, const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| void | add_route_with_meta (http::HttpMethod method, const std::string &path, std::vector< Middleware > mws, const openapi::RouteMetadata &meta, RouteHandler handler) |
| Router & | get (const std::string &path, RouteHandler handler) |
| Router & | get (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| Router & | post (const std::string &path, RouteHandler handler) |
| Router & | post (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| Router & | put (const std::string &path, RouteHandler handler) |
| Router & | put (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| Router & | patch (const std::string &path, RouteHandler handler) |
| Router & | patch (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| Router & | del (const std::string &path, RouteHandler handler) |
| Router & | del (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| Router & | options (const std::string &path, RouteHandler handler) |
| Router & | options (const std::string &path, std::vector< Middleware > mws, RouteHandler handler) |
| void | ws (const std::string &path, WsHandler handler) |
| void | ws (const std::string &path, std::vector< Middleware > mws, WsHandler handler) |
| 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 | use (Middleware m) |
| 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/..."; not "/svcx"). | |
| void | not_found (RouteHandler handler) |
| void | on_error (ErrorHandler handler) |
| void | route (http::HttpRequest &request, std::shared_ptr< http::ResponseWriter > response_writer) const |
| bool | has_ws_route (const std::string &path) const |
| std::vector< std::string > | validate_routes () const |
| Finds routes registered more than once: the same HTTP method and exact path pattern (text, not matching behaviour) added twice, which silently keeps only one of them (the last registration for a static path, the first match for a dynamic one) with no warning otherwise. | |
| WsHandler | get_ws_route (const std::string &path) const |
| bool | is_stream_route (http::HttpMethod method, const std::string &path) const |
| void | add_stream_route (http::HttpMethod method, const std::string &path, RouteHandler handler) |
Private Member Functions | |
| void | handle_exception (std::exception_ptr error, http::HttpRequest &request, std::shared_ptr< http::ResponseWriter > response_writer, const ErrorScope *scope=nullptr) const |
| std::string | make_route_key (http::HttpMethod method, std::string_view path) const |
| void | mark_stream_route (http::HttpMethod method, const std::string &full_path) |
| std::vector< std::string > | split_path (std::string_view path) const |
| Router * | root () |
| const Router * | root () const |
Static Private Member Functions | |
| static int | match_segments (const std::vector< std::string > &pattern, const std::vector< std::string > &request, std::unordered_map< std::string, std::string > *params) |
| static bool | is_wildcard (const std::string &segment) |
Private Attributes | |
| std::string | prefix_ |
| Router * | parent_ {nullptr} |
| std::shared_ptr< openapi::OpenApiRegistry > | openapi_ |
| std::vector< Middleware > | local_middlewares_ |
| ErrorHandler | error_handler_ |
| std::shared_ptr< ErrorScope > | error_scope_ |
| RouteHandler | not_found_handler_ |
| std::unordered_map< std::string, StaticRoute > | routes_ |
| std::unordered_map< std::string, WsRoute > | ws_routes_ |
| std::vector< DynamicRoute > | dynamic_routes_ |
| std::vector< Middleware > | middlewares_ |
| std::unordered_set< std::string > | stream_routes_ |
| std::vector< DynamicRoute > | dynamic_stream_routes_ |
| std::unordered_map< std::string, int > | registration_counts_ |
Manages routing of HTTP requests to their appropriate handlers.
|
inline |
Constructs a new Router.
| prefix | The route prefix for this router. |
| parent | Pointer to the parent router, if any. |
| void orbit::routing::Router::add_route | ( | http::HttpMethod | method, |
| const std::string & | path, | ||
| RouteHandler | handler | ||
| ) |
| void orbit::routing::Router::add_route | ( | http::HttpMethod | method, |
| const std::string & | path, | ||
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| void orbit::routing::Router::add_route_with_meta | ( | http::HttpMethod | method, |
| const std::string & | path, | ||
| std::vector< Middleware > | mws, | ||
| const openapi::RouteMetadata & | meta, | ||
| RouteHandler | handler | ||
| ) |
| void orbit::routing::Router::add_stream_route | ( | http::HttpMethod | method, |
| const std::string & | path, | ||
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::del | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::del | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::get | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::get | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| WsHandler orbit::routing::Router::get_ws_route | ( | const std::string & | path | ) | const |
| void orbit::routing::Router::group | ( | const std::string & | prefix, |
| std::function< void(Router &)> | callback | ||
| ) |
Creates a route group with a specific prefix.
| prefix | The URL prefix for the group. |
| callback | A function to configure routes within the group. |
|
private |
| bool orbit::routing::Router::has_ws_route | ( | const std::string & | path | ) | const |
| bool orbit::routing::Router::is_stream_route | ( | http::HttpMethod | method, |
| const std::string & | path | ||
| ) | const |
|
inlinestaticprivate |
|
private |
|
private |
|
staticprivate |
| void orbit::routing::Router::not_found | ( | RouteHandler | handler | ) |
Answers requests that match no route (instead of "404 Not Found"). A path that exists under other methods still gets 405 with Allow.
| void orbit::routing::Router::on_error | ( | ErrorHandler | handler | ) |
Handles exceptions from routes. On the app (root router) it covers every route, unmatched requests and app-level middleware; in a group it covers that group's routes and nested groups, before or after they are registered.
|
inline |
The OpenAPI registry for this router tree (shared by its groups).
| Router & orbit::routing::Router::options | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::options | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::patch | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::patch | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::post | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::post | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::put | ( | const std::string & | path, |
| RouteHandler | handler | ||
| ) |
| Router & orbit::routing::Router::put | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| RouteHandler | handler | ||
| ) |
|
inlineprivate |
|
inlineprivate |
|
inline |
Initiates a route builder for a specific path and method.
| path | The URL path. |
| method | The HTTP method (default is GET). |
| void orbit::routing::Router::route | ( | http::HttpRequest & | request, |
| std::shared_ptr< http::ResponseWriter > | response_writer | ||
| ) | const |
| bool orbit::routing::Router::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.
|
private |
| void orbit::routing::Router::use | ( | const std::string & | prefix, |
| Middleware | m | ||
| ) |
Runs m only for requests under prefix: the prefix itself and anything below it ("/svc" and "/svc/..."; not "/svcx").
Runs before route matching, so it can serve paths that have no route, e.g. app.use("/api", orbit::middleware::proxy(opts)).
| std::invalid_argument | if prefix does not start with '/'. |
| void orbit::routing::Router::use | ( | Middleware | m | ) |
| std::vector< std::string > orbit::routing::Router::validate_routes | ( | ) | const |
Finds routes registered more than once: the same HTTP method and exact path pattern (text, not matching behaviour) added twice, which silently keeps only one of them (the last registration for a static path, the first match for a dynamic one) with no warning otherwise.
| void orbit::routing::Router::ws | ( | const std::string & | path, |
| std::vector< Middleware > | mws, | ||
| WsHandler | handler | ||
| ) |
| void orbit::routing::Router::ws | ( | const std::string & | path, |
| WsHandler | handler | ||
| ) |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |