Struct axum_extra::routing::Resource
source · [−]pub struct Resource<B = Body> { /* private fields */ }
Expand description
A resource which defines a set of conventional CRUD routes.
Example
use axum::{Router, routing::get, extract::Path};
use axum_extra::routing::{RouterExt, Resource};
let users = Resource::named("users")
// Define a route for `GET /users`
.index(|| async {})
// `POST /users`
.create(|| async {})
// `GET /users/new`
.new(|| async {})
// `GET /users/:users_id`
.show(|Path(user_id): Path<u64>| async {})
// `GET /users/:users_id/edit`
.edit(|Path(user_id): Path<u64>| async {})
// `PUT or PATCH /users/:users_id`
.update(|Path(user_id): Path<u64>| async {})
// `DELETE /users/:users_id`
.destroy(|Path(user_id): Path<u64>| async {})
// Nest another router at the "member level"
// This defines a route for `GET /users/:users_id/tweets`
.nest(Router::new().route(
"/tweets",
get(|Path(user_id): Path<u64>| async {}),
))
// Nest another router at the "collection level"
// This defines a route for `GET /users/featured`
.nest_collection(
Router::new().route("/featured", get(|| async {})),
);
let app = Router::new().merge(users);
Implementations
sourceimpl<B> Resource<B> where
B: HttpBody + Send + 'static,
impl<B> Resource<B> where
B: HttpBody + Send + 'static,
sourcepub fn named(resource_name: &str) -> Self
pub fn named(resource_name: &str) -> Self
Create a Resource
with the given name.
All routes will be nested at /{resource_name}
.
sourcepub fn index<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn index<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at GET /{resource_name}
.
sourcepub fn create<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn create<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at POST /{resource_name}
.
sourcepub fn new<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn new<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at GET /{resource_name}/new
.
sourcepub fn show<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn show<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id
.
sourcepub fn edit<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn edit<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id/edit
.
sourcepub fn update<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn update<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at PUT or PATCH /resource_name/:{resource_name}_id
.
sourcepub fn destroy<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
pub fn destroy<H, T>(self, handler: H) -> Self where
H: Handler<T, B>,
T: 'static,
Add a handler at DELETE /{resource_name}/:{resource_name}_id
.
sourcepub fn nest<T>(self, svc: T) -> Self where
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
pub fn nest<T>(self, svc: T) -> Self where
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
Nest another route at the “member level”.
The routes will be nested at /{resource_name}/:{resource_name}_id
.
sourcepub fn nest_collection<T>(self, svc: T) -> Self where
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
pub fn nest_collection<T>(self, svc: T) -> Self where
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
Nest another route at the “collection level”.
The routes will be nested at /{resource_name}
.
Trait Implementations
Auto Trait Implementations
impl<B = Body> !RefUnwindSafe for Resource<B>
impl<B> Send for Resource<B>
impl<B = Body> !Sync for Resource<B>
impl<B> Unpin for Resource<B>
impl<B = Body> !UnwindSafe for Resource<B>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more