Skip to main content

ResourceController

Trait ResourceController 

Source
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§

Source

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

Source

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

Source

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

Source

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}

Source

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

Source

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}

Source

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}

Source

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".

Implementors§