pub struct Router { /* private fields */ }Expand description
Router.
Routers allow to scope specific actions to a combination of HTTP methods
and path patterns, making them essentially a specialization of Stack.
Additionally, routers allow for the addition of middlewares, which are
grouped into stacks, and can be defined before and after routes.
Implementations§
Source§impl Router
impl Router
Sourcepub fn new<P>(path: P) -> Self
pub fn new<P>(path: P) -> Self
Creates a router.
The given path is prepended to all routes that are created as part of
the router. Using Router::default is equivalent to passing /.
§Examples
use zapient::router::Router;
// Create router
let router = Router::new("/");Sourcepub fn get<P, A>(self, path: P, action: A) -> Self
pub fn get<P, A>(self, path: P, action: A) -> Self
Adds a GET route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.get("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn post<P, A>(self, path: P, action: A) -> Self
pub fn post<P, A>(self, path: P, action: A) -> Self
Adds a POST route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.post("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn put<P, A>(self, path: P, action: A) -> Self
pub fn put<P, A>(self, path: P, action: A) -> Self
Adds a PUT route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.put("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn delete<P, A>(self, path: P, action: A) -> Self
pub fn delete<P, A>(self, path: P, action: A) -> Self
Adds a DELETE route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.delete("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn patch<P, A>(self, path: P, action: A) -> Self
pub fn patch<P, A>(self, path: P, action: A) -> Self
Adds a PATCH route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.patch("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn head<P, A>(self, path: P, action: A) -> Self
pub fn head<P, A>(self, path: P, action: A) -> Self
Adds a HEAD route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.head("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn options<P, A>(self, path: P, action: A) -> Self
pub fn options<P, A>(self, path: P, action: A) -> Self
Adds a OPTIONS route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.options("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn trace<P, A>(self, path: P, action: A) -> Self
pub fn trace<P, A>(self, path: P, action: A) -> Self
Adds a TRACE route to the router.
§Examples
use zapient::http::{Request, Response};
use zapient::router::{Router, Params};
// Create router and add route
let router = Router::default()
.trace("/", |req: Request, params: Params| {
Response::default()
});Sourcepub fn with<M>(self, middleware: M) -> Selfwhere
M: TryIntoMiddleware,
pub fn with<M>(self, middleware: M) -> Selfwhere
M: TryIntoMiddleware,
Adds a middleware to the router.
Middlewares can be added at any point in the router stack, including before or after routes. This allows for flexible routing and middleware combinations, as routes are themselves combines into middlewares, when the router is converted into a middleware.
Anything that can be converted into a Middleware can be added to
the stack, including middlewares, routers, stacks and closures.
§Errors
Errors returned by TryIntoMiddleware are passed through.
§Examples
use zapient::handler::Handler;
use zapient::http::{Method, Request, Response, Status};
use zapient::router::Router;
// Create router with middleware
let stack = Router::default()
.with(|req: Request, next: &dyn Handler| {
if req.method == Method::Get && req.uri.path == "/coffee" {
Response::new().status(Status::ImATeapot)
} else {
next.handle(req)
}
});Trait Implementations§
Source§impl TryIntoHandler for Router
impl TryIntoHandler for Router
Source§fn try_into_handler(self) -> Result<Self::Output>
fn try_into_handler(self) -> Result<Self::Output>
Attempts to convert the router into a handler.
This method is equivalent to calling Router::try_into_middleware
with Scope::default, scoping all middlewares and routes to /.
§Errors
In case conversion fails, an Error is returned.
§Examples
use zapient::handler::TryIntoHandler;
use zapient::http::{Request, Response, Status};
use zapient::router::{Router, Params};
// Create router and convert into handler
let router = Router::default()
.get("/coffee", |req: Request, params: Params| {
Response::new().status(Status::ImATeapot)
})
.try_into_handler()?;Source§impl TryIntoMiddleware for Router
impl TryIntoMiddleware for Router
Source§fn try_into_middleware(self, scope: &Scope) -> Result<Self::Output>
fn try_into_middleware(self, scope: &Scope) -> Result<Self::Output>
Attempts to convert the router into a middleware.
§Errors
In case conversion fails, an Error is returned.
§Examples
use zapient::handler::Scope;
use zapient::http::{Request, Response, Status};
use zapient::middleware::TryIntoMiddleware;
use zapient::router::{Router, Params};
// Create scope
let scope = Scope::default();
// Create router and convert into middleware
let router = Router::default()
.get("/coffee", |req: Request, params: Params| {
Response::new().status(Status::ImATeapot)
})
.try_into_middleware(&scope)?;