Struct actix_web::Route [−][src]
pub struct Route { /* fields omitted */ }Expand description
Resource route definition
Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.
Implementations
Add method guard to the route.
App::new().service(web::resource("/path").route(
web::get()
.method(http::Method::CONNECT)
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| HttpResponse::Ok()))
);Add guard to the route.
App::new().service(web::resource("/path").route(
web::route()
.guard(guard::Get())
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| HttpResponse::Ok()))
);pub fn to<F, T, R, U>(self, handler: F) -> Self where
F: Factory<T, R, U>,
T: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
pub fn to<F, T, R, U>(self, handler: F) -> Self where
F: Factory<T, R, U>,
T: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
Set handler function, use request extractors for parameters.
use actix_web::{web, http, App};
use serde_derive::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(info: web::Path<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let app = App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- register handler
);
}It is possible to use multiple extractors for one handler function.
use actix_web::{web, App};
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(path: web::Path<Info>, query: web::Query<HashMap<String, String>>, body: web::Json<Info>) -> String {
format!("Welcome {}!", path.username)
}
fn main() {
let app = App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index))
);
}Trait Implementations
type Request = ServiceRequest
type Request = ServiceRequest
Requests handled by the created services.
type Response = ServiceResponse
type Response = ServiceResponse
Responses given by the created services.
type Service = RouteService
type Service = RouteService
The kind of Service created by this factory.
type Future = CreateRouteService
type Future = CreateRouteService
The future of the Service instance.
Create and return a new service asynchronously.
Map this service’s output to a different type, returning a new service of the resulting type. Read more
Map this service’s error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E> where
F: Fn(Self::InitError) -> E + Clone,
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E> where
F: Fn(Self::InitError) -> E + Clone,
Map this factory’s init error to a different error, returning a new service.
Auto Trait Implementations
impl !RefUnwindSafe for Route
impl !UnwindSafe for Route
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span, returning an
Instrumented wrapper. Read more
Convert Self to a ServiceFactory
pub fn vzip(self) -> V
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more
