[−][src]Struct actix_web::Resource
Resource is an entry in resources table which corresponds to requested URL.
Resource in turn has at least one route.
Route consists of an handlers objects and list of guards
(objects that implement Guard
trait).
Resources and rouets uses builder-like pattern for configuration.
During request handling, resource object iterate through all routes
and check guards for specific route, if request matches all
guards, route considered matched and route handler get called.
use actix_web::{web, App, HttpResponse}; fn main() { let app = App::new().service( web::resource("/") .route(web::get().to(|| HttpResponse::Ok()))); }
If no matching route could be found, 405 response code get returned.
Default behavior could be overriden with default_resource()
method.
Methods
impl<P> Resource<P>
[src]
impl<P, T> Resource<P, T> where
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
[src]
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
pub fn name(self, name: &str) -> Self
[src]
Set resource name.
Name is used for url generation.
pub fn guard<G: Guard + 'static>(self, guard: G) -> Self
[src]
Add match guard to a resource.
use actix_web::{web, guard, App, HttpResponse}; fn index(data: web::Path<(String, String)>) -> &'static str { "Welcome!" } fn main() { let app = App::new() .service( web::resource("/app") .guard(guard::Header("content-type", "text/plain")) .route(web::get().to(index)) ) .service( web::resource("/app") .guard(guard::Header("content-type", "text/json")) .route(web::get().to(|| HttpResponse::MethodNotAllowed())) ); }
pub fn route(self, route: Route<P>) -> Self
[src]
Register a new route.
use actix_web::{web, guard, App, HttpResponse}; fn main() { let app = App::new().service( web::resource("/").route( web::route() .guard(guard::Any(guard::Get()).or(guard::Put())) .guard(guard::Header("Content-Type", "text/plain")) .to(|| HttpResponse::Ok())) ); }
Multiple routes could be added to a resource. Resource object uses match guards for route selection.
use actix_web::{web, guard, App, HttpResponse}; fn main() { let app = App::new().service( web::resource("/container/") .route(web::get().to(get_handler)) .route(web::post().to(post_handler)) .route(web::delete().to(delete_handler)) ); }
pub fn to<F, I, R>(self, handler: F) -> Self where
F: Factory<I, R> + 'static,
I: FromRequest<P> + 'static,
R: Responder + 'static,
[src]
F: Factory<I, R> + 'static,
I: FromRequest<P> + 'static,
R: Responder + 'static,
Register a new route and add handler. This route matches all requests.
use actix_web::*; fn index(req: HttpRequest) -> HttpResponse { unimplemented!() } App::new().service(web::resource("/").to(index));
This is shortcut for:
App::new().service(web::resource("/").route(web::route().to(index)));
pub fn to_async<F, I, R>(self, handler: F) -> Self where
F: AsyncFactory<I, R>,
I: FromRequest<P> + 'static,
R: IntoFuture + 'static,
R::Item: Into<Response>,
R::Error: Into<Error>,
[src]
F: AsyncFactory<I, R>,
I: FromRequest<P> + 'static,
R: IntoFuture + 'static,
R::Item: Into<Response>,
R::Error: Into<Error>,
Register a new route and add async handler.
use actix_web::*; use futures::future::{ok, Future}; fn index(req: HttpRequest) -> impl Future<Item=HttpResponse, Error=Error> { ok(HttpResponse::Ok().finish()) } App::new().service(web::resource("/").to_async(index));
This is shortcut for:
App::new().service(web::resource("/").route(web::route().to_async(index)));
pub fn wrap<M, F>(
self,
mw: F
) -> Resource<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
F: IntoTransform<M, T::Service>,
[src]
self,
mw: F
) -> Resource<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
F: IntoTransform<M, T::Service>,
Register a resource middleware.
This is similar to App's
middlewares, but middleware get invoked on resource level.
Resource level middlewares are not allowed to change response
type (i.e modify response's body).
Note: middlewares get called in opposite order of middlewares registration.
pub fn wrap_fn<F, R>(
self,
mw: F
) -> Resource<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
R: IntoFuture<Item = ServiceResponse, Error = Error>,
[src]
self,
mw: F
) -> Resource<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
R: IntoFuture<Item = ServiceResponse, Error = Error>,
Register a resource middleware function.
This function accepts instance of ServiceRequest
type and
mutable reference to the next middleware in chain.
This is similar to App's
middlewares, but middleware get invoked on resource level.
Resource level middlewares are not allowed to change response
type (i.e modify response's body).
use actix_service::Service; use actix_web::{web, App}; use actix_web::http::{header::CONTENT_TYPE, HeaderValue}; fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::resource("/index.html") .wrap_fn(|req, srv| srv.call(req).map(|mut res| { res.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("text/plain"), ); res })) .route(web::get().to(index))); }
pub fn default_resource<F, R, U>(self, f: F) -> Self where
F: FnOnce(Resource<P>) -> R,
R: IntoNewService<U>,
U: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error> + 'static,
[src]
F: FnOnce(Resource<P>) -> R,
R: IntoNewService<U>,
U: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error> + 'static,
Default resource to be used if no matching route could be found.
By default 405 response get returned. Resource does not use
default handler from App
or Scope
.
Trait Implementations
impl<P, T> HttpServiceFactory<P> for Resource<P, T> where
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
[src]
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
fn register(self, config: &mut ServiceConfig<P>)
[src]
impl<P, T> IntoNewService<T, ()> for Resource<P, T> where
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
[src]
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
fn into_new_service(self) -> T
[src]
Auto Trait Implementations
impl<P, T = ResourceEndpoint<P>> !Send for Resource<P, T>
impl<P, T = ResourceEndpoint<P>> !Sync for Resource<P, T>
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,