pub struct ResourceServices<St: State, In, Out, M, F> { /* private fields */ }Expand description
A resource with routes or a configured fallback.
From here, you can add more routes or handlers and change the resource fallback. Configure resource-wide settings such as its name, guards, filters, and middleware before adding the first route or fallback.
The finished builder can be registered with App::service() or
Scope::service(). It keeps the path and all configuration from the
original Resource.
Implementations§
Source§impl<St, In, Out, M, F> ResourceServices<St, In, Out, M, F>where
St: State,
In: 'static,
Out: 'static,
M: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure>,
impl<St, In, Out, M, F> ResourceServices<St, In, Out, M, F>where
St: State,
In: 'static,
Out: 'static,
M: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure>,
Sourcepub fn route<R>(self, route: R) -> Selfwhere
R: IntoRoutes<St, Out>,
pub fn route<R>(self, route: R) -> Selfwhere
R: IntoRoutes<St, Out>,
Add one or more routes to this resource.
Routes are checked in registration order after the resource path and
resource guards match. The first route whose method and custom guards
accept the request is called. If no route matches, the resource’s
default service is used; without a custom default, it returns
405 Method Not Allowed.
A single Route or a collection of routes can be supplied.
use ntex::web::{self, App, HttpResponse};
App::default().service(
web::resource("/items")
.route(web::get().to(async || "list"))
.route([
web::post().to(async || HttpResponse::Created()),
web::delete().to(async || HttpResponse::NoContent()),
])
);Sourcepub fn to<Args>(self, handler: impl Handler<St, Args>) -> Self
pub fn to<Args>(self, handler: impl Handler<St, Args>) -> Self
Add an unguarded route with a handler.
The route matches every request not accepted by an earlier route. Routes are checked in registration order, so this catch-all route should normally be added last; routes added after it cannot be selected.
The handler receives request extractor values and returns a type
implementing Responder.
use ntex::web::{self, App};
App::default().service(
web::resource("/items")
.route(web::get().to(async || "list"))
.to(async || "Unsupported request")
);Sourcepub fn to_with_state<Args>(self, handler: impl HandlerSt<St, Out, Args>) -> Self
pub fn to_with_state<Args>(self, handler: impl HandlerSt<St, Out, Args>) -> Self
Add a state-aware handler as a new route.
The route has no guards and matches any request not accepted by an earlier route. The handler receives a shared reference to the application state, followed by the current request state and any request extractors.
use ntex::web;
struct AppState;
impl web::State for AppState {
type Error = web::DefaultError;
}
async fn fallback(
_state: &AppState,
_request_state: (),
req: web::HttpRequest,
) -> String {
format!("No route for {}", req.path())
}
web::App::<AppState>::new().service(
web::resource("/")
.route(web::get().to(async || "GET"))
.to_with_state(fallback)
);Sourcepub fn default_service<S>(
self,
f: impl IntoServiceFactory<S, St, WebRequest<Out>>,
) -> Selfwhere
S: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static,
S::Error: WebResponseError<St, St::Error>,
S::InitError: IntoFailure,
pub fn default_service<S>(
self,
f: impl IntoServiceFactory<S, St, WebRequest<Out>>,
) -> Selfwhere
S: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static,
S::Error: WebResponseError<St, St::Error>,
S::InitError: IntoFailure,
Set the fallback service for this resource.
The fallback is called after the resource path and guards match but none
of its routes match, commonly because the request method is unsupported.
Without a custom fallback, the resource returns
405 Method Not Allowed. It does not delegate to a scope or application
fallback.
use ntex::web::{self, App, HttpResponse};
App::default().service(
web::resource("/items")
.route(web::get().to(async || "items"))
.default_service(web::to(async || {
HttpResponse::MethodNotAllowed().body("Use GET")
}))
);Trait Implementations§
Source§impl<St: State, In, Out, M, F> Debug for ResourceServices<St, In, Out, M, F>
impl<St: State, In, Out, M, F> Debug for ResourceServices<St, In, Out, M, F>
Source§impl<St, Outer, In, Out, M, F> WebServiceFactory<St, Outer> for ResourceServices<St, In, Out, M, F>where
St: State,
Outer: 'static,
In: 'static,
Out: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure> + 'static,
M: Middleware<ResourceService<St, In, Out, F::Service>, St> + 'static,
M::Service: Service<St, WebRequest<Outer>, Res = WebResponse, Error = WebError<St, St::Error>>,
impl<St, Outer, In, Out, M, F> WebServiceFactory<St, Outer> for ResourceServices<St, In, Out, M, F>where
St: State,
Outer: 'static,
In: 'static,
Out: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure> + 'static,
M: Middleware<ResourceService<St, In, Out, F::Service>, St> + 'static,
M::Service: Service<St, WebRequest<Outer>, Res = WebResponse, Error = WebError<St, St::Error>>,
Source§fn register(self, config: &mut WebServiceConfig<St, Outer>)
fn register(self, config: &mut WebServiceConfig<St, Outer>)
config.