Struct ntex::web::Resource [−][src]
pub struct Resource<Err: ErrorRenderer, M = Identity, T = Filter<Err>> { /* fields omitted */ }
Expand description
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 routes 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 ntex::web::{self, App, HttpResponse};
fn main() {
let app = App::new().service(
web::resource("/")
.route(web::get().to(|| async { HttpResponse::Ok() })));
}
If no matching route could be found, 405 response code get returned.
Default behavior could be overriden with default_resource()
method.
Implementations
impl<Err, M, T> Resource<Err, M, T> where
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
impl<Err, M, T> Resource<Err, M, T> where
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
Add match guard to a resource.
use ntex::web::{self, guard, App, HttpResponse};
async fn index(data: web::types::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(|| async { HttpResponse::MethodNotAllowed() }))
);
}
Register a new route.
use ntex::web::{self, 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(|| async { HttpResponse::Ok() }))
);
}
Multiple routes could be added to a resource. Resource object uses match guards for route selection.
use ntex::web::{self, guard, App};
fn main() {
let app = App::new().service(
web::resource("/container/")
.route([
web::get().to(get_handler),
web::post().to(post_handler),
web::delete().to(delete_handler)
])
);
}
Provide resource specific data. This method allows to add extractor
configuration or specific state available via Data<T>
extractor.
Provided data is available for all routes registered for the current resource.
Resource data overrides data registered by App::data()
method.
use ntex::web::{self, App, FromRequest};
/// extract text data from request
async fn index(body: String) -> String {
format!("Body {}!", body)
}
fn main() {
let app = App::new().service(
web::resource("/index.html")
// limit size of the payload
.app_data(web::types::PayloadConfig::new(4096))
.route(
web::get()
// register handler
.to(index)
));
}
Set or override application data.
This method overrides data stored with App::app_data()
Register a new route and add handler. This route matches all requests.
use ntex::web::{self, App, HttpRequest, HttpResponse};
async 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 filter<U, F>(
self,
filter: F
) -> Resource<Err, M, impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>> where
U: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
F: IntoServiceFactory<U>,
pub fn filter<U, F>(
self,
filter: F
) -> Resource<Err, M, impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>> where
U: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
F: IntoServiceFactory<U>,
Register request filter.
This is similar to App's
filters, but filter get invoked on resource level.
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 default_service<F, S>(self, f: F) -> Self where
F: IntoServiceFactory<S>,
S: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
S::InitError: Debug,
pub fn default_service<F, S>(self, f: F) -> Self where
F: IntoServiceFactory<S>,
S: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
S::InitError: Debug,
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<Err, M, T> WebServiceFactory<Err> for Resource<Err, M, T> where
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()> + 'static,
M: Transform<ResourceService<T::Service, Err>> + 'static,
M::Service: Service<Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container>,
Err: ErrorRenderer,
impl<Err, M, T> WebServiceFactory<Err> for Resource<Err, M, T> where
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()> + 'static,
M: Transform<ResourceService<T::Service, Err>> + 'static,
M::Service: Service<Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container>,
Err: ErrorRenderer,
Auto Trait Implementations
impl<Err, M = Identity, T = Filter<Err>> !RefUnwindSafe for Resource<Err, M, T>
impl<Err, M = Identity, T = Filter<Err>> !UnwindSafe for Resource<Err, M, T>
Blanket Implementations
Mutably borrows from an owned value. Read more
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