Skip to main content

AppServices

Struct AppServices 

Source
pub struct AppServices<St: State, In, Out, M, F> { /* private fields */ }
Expand description

An application builder that already has routing configuration.

You usually do not need to name this type. It is returned naturally after calling App::route(), App::service(), App::configure(), or App::default_service().

From here, you can add more routes and services or set the application fallback. Return the finished builder from the factory passed to web::server(). The AppServices::build() and AppServices::build_with() methods are available when you need to connect the application to a lower-level HTTP service yourself.

Implementations§

Source§

impl<St, In, Out, M, F> AppServices<St, In, Out, M, F>
where St: State, Out: 'static, F: ServiceFactory<St, WebRequest<In>, Res = WebRequest<Out>, Error = WebError<St, St::Error>, InitError = Failure>,

Source

pub fn route(self, path: &str, route: Route<St, Out>) -> Self

Register a route for an application path.

This is shorthand for creating a Resource with one route and registering it with AppServices::service(). The route’s method and custom guards are promoted to resource guards.

Consequently, if those guards reject a request, the generated resource does not match and the application router continues searching. If nothing else matches, the application default service is used. To use a resource-level fallback such as the built-in 405 Method Not Allowed, register an explicit Resource and add routes with Resource::route().

Each call creates a separate resource, so the same path can be registered more than once with different guards.

use ntex::web::{self, App, HttpResponse};

App::default()
    .route("/items", web::get().to(async || "list"))
    .route(
        "/items",
        web::post().to(async || HttpResponse::Created()),
    );
Source

pub fn service<S>(self, factory: S) -> Self
where S: WebServiceFactory<St, Out> + 'static,

Registers another web service with the application.

This has the same behavior as App::service(). The service supplies its own path and guards and becomes part of the application’s top-level router.

If none of the registered services match, the application’s default service is used.

Source

pub fn default_service<U>( self, f: impl IntoServiceFactory<U, St, WebRequest<Out>>, ) -> Self
where U: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static, U::Error: WebResponseError<St, St::Error>, U::InitError: IntoFailure,

Set the fallback service for unmatched application requests.

The fallback is called when no top-level resource or scope matches the request path and guards. Without a custom fallback, the application returns 404 Not Found.

A matched resource or scope handles its own routing failures, so its requests do not fall through to this service.

use ntex::web::{self, App, HttpRequest, HttpResponse};

async fn not_found(req: HttpRequest) -> HttpResponse {
    HttpResponse::NotFound()
        .body(format!("No resource for {}", req.path()))
}

App::default()
    .route("/health", web::get().to(async || "ready"))
    .default_service(web::to(not_found));
Source§

impl<St, In, Out, M, F> AppServices<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>, M: Middleware<WebServiceRouter<St, In, Out, F::Service>, St> + 'static, M::Service: Service<St, WebRequest<()>, Res = WebResponse, Error = WebError<St, St::Error>>,

Source

pub fn build( self, ) -> impl ServiceFactory<St, Request, Res = Response, Error = WebError<St, St::Error>, InitError = Failure>

Builds the application into a service factory.

The returned factory accepts Request values and uses application state supplied by the surrounding service pipeline. It can be passed to HttpService when building an HTTP server manually.

Applications passed to web::server() do not normally need an explicit call to build().

use ntex::web;

let factory = web::App::default()
    .route("/", web::get().to(async || "Hello"))
    .build();
Source§

impl<St, In, Out, M, F> AppServices<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>, M: Middleware<WebServiceRouter<St, In, Out, F::Service>, St> + 'static, M::Service: Service<St, WebRequest<()>, Res = WebResponse, Error = WebError<St, St::Error>>,

Source

pub fn build_with<Outer>( self, state: St, ) -> impl ServiceFactory<Outer, Request, Res = Response, Error = WebError<St, St::Error>, InitError = Failure>
where St: Clone,

Builds the application with a fixed application state.

Unlike AppServices::build(), which takes its application state from the surrounding service pipeline, this method stores state in the returned factory. Web handlers, filters, middleware, and services use this fixed state even when the outer pipeline uses a different state type.

use ntex::web;

#[derive(Clone)]
struct AppState {
    greeting: &'static str,
}

impl web::State for AppState {
    type Error = web::DefaultError;
}

async fn index(state: &AppState, _request_state: ()) -> String {
    state.greeting.to_owned()
}

let app = web::App::<AppState>::new()
    .route("/", web::get().to_with_state(index))
    .build_with::<()>(AppState { greeting: "Hello" });

Trait Implementations§

Source§

impl<St: State, In, Out, M, F> Debug for AppServices<St, In, Out, M, F>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<St, In, Out, M, F> !Freeze for AppServices<St, In, Out, M, F>

§

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

§

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

§

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

§

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

§

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

§

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