pub struct Route<Err: ErrorRenderer = DefaultError> { /* private fields */ }
Expand description
Resource route definition
Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.
Implementations§
source§impl<Err: ErrorRenderer> Route<Err>
impl<Err: ErrorRenderer> Route<Err>
source§impl<Err: ErrorRenderer> Route<Err>
impl<Err: ErrorRenderer> Route<Err>
sourcepub fn method(self, method: Method) -> Self
pub fn method(self, method: Method) -> Self
Add method guard to the route.
App::new().service(web::resource("/path").route(
web::route()
.method(ntex::http::Method::CONNECT)
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| async { HttpResponse::Ok() }))
);
sourcepub fn guard<F: Guard + 'static>(self, f: F) -> Self
pub fn guard<F: Guard + 'static>(self, f: F) -> Self
Add guard to the route.
App::new().service(web::resource("/path").route(
web::route()
.guard(guard::Get())
.guard(guard::Header("content-type", "text/plain"))
.to(|req: HttpRequest| async { HttpResponse::Ok() }))
);
sourcepub fn to<F, Args>(self, handler: F) -> Selfwhere
F: Handler<Args, Err> + 'static,
Args: FromRequest<Err> + 'static,
Args::Error: Into<Err::Container>,
pub fn to<F, Args>(self, handler: F) -> Selfwhere
F: Handler<Args, Err> + 'static,
Args: FromRequest<Err> + 'static,
Args::Error: Into<Err::Container>,
Set handler function, use request extractors for parameters.
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(info: web::types::Path<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let app = web::App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index)) // <- register handler
);
}
It is possible to use multiple extractors for one handler function.
use ntex::web;
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// extract path info using serde
async fn index(path: web::types::Path<Info>, query: web::types::Query<HashMap<String, String>>, body: web::types::Json<Info>) -> String {
format!("Welcome {}!", path.username)
}
fn main() {
let app = web::App::new().service(
web::resource("/{username}/index.html") // <- define path parameters
.route(web::get().to(index))
);
}
Trait Implementations§
source§impl<Err: ErrorRenderer> Debug for Route<Err>
impl<Err: ErrorRenderer> Debug for Route<Err>
source§impl<Err: ErrorRenderer> IntoRoutes<Err> for Route<Err>
impl<Err: ErrorRenderer> IntoRoutes<Err> for Route<Err>
source§impl<Err: ErrorRenderer> ServiceFactory<WebRequest<Err>> for Route<Err>
impl<Err: ErrorRenderer> ServiceFactory<WebRequest<Err>> for Route<Err>
§type Response = WebResponse
type Response = WebResponse
Responses given by the created services.
§type Error = <Err as ErrorRenderer>::Container
type Error = <Err as ErrorRenderer>::Container
Errors produced by the created services.
source§async fn create(&self, _: ()) -> Result<RouteService<Err>, ()>
async fn create(&self, _: ()) -> Result<RouteService<Err>, ()>
Create and return a new service value asynchronously.
source§async fn pipeline(
&self,
cfg: Cfg
) -> Result<Pipeline<Self::Service>, Self::InitError>where
Self: Sized,
async fn pipeline(
&self,
cfg: Cfg
) -> Result<Pipeline<Self::Service>, Self::InitError>where
Self: Sized,
Create and return a new service value asynchronously and wrap into a container
source§fn map<F, Res>(
self,
f: F
) -> ServiceChainFactory<MapFactory<Self, F, Req, Res, Cfg>, Req, Cfg>
fn map<F, Res>( self, f: F ) -> ServiceChainFactory<MapFactory<Self, F, Req, Res, Cfg>, Req, Cfg>
Map this service’s output to a different type, returning a new service
of the resulting type.
source§fn map_err<F, E>(
self,
f: F
) -> ServiceChainFactory<MapErrFactory<Self, Req, Cfg, F, E>, Req, Cfg>
fn map_err<F, E>( self, f: F ) -> ServiceChainFactory<MapErrFactory<Self, Req, Cfg, F, E>, Req, Cfg>
Map this service’s error to a different error, returning a new service.
source§fn map_init_err<F, E>(
self,
f: F
) -> ServiceChainFactory<MapInitErr<Self, Req, Cfg, F, E>, Req, Cfg>
fn map_init_err<F, E>( self, f: F ) -> ServiceChainFactory<MapInitErr<Self, Req, Cfg, F, E>, Req, Cfg>
Map this factory’s init error to a different error, returning a new service.
Auto Trait Implementations§
impl<Err> Freeze for Route<Err>
impl<Err = DefaultError> !RefUnwindSafe for Route<Err>
impl<Err = DefaultError> !Send for Route<Err>
impl<Err = DefaultError> !Sync for Route<Err>
impl<Err> Unpin for Route<Err>
impl<Err = DefaultError> !UnwindSafe for Route<Err>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T, Req, Cfg> IntoServiceFactory<T, Req, Cfg> for Twhere
T: ServiceFactory<Req, Cfg>,
impl<T, Req, Cfg> IntoServiceFactory<T, Req, Cfg> for Twhere
T: ServiceFactory<Req, Cfg>,
source§fn into_factory(self) -> T
fn into_factory(self) -> T
Convert
Self
to a ServiceFactory