pub struct Resource<S = ()> { /* private fields */ }
Expand description
Resource is an entry in route table which corresponds to requested URL.
Resource in turn has at least one route.
Route consists of an object that implements Handler
trait (handler)
and list of predicates (objects that implement Predicate
trait).
Route uses builder-like pattern for configuration.
During request handling, resource object iterate through all routes
and check all predicates for specific route, if request matches all
predicates route route considered matched and route handler get called.
use actix_web::{App, HttpResponse, http};
fn main() {
let app = App::new()
.resource(
"/", |r| r.method(http::Method::GET).f(|r| HttpResponse::Ok()))
.finish();
}
Implementations
sourceimpl<S> Resource<S>
impl<S> Resource<S>
sourcepub fn new(rdef: ResourceDef) -> Self
pub fn new(rdef: ResourceDef) -> Self
Create new resource with specified resource definition
sourcepub fn rdef(&self) -> &ResourceDef
pub fn rdef(&self) -> &ResourceDef
Resource definition
sourceimpl<S: 'static> Resource<S>
impl<S: 'static> Resource<S>
sourcepub fn route(&mut self) -> &mut Route<S>
pub fn route(&mut self) -> &mut Route<S>
Register a new route and return mutable reference to Route object. Route is used for route configuration, i.e. adding predicates, setting up handler.
use actix_web::*;
fn main() {
let app = App::new()
.resource("/", |r| {
r.route()
.filter(pred::Any(pred::Get()).or(pred::Put()))
.filter(pred::Header("Content-Type", "text/plain"))
.f(|r| HttpResponse::Ok())
})
.finish();
}
sourcepub fn method(&mut self, method: Method) -> &mut Route<S>
pub fn method(&mut self, method: Method) -> &mut Route<S>
Register a new route and add method check to route.
use actix_web::*;
fn index(req: &HttpRequest) -> HttpResponse { unimplemented!() }
App::new().resource("/", |r| r.method(http::Method::GET).f(index));
This is shortcut for:
App::new().resource("/", |r| r.route().filter(pred::Get()).f(index));
sourcepub fn h<H: Handler<S>>(&mut self, handler: H)
pub fn h<H: Handler<S>>(&mut self, handler: H)
Register a new route and add handler object.
use actix_web::*;
fn handler(req: &HttpRequest) -> HttpResponse { unimplemented!() }
App::new().resource("/", |r| r.h(handler));
This is shortcut for:
App::new().resource("/", |r| r.route().h(handler));
sourcepub fn f<F, R>(&mut self, handler: F)where
F: Fn(&HttpRequest<S>) -> R + 'static,
R: Responder + 'static,
pub fn f<F, R>(&mut self, handler: F)where
F: Fn(&HttpRequest<S>) -> R + 'static,
R: Responder + 'static,
Register a new route and add handler function.
use actix_web::*;
fn index(req: &HttpRequest) -> HttpResponse { unimplemented!() }
App::new().resource("/", |r| r.f(index));
This is shortcut for:
App::new().resource("/", |r| r.route().f(index));
sourcepub fn with<T, F, R>(&mut self, handler: F)where
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
pub fn with<T, F, R>(&mut self, handler: F)where
F: WithFactory<T, S, R>,
R: Responder + 'static,
T: FromRequest<S> + 'static,
Register a new route and add handler.
use actix_web::*;
fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
App::new().resource("/", |r| r.with(index));
This is shortcut for:
App::new().resource("/", |r| r.route().with(index));
sourcepub fn with_async<T, F, R, I, E>(&mut self, handler: F)where
F: Fn(T) -> R + 'static,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
pub fn with_async<T, F, R, I, E>(&mut self, handler: F)where
F: Fn(T) -> R + 'static,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
Register a new route and add async handler.
use actix_web::*;
use futures::future::Future;
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
unimplemented!()
}
App::new().resource("/", |r| r.with_async(index));
This is shortcut for:
App::new().resource("/", |r| r.route().with_async(index));
sourcepub fn middleware<M: Middleware<S>>(&mut self, mw: M)
pub fn middleware<M: Middleware<S>>(&mut self, mw: M)
Register a resource middleware
This is similar to App's
middlewares, but
middlewares get invoked on resource level.
Note Middleware::finish()
fires right after response get
prepared. It does not wait until body get sent to peer.