pub trait ResourceController:
Send
+ Sync
+ 'static {
// Provided methods
fn index<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn create<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn store<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn show<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn edit<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn update<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn destroy<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn middleware(&self) -> ControllerMiddleware { ... }
}Expand description
A controller serving a RESTful resource.
Implement the actions the resource supports; the rest answer 405.
use rainier_http::{Request, Response};
use rainier_routing::{ResourceController, Router};
use std::sync::Arc;
struct PostController;
#[async_trait::async_trait]
impl ResourceController for PostController {
async fn index(&self, _request: Request) -> Response {
Response::text("every post")
}
async fn show(&self, request: Request) -> Response {
Response::text(format!("post {}", request.route_param("post").unwrap_or("?")))
}
}
let mut router = Router::new();
router.resource("posts", Arc::new(PostController));
assert_eq!(router.uri_for("posts.show"), Some("/posts/{post}"));Provided Methods§
Sourcefn index<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn index<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
GET /resource
Sourcefn create<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn create<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
GET /resource/create
Sourcefn store<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn store<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
POST /resource
Sourcefn show<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn show<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
GET /resource/{id}
Sourcefn edit<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn edit<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
GET /resource/{id}/edit
Sourcefn update<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn update<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
PUT|PATCH /resource/{id}
Sourcefn destroy<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn destroy<'life0, 'async_trait>(
&'life0 self,
request: Request,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
DELETE /resource/{id}
Sourcefn middleware(&self) -> ControllerMiddleware
fn middleware(&self) -> ControllerMiddleware
Middleware this controller applies to its own actions.
Runs inside anything the route’s group applied and outside the action itself, so a group’s session middleware still wraps a controller’s authorisation check.
ⓘ
fn middleware(&self) -> ControllerMiddleware {
ControllerMiddleware::new()
.except([ResourceAction::Index, ResourceAction::Show], Authenticate::new(auth))
}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".