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>,
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>,
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 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()),
);Sourcepub fn service<S>(self, factory: S) -> Selfwhere
S: WebServiceFactory<St, Out> + 'static,
pub fn service<S>(self, factory: S) -> Selfwhere
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.
Sourcepub fn default_service<U>(
self,
f: impl IntoServiceFactory<U, St, WebRequest<Out>>,
) -> Selfwhere
U: ServiceFactory<St, WebRequest<Out>, Res = WebResponse> + 'static,
U::Error: WebResponseError<St, St::Error>,
U::InitError: IntoFailure,
pub fn default_service<U>(
self,
f: impl IntoServiceFactory<U, St, WebRequest<Out>>,
) -> Selfwhere
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>>,
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>>,
Sourcepub fn build(
self,
) -> impl ServiceFactory<St, Request, Res = Response, Error = WebError<St, St::Error>, InitError = Failure>
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>>,
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>>,
Sourcepub fn build_with<Outer>(
self,
state: St,
) -> impl ServiceFactory<Outer, Request, Res = Response, Error = WebError<St, St::Error>, InitError = Failure>where
St: Clone,
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" });