pub trait PageController: Controller {
// Required method
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided methods
fn post<'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 { ... }
}
Expand description
A controller that splits GET and POST requests into two different methods.
Most web apps using templates would want to implement the PageController
which splits up GET
from POST
requests,
allowing to handle form submissions together with page rendering.
§Example
// Include all necessary types.
use rwf::prelude::*;
// Your controller.
#[derive(Default, macros::PageController)]
pub struct MyPage;
// Controllers are async and use the `async_trait` crate.
#[rwf::async_trait]
impl PageController for MyPage {
// Respond to a GET request.
async fn get(&self, request: &Request) -> Result<Response, Error> {
render!(request, "templates/my_page.html")
}
}
The macros::PageController
expands to this:
ⓘ
#[rwf::async_trait]
impl Controller for MyPage {
async fn handle(&self, request: &Request) -> Result<Response, Error> {
// Delegate request handling to the `PageController::handle` method.
PageController::handle(self, request)
}
}
This is required because of how Rust trait dynamic dispatch works. Rwf HTTP server only handles structs that implement the Controller
trait, so all controllers that implement
a descendant trait (e.g. PageController
) must also implement the supertrait (Controller
).
Required Methods§
Provided Methods§
Sourcefn post<'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 post<'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,
Respond to a POST
request to this controller.
By default, 405 - Method Not Allowed
is returned.