[][src]Struct actix_web::Resource

pub struct Resource<T = ResourceEndpoint> { /* fields omitted */ }

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 Resource[src]

pub fn new(path: &str) -> Resource[src]

impl<T> Resource<T> where
    T: NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>, 
[src]

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) -> 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 + 'static,
    R: Responder + 'static, 
[src]

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 + 'static,
    R: IntoFuture + 'static,
    R::Item: Into<Response>,
    R::Error: Into<Error>, 
[src]

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<impl NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
    M: Transform<T::Service, Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
    F: IntoTransform<M, T::Service>, 
[src]

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<impl NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
    F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
    R: IntoFuture<Item = ServiceResponse, Error = Error>, 
[src]

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_service<F, U>(self, f: F) -> Self where
    F: IntoNewService<U>,
    U: NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
    U::InitError: Debug
[src]

Default service 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<T> HttpServiceFactory for Resource<T> where
    T: NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()> + 'static, 
[src]

impl<T> IntoNewService<T, ()> for Resource<T> where
    T: NewService<Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>, 
[src]

Auto Trait Implementations

impl<T = ResourceEndpoint> !Send for Resource<T>

impl<T = ResourceEndpoint> !Sync for Resource<T>

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Erased for T