pub trait Controller {
// Required methods
fn is_matching(request: &Request, connection: &ConnectionInfo) -> bool;
fn process(
request: &Request,
response: Response,
connection: &ConnectionInfo,
) -> Response;
}Expand description
Core routing and handling trait. Implement this to add a route to the server.
§Example
use rust_web_server::controller::Controller;
use rust_web_server::request::{METHOD, Request};
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
use rust_web_server::range::Range;
use rust_web_server::mime_type::MimeType;
use rust_web_server::server::ConnectionInfo;
pub struct HelloController;
impl Controller for HelloController {
fn is_matching(request: &Request, _: &ConnectionInfo) -> bool {
request.method == METHOD.get && request.request_uri == "/hello"
}
fn process(_: &Request, mut response: Response, _: &ConnectionInfo) -> Response {
response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
response.content_range_list = vec![
Range::get_content_range(b"Hello!".to_vec(), MimeType::TEXT_PLAIN.to_string())
];
response
}
}Required Methods§
Sourcefn is_matching(request: &Request, connection: &ConnectionInfo) -> bool
fn is_matching(request: &Request, connection: &ConnectionInfo) -> bool
Returns true if this controller should handle the given request.
Called in declaration order; the first match wins.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".