pub struct WebServiceAdapter { /* private fields */ }Expand description
Create service adapter for a specific path.
use std::convert::Infallible;
use ntex::web::{self, guard, App, HttpResponse};
async fn my_service(req: web::WebRequest<()>) -> Result<web::WebResponse, Infallible> {
Ok(req.into_response(HttpResponse::Ok().build()))
}
let app = App::default().service(
web::service("/users/*")
.guard(guard::Header("content-type", "text/plain"))
.build(my_service)
);Implementations§
Source§impl WebServiceAdapter
impl WebServiceAdapter
Sourcepub fn new<T: IntoPattern>(path: T) -> Self
pub fn new<T: IntoPattern>(path: T) -> Self
Create new WebServiceAdapter instance.
Sourcepub fn name(self, name: &str) -> Self
pub fn name(self, name: &str) -> Self
Set service name.
Name is used for url generation, it must be unique within the enclosing scope or application, which panics on start otherwise.
Sourcepub fn guard<G: Guard + 'static>(self, guard: G) -> Self
pub fn guard<G: Guard + 'static>(self, guard: G) -> Self
Add a match guard to this web service.
The service is selected only when its path and all registered guards match. If a guard rejects the request, the router can try another matching service; otherwise the containing scope or application fallback is used.
use std::convert::Infallible;
use ntex::web::{self, guard, App, HttpResponse};
async fn index(
req: web::WebRequest<()>,
) -> Result<web::WebResponse, Infallible> {
Ok(req.into_response(HttpResponse::Ok().build()))
}
App::default().service(
web::service("/health")
.guard(guard::Get())
.guard(guard::Header("x-health-check", "true"))
.build(index)
);Sourcepub fn method(self, method: Method) -> Self
pub fn method(self, method: Method) -> Self
Add a supported method to this service.
Repeated calls allow any of the registered methods. If this method is never called, no method check is added and existing guard and service behavior is unchanged.
Methods are checked after the path and all guards match, but before the
service is called. An unsupported method returns 405 Method Not Allowed
with an Allow header; the router does not try another service or fallback.
Use a method guard instead when a mismatch should continue routing.
use std::convert::Infallible;
use ntex::http::Method;
use ntex::web::{self, guard, App, HttpResponse};
async fn index(
req: web::WebRequest<()>,
) -> Result<web::WebResponse, Infallible> {
Ok(req.into_response(HttpResponse::Ok().build()))
}
App::default().service(
web::service("/health")
.method(Method::GET)
.guard(guard::Header("x-health-check", "true"))
.build(index)
);Sourcepub fn build<Sf, St, In, F>(self, service: F) -> impl WebServiceFactory<St, In>where
St: State,
In: 'static,
F: IntoServiceFactory<Sf, St, WebRequest<In>>,
Sf: ServiceFactory<St, WebRequest<In>, Res = WebResponse> + 'static,
Sf::Error: WebResponseError<St, St::Error>,
Sf::InitError: IntoFailure,
pub fn build<Sf, St, In, F>(self, service: F) -> impl WebServiceFactory<St, In>where
St: State,
In: 'static,
F: IntoServiceFactory<Sf, St, WebRequest<In>>,
Sf: ServiceFactory<St, WebRequest<In>, Res = WebResponse> + 'static,
Sf::Error: WebResponseError<St, St::Error>,
Sf::InitError: IntoFailure,
Set a service factory implementation and generate web service.