pub trait RestController: Controller {
type Resource: ToParameter;
// Provided methods
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn rest(self, path: &str) -> Handler
where Self: Sized + 'static { ... }
fn list<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn get<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 Self::Resource,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn create<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn update<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 Self::Resource,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn patch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 Self::Resource,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn delete<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 Request,
id: &'life2 Self::Resource,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}
Expand description
REST controller, which splits requests into 6 REST verbs.
This controller will split incoming requests based on the REST specification and route them to their respective methods.
Available methods are:
- list (
GET /path
) - create (
POST /path
) - get (
GET /path/:id
) - update (
PUT /path/:id
) - patch (
PATCH /path/:id
) - delete (
DELETE /path/:id
)
By default, all methods will respond with 501 - Not Implemented
. It’s up to the user
to implement each method according to their needs.
The :id
can be any value which implements the ToParameter
trait.
Common data types are implemented, e.g., i64
and String
.
§Example
use rwf::prelude::*;
#[derive(Default, macros::RestController)]
struct MyController;
#[async_trait]
impl RestController for MyController {
type Resource = i64;
async fn get(&self, request: &Request, id: &i64) -> Result<Response, Error> {
Ok(Response::new().html(format!("Hello, id #{}", id)))
}
}
Required Associated Types§
Sourcetype Resource: ToParameter
type Resource: ToParameter
Resource type used in the request.
Rust is a typed language, this makes handling IDs easier by specifying the expected data type. Inputs not matching this data type will be rejected.
Provided Methods§
Sourcefn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Figure out which method to call based on request method and path.
Sourcefn rest(self, path: &str) -> Handlerwhere
Self: Sized + 'static,
fn rest(self, path: &str) -> Handlerwhere
Self: Sized + 'static,
Get a route handler for this controller. Used when adding this controller to the server with a route mapping.
Use the rest!
macro instead, e.g.:
rest!("/path" => YourResetController)