pub struct Resource<S = ()> { /* 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 {});
let app = Router::new().merge(users);
Implementations§
Source§impl<S> Resource<S>
impl<S> Resource<S>
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) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn index<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}
.
Sourcepub fn create<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn create<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at POST /{resource_name}
.
Sourcepub fn new<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn new<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/new
.
Sourcepub fn show<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn show<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id
.
Sourcepub fn edit<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn edit<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id/edit
.
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Resource<S>
impl<S = ()> !RefUnwindSafe for Resource<S>
impl<S> Send for Resource<S>
impl<S> Sync for Resource<S>
impl<S> Unpin for Resource<S>
impl<S = ()> !UnwindSafe for Resource<S>
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