pub struct Router { /* private fields */ }Expand description
A modular router for grouping and mounting routes under a common base path.
The Router struct allows you to organize related routes together and mount them
onto an application at a specified base path. This is useful for building APIs
with versioning, grouping endpoints, or composing applications from multiple routers.
§Example
use ripress::{router::Router, app::App};
use ripress::{req::HttpRequest, res::HttpResponse};
use ripress::types::RouterFns;
async fn handler(req: HttpRequest, res: HttpResponse) -> HttpResponse {
res.ok().text("Hello, World!")
}
let mut router = Router::new("/api");
router.get("/hello", handler);
let mut app = App::new();
app.router(router);Implementations§
Source§impl Router
impl Router
Sourcepub fn new(base_path: &'static str) -> Self
pub fn new(base_path: &'static str) -> Self
Creates a new Router instance with the specified base path.
The base path determines the prefix under which all routes registered to this router will be mounted when attached to an application.
§Arguments
base_path- The base path (e.g., “/api” or “/v1”) for this router.
§Returns
A new Router with an empty set of routes, ready for route registration.
§Example
use ripress::router::Router;
let router = Router::new("/api");Sourcepub fn register(self, app: &mut App)
👎Deprecated since 1.9.12: use app.router instead
pub fn register(self, app: &mut App)
use app.router instead
Registers a router with an app.
§Arguments
mut app- The instance of the app to register the router too
§Example
use ripress::{router::Router, app::App};
use ripress::{req::HttpRequest, res::HttpResponse};
use ripress::types::RouterFns;
async fn handler(req: HttpRequest, res: HttpResponse) -> HttpResponse {
res.ok().text("Hello, World!")
}
let mut router = Router::new("/api");
let mut app = App::new();
router.patch("/hello", handler);
app.router(router);Trait Implementations§
Source§impl RouterFns for Router
impl RouterFns for Router
Source§fn routes(
&mut self,
) -> &mut HashMap<String, HashMap<HttpMethods, Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>>
fn routes( &mut self, ) -> &mut HashMap<String, HashMap<HttpMethods, Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>>
Get a mutable reference to the internal routes map. Read more
Source§fn add_route<F, HFut>(&mut self, method: HttpMethods, path: &str, handler: F)where
F: Fn(HttpRequest, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
fn add_route<F, HFut>(&mut self, method: HttpMethods, path: &str, handler: F)where
F: Fn(HttpRequest, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
Register a handler for a specific HTTP method/path. Read more
Source§fn get<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn get<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a GET handler for a path, with extractor integration. Read more
Source§fn options<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn options<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register an OPTIONS handler for a path, with extractor integration.
Source§fn post<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn post<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a POST handler for a path, with extractor integration.
Source§fn put<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn put<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a PUT handler for a path, with extractor integration.
Source§fn delete<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn delete<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a DELETE handler for a path, with extractor integration.
Source§fn head<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn head<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a HEAD handler for a path, with extractor integration.
Source§fn patch<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn patch<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Selfwhere
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Register a PATCH handler for a path, with extractor integration.
Source§fn get_routes(
&mut self,
path: &str,
method: HttpMethods,
) -> Option<&Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>
fn get_routes( &mut self, path: &str, method: HttpMethods, ) -> Option<&Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>
Retrieve the route handler for a given path/method, if one is registered. Read more
Source§fn add_route_with_extraction<F, HFut, P>(
&mut self,
method: HttpMethods,
path: &str,
handler: F,
)where
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
fn add_route_with_extraction<F, HFut, P>(
&mut self,
method: HttpMethods,
path: &str,
handler: F,
)where
F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static,
HFut: Future<Output = HttpResponse> + Send + 'static,
P: ExtractFromOwned + Send + 'static,
Internal helper: Register a handler using extractor integration. Read more
Auto Trait Implementations§
impl Freeze for Router
impl !RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more