Skip to main content

ScopeServices

Struct ScopeServices 

Source
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>,

Source

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.

Source

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()),
        )
);
Source

pub fn default_service<Sf>( self, f: impl IntoServiceFactory<Sf, St, WebRequest<Out>>, ) -> Self
where 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>

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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>>,

Source§

fn register(self, config: &mut WebServiceConfig<St, Outer>)

Register the service, its path, and its guards in config.

Auto Trait Implementations§

§

impl<St, In, Out, M, F> !RefUnwindSafe for ScopeServices<St, In, Out, M, F>

§

impl<St, In, Out, M, F> !Send for ScopeServices<St, In, Out, M, F>

§

impl<St, In, Out, M, F> !Sync for ScopeServices<St, In, Out, M, F>

§

impl<St, In, Out, M, F> !UnwindSafe for ScopeServices<St, In, Out, M, F>

§

impl<St, In, Out, M, F> Freeze for ScopeServices<St, In, Out, M, F>
where M: Freeze, F: Freeze,

§

impl<St, In, Out, M, F> Unpin for ScopeServices<St, In, Out, M, F>
where M: Unpin, F: Unpin, St: Unpin, In: Unpin,

§

impl<St, In, Out, M, F> UnsafeUnpin for ScopeServices<St, In, Out, M, F>
where M: UnsafeUnpin, F: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.