[−][src]Struct ntex::web::App
Application builder - structure that follows the builder pattern for building application instances.
Implementations
impl App<AppEntry<DefaultError>, DefaultError>
[src]
pub fn new() -> Self
[src]
Create application builder. Application can be configured with a builder-like pattern.
impl<Err: ErrorRenderer> App<AppEntry<Err>, Err>
[src]
impl<T, Err> App<T, Err> where
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
[src]
T: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
pub fn data<U: 'static>(mut self: Self, data: U) -> Self
[src]
Set application data. Application data could be accessed
by using Data<T>
extractor where T
is data type.
Note: http server accepts an application factory rather than
an application instance. Http server constructs an application
instance for each thread, thus application data must be constructed
multiple times. If you want to share data between different
threads, a shared object should be used, e.g. Arc
. Internally Data
type
uses Arc
so data could be created outside of app factory and clones could
be stored via App::app_data()
method.
use std::cell::Cell; use ntex::web::{self, App, HttpResponse}; struct MyData { counter: Cell<usize>, } async fn index(data: web::types::Data<MyData>) -> HttpResponse { data.counter.set(data.counter.get() + 1); HttpResponse::Ok().into() } let app = App::new() .data(MyData{ counter: Cell::new(0) }) .service( web::resource("/index.html").route(web::get().to(index)) );
pub fn data_factory<F, Out, D, E>(mut self: Self, data: F) -> Self where
F: Fn() -> Out + 'static,
Out: Future<Output = Result<D, E>> + 'static,
D: 'static,
E: Debug,
[src]
F: Fn() -> Out + 'static,
Out: Future<Output = Result<D, E>> + 'static,
D: 'static,
E: Debug,
Set application data factory. This function is
similar to .data()
but it accepts data factory. Data object get
constructed asynchronously during application initialization.
pub fn app_data<U: 'static>(mut self: Self, ext: U) -> Self
[src]
Set application level arbitrary data item.
Application data stored with App::app_data()
method is available
via HttpRequest::app_data()
method at runtime.
This method could be used for storing Data<T>
as well, in that case
data could be accessed by using Data<T>
extractor.
pub fn configure<F>(mut self: Self, f: F) -> Self where
F: FnOnce(&mut ServiceConfig<Err>),
[src]
F: FnOnce(&mut ServiceConfig<Err>),
Run external configuration as part of the application building process
This function is useful for moving parts of configuration to a different module or even library. For example, some of the resource's configuration could be moved to different module.
use ntex::web::{self, middleware, App, HttpResponse}; // this function could be located in different module fn config(cfg: &mut web::ServiceConfig) { cfg.service(web::resource("/test") .route(web::get().to(|| async { HttpResponse::Ok() })) .route(web::head().to(|| async { HttpResponse::MethodNotAllowed() })) ); } fn main() { let app = App::new() .wrap(middleware::Logger::default()) .configure(config) // <- register resources .route("/index.html", web::get().to(|| async { HttpResponse::Ok() })); }
pub fn route(self, path: &str, mut route: Route<Err>) -> Self
[src]
Configure route for a specific path.
This is a simplified version of the App::service()
method.
This method can be used multiple times with same path, in that case
multiple resources with one route would be registered for same resource path.
use ntex::web::{self, App, HttpResponse}; async fn index(data: web::types::Path<(String, String)>) -> &'static str { "Welcome!" } fn main() { let app = App::new() .route("/test1", web::get().to(index)) .route("/test2", web::post().to(|| async { HttpResponse::MethodNotAllowed() })); }
pub fn service<F>(mut self: Self, factory: F) -> Self where
F: WebServiceFactory<Err> + 'static,
[src]
F: WebServiceFactory<Err> + 'static,
Register http service.
Http service is any type that implements WebServiceFactory
trait.
ntex provides several services implementations:
- Resource is an entry in resource table which corresponds to requested URL.
- Scope is a set of resources with common root path.
- "StaticFiles" is a service for static files support
pub fn default_service<F, U>(mut self: Self, f: F) -> Self where
F: IntoServiceFactory<U>,
U: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
U::InitError: Debug,
[src]
F: IntoServiceFactory<U>,
U: ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
U::InitError: Debug,
Default service to be used if no matching resource could be found.
It is possible to use services like Resource
, Route
.
use ntex::web::{self, App, HttpResponse}; async fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new() .service( web::resource("/index.html").route(web::get().to(index))) .default_service( web::route().to(|| async { HttpResponse::NotFound() })); }
It is also possible to use static files as default service.
use ntex::web::{self, App, HttpResponse}; fn main() { let app = App::new() .service( web::resource("/index.html").to(|| async { HttpResponse::Ok() })) .default_service( web::to(|| async { HttpResponse::NotFound() }) ); }
pub fn external_resource<N, U>(mut self: Self, name: N, url: U) -> Self where
N: AsRef<str>,
U: AsRef<str>,
[src]
N: AsRef<str>,
U: AsRef<str>,
Register an external resource.
External resources are useful for URL generation purposes only
and are never considered for matching at request time. Calls to
HttpRequest::url_for()
will work as expected.
use ntex::web::{self, App, HttpRequest, HttpResponse, Error}; async fn index(req: HttpRequest) -> Result<HttpResponse, Error> { let url = req.url_for("youtube", &["asdlkjqme"])?; assert_eq!(url.as_str(), "https://youtube.com/watch/asdlkjqme"); Ok(HttpResponse::Ok().into()) } fn main() { let app = App::new() .service(web::resource("/index.html").route( web::get().to(index))) .external_resource("youtube", "https://youtube.com/watch/{video_id}"); }
pub fn wrap<M>(
self,
mw: M
) -> App<impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>, Err> where
M: Transform<T::Service, Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>,
[src]
self,
mw: M
) -> App<impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>, Err> where
M: Transform<T::Service, Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>,
Registers middleware, in the form of a middleware component (type), that runs during inbound and/or outbound processing in the request lifecycle (request -> response), modifying request/response as necessary, across all requests managed by the Application.
Use middleware when you need to read or modify every request or response in some way.
Notice that the keyword for registering middleware is wrap
. As you
register middleware using wrap
in the App builder, imagine wrapping
layers around an inner App. The first middleware layer exposed to a
Request is the outermost layer-- the last registered in
the builder chain. Consequently, the first middleware registered
in the builder chain is the last to execute during request processing.
use ntex::http::header::{CONTENT_TYPE, HeaderValue}; use ntex::web::{self, middleware, App}; async fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new() .wrap(middleware::Logger::default()) .route("/index.html", web::get().to(index)); }
pub fn wrap_fn<F, R>(
self,
mw: F
) -> App<impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>, Err> where
F: Fn(WebRequest<Err>, &T::Service) -> R + Clone,
R: Future<Output = Result<WebResponse, Err::Container>>,
[src]
self,
mw: F
) -> App<impl ServiceFactory<Config = (), Request = WebRequest<Err>, Response = WebResponse, Error = Err::Container, InitError = ()>, Err> where
F: Fn(WebRequest<Err>, &T::Service) -> R + Clone,
R: Future<Output = Result<WebResponse, Err::Container>>,
Registers middleware, in the form of a closure, that runs during inbound and/or outbound processing in the request lifecycle (request -> response), modifying request/response as necessary, across all requests managed by the Application.
Use middleware when you need to read or modify every request or response in some way.
use ntex::{web, Service}; use ntex::http::header::{CONTENT_TYPE, HeaderValue}; async fn index() -> &'static str { "Welcome!" } fn main() { let app = web::App::new() .wrap_fn(|req, srv| { let fut = srv.call(req); async { let mut res = fut.await?; res.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("text/plain"), ); Ok(res) } }) .route("/index.html", web::get().to(index)); }
pub fn case_insensitive_routing(mut self: Self) -> Self
[src]
Use ascii case-insensitive routing.
Only static segments could be case-insensitive.
pub fn with_config(
self,
cfg: AppConfig
) -> impl ServiceFactory<Config = (), Request = Request, Response = WebResponse, Error = T::Error, InitError = T::InitError>
[src]
self,
cfg: AppConfig
) -> impl ServiceFactory<Config = (), Request = Request, Response = WebResponse, Error = T::Error, InitError = T::InitError>
Construct service factory suitable for http::HttpService
.
use ntex::{web, http, server}; #[ntex::main] async fn main() -> std::io::Result<()> { server::build().bind("http", "127.0.0.1:0", || http::HttpService::build().finish( web::App::new() .route("/index.html", web::get().to(|| async { "hello_world" })) .with_config(web::dev::AppConfig::default()) ).tcp() )? .run() .await }
Auto Trait Implementations
impl<T, Err = DefaultError> !RefUnwindSafe for App<T, Err>
[src]
impl<T, Err = DefaultError> !Send for App<T, Err>
[src]
impl<T, Err = DefaultError> !Sync for App<T, Err>
[src]
impl<T, Err> Unpin for App<T, Err> where
Err: Unpin,
T: Unpin,
[src]
Err: Unpin,
T: Unpin,
impl<T, Err = DefaultError> !UnwindSafe for App<T, Err>
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,