pub struct ScopeServices<St: State, In, Out, M, F> { /* private fields */ }Expand description
A scope with nested services or a configured fallback.
Methods such as Scope::service(), Scope::route(),
Scope::configure(), and Scope::default_service() turn a Scope
into ScopeServices. You can then add more services or routes and adjust
the scope fallback.
Implementations§
Source§impl<St, In, Out, M, F> ScopeServices<St, In, Out, M, F>where
St: State,
In: 'static,
Out: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure>,
impl<St, In, Out, M, F> ScopeServices<St, In, Out, M, F>where
St: State,
In: 'static,
Out: 'static,
F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure>,
Sourcepub fn service(self, factory: impl WebServiceFactory<St, Out>) -> Self
pub fn service(self, factory: impl WebServiceFactory<St, Out>) -> Self
Registers another web service inside this scope.
This has the same behavior as Scope::service(). The service’s path
is joined with the scope prefix and added to the scope’s nested router.
If no nested service matches, the scope’s default service is used.
Sourcepub fn route(self, path: &str, route: Route<St, Out>) -> Self
pub fn route(self, path: &str, route: Route<St, Out>) -> Self
Register a route for a path relative to this scope.
This is shorthand for creating a Resource with one route and
registering it with ScopeServices::service(). The route’s method and
custom guards are promoted to resource guards.
Each call creates a separate resource, so the same relative path can be registered more than once with different guards.
use ntex::web::{self, App, HttpResponse};
App::default().service(
web::scope("/api")
.route("/items", web::get().to(async || "list"))
.route(
"/items",
web::post().to(async || HttpResponse::Created()),
)
);Sourcepub fn default_service<Sf>(
self,
f: impl IntoServiceFactory<Sf, St, WebRequest<Out>>,
) -> Selfwhere
Sf: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static,
Sf::Error: WebResponseError<St, St::Error>,
Sf::InitError: IntoFailure,
pub fn default_service<Sf>(
self,
f: impl IntoServiceFactory<Sf, St, WebRequest<Out>>,
) -> Selfwhere
Sf: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static,
Sf::Error: WebResponseError<St, St::Error>,
Sf::InitError: IntoFailure,
Set the fallback service for unmatched requests within this scope.
The fallback is called after the scope prefix and guards match but no
nested resource matches. Without a custom fallback, the scope returns
404 Not Found; it does not delegate to the application’s fallback.
Routing failures inside a matched resource are handled by that
resource’s fallback instead.
use ntex::web::{self, App, HttpRequest, HttpResponse};
async fn not_found(req: HttpRequest) -> HttpResponse {
HttpResponse::NotFound()
.body(format!("No API resource for {}", req.path()))
}
App::default().service(
web::scope("/api")
.route("/health", web::get().to(async || "ready"))
.default_service(web::to(not_found))
);Trait Implementations§
Source§impl<St: State, In, Out, M, F> Debug for ScopeServices<St, In, Out, M, F>
impl<St: State, In, Out, M, F> Debug for ScopeServices<St, In, Out, M, F>
Source§impl<St, Outer, In, Out, M, F> WebServiceFactory<St, Outer> for ScopeServices<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<WebServiceRouter<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 ScopeServices<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<WebServiceRouter<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.