Skip to main content

ResourceServices

Struct ResourceServices 

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

Source

pub fn route<R>(self, route: R) -> Self
where 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()),
        ])
);
Source

pub fn to<Args>(self, handler: impl Handler<St, Args>) -> Self
where Args: FromRequest<St> + 'static, Args::Error: WebResponseError<St, St::Error>,

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")
);
Source

pub fn to_with_state<Args>(self, handler: impl HandlerSt<St, Out, Args>) -> Self
where Args: FromRequest<St> + 'static, Args::Error: WebResponseError<St, St::Error>,

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

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

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 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>)

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<St, In, Out, M, F> UnsafeUnpin for ResourceServices<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.