pub struct Route { /* private fields */ }Expand description
A request handler with guards.
Route uses a builder-like pattern for configuration. If handler is not set, a 404 Not Found
handler is used.
Implementations§
Source§impl Route
impl Route
Sourcepub fn wrap<M, B>(self, mw: M) -> Routewhere
M: Transform<BoxService<ServiceRequest, ServiceResponse, Error>, ServiceRequest, Response = ServiceResponse<B>, Error = Error, InitError = ()> + 'static,
B: MessageBody + 'static,
pub fn wrap<M, B>(self, mw: M) -> Routewhere
M: Transform<BoxService<ServiceRequest, ServiceResponse, Error>, ServiceRequest, Response = ServiceResponse<B>, Error = Error, InitError = ()> + 'static,
B: MessageBody + 'static,
Registers a route middleware.
mw is a middleware component (type), that can modify the requests and responses handled by
this Route.
This middleware wraps the currently configured route service. Call this method after
Route::to or Route::service so the middleware is applied to the final handler.
§Examples
web::get()
.to(|| async { HttpResponse::Ok() })
.wrap(middleware::Logger::default());See App::wrap for more details.
Source§impl Route
impl Route
Sourcepub fn method(self, method: Method) -> Self
pub fn method(self, method: Method) -> Self
Add method guard to the route.
§Examples
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()))
);Sourcepub fn guard<F: Guard + 'static>(self, f: F) -> Self
pub fn guard<F: Guard + 'static>(self, f: F) -> Self
Add guard to the route.
§Examples
App::new().service(web::resource("/path").route(
web::route()
.guard(guard::Get())
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| HttpResponse::Ok()))
);Sourcepub fn to<F, Args>(self, handler: F) -> Self
pub fn to<F, Args>(self, handler: F) -> Self
Set handler function, use request extractors for parameters.
§Examples
use actix_web::{web, http, App};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(info: web::Path<Info>) -> String {
format!("Welcome {}!", info.username)
}
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)
}
let app = App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index))
);§Panics
Panics if called after Route::wrap, since this would replace the wrapped service and
silently discard middleware.
Sourcepub fn service<S, E>(self, service_factory: S) -> Selfwhere
S: ServiceFactory<ServiceRequest, Response = ServiceResponse, Error = E, InitError = (), Config = ()> + 'static,
E: Into<Error> + 'static,
pub fn service<S, E>(self, service_factory: S) -> Selfwhere
S: ServiceFactory<ServiceRequest, Response = ServiceResponse, Error = E, InitError = (), Config = ()> + 'static,
E: Into<Error> + 'static,
Set raw service to be constructed and called as the request handler.
§Examples
struct HelloWorld;
impl Service<ServiceRequest> for HelloWorld {
type Response = ServiceResponse;
type Error = Infallible;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
dev::always_ready!();
fn call(&self, req: ServiceRequest) -> Self::Future {
let (req, _) = req.into_parts();
let res = HttpResponse::Ok()
.insert_header(header::ContentType::plaintext())
.body("Hello world!");
Box::pin(async move { Ok(ServiceResponse::new(req, res)) })
}
}
App::new().route(
"/",
web::get().service(fn_factory(|| async { Ok(HelloWorld) })),
);§Panics
Panics if called after Route::wrap, since this would replace the wrapped service and
silently discard middleware.
Trait Implementations§
Source§impl ServiceFactory<ServiceRequest> for Route
impl ServiceFactory<ServiceRequest> for Route
Source§type Response = ServiceResponse
type Response = ServiceResponse
Source§type Future = Pin<Box<dyn Future<Output = Result<<Route as ServiceFactory<ServiceRequest>>::Service, <Route as ServiceFactory<ServiceRequest>>::InitError>>>>
type Future = Pin<Box<dyn Future<Output = Result<<Route as ServiceFactory<ServiceRequest>>::Service, <Route as ServiceFactory<ServiceRequest>>::InitError>>>>
Service instance.gSource§fn new_service(&self, _: ()) -> Self::Future
fn new_service(&self, _: ()) -> Self::Future
Auto Trait Implementations§
impl !RefUnwindSafe for Route
impl !Send for Route
impl !Sync for Route
impl !UnwindSafe for Route
impl Freeze for Route
impl Unpin for Route
impl UnsafeUnpin for Route
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
Source§fn into_factory(self) -> SF
fn into_factory(self) -> SF
Self to a ServiceFactory