[−][src]Struct actix_web::App
Application builder - structure that follows the builder pattern for building application instances.
Methods
impl App<PayloadStream, PayloadStream, AppChain>
[src]
pub fn new() -> Self
[src]
Create application builder. Application can be configured with a builder-like pattern.
impl<In, Out, T> App<In, Out, T> where
In: 'static,
Out: 'static,
T: NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Out>, Error = Error, InitError = ()>,
[src]
In: 'static,
Out: 'static,
T: NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Out>, Error = Error, InitError = ()>,
pub fn data<S: 'static>(self, data: S) -> 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
. Application
data does not need to be Send
or Sync
.
use std::cell::Cell; use actix_web::{web, App}; struct MyData { counter: Cell<usize>, } fn index(data: web::Data<MyData>) { data.counter.set(data.counter.get() + 1); } fn main() { 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, R>(self, data: F) -> Self where
F: Fn() -> R + 'static,
R: IntoFuture + 'static,
R::Error: Debug,
[src]
F: Fn() -> R + 'static,
R: IntoFuture + 'static,
R::Error: 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 wrap<M, B, F>(
self,
mw: F
) -> AppRouter<T, Out, B, impl NewService<Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>> where
M: Transform<AppRouting<Out>, Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>,
F: IntoTransform<M, AppRouting<Out>>,
[src]
self,
mw: F
) -> AppRouter<T, Out, B, impl NewService<Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>> where
M: Transform<AppRouting<Out>, Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>,
F: IntoTransform<M, AppRouting<Out>>,
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.
use actix_service::Service; use actix_web::{middleware, web, App}; use actix_web::http::{header::CONTENT_TYPE, HeaderValue}; 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, B>(
self,
mw: F
) -> AppRouter<T, Out, B, impl NewService<Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<Out>, &mut AppRouting<Out>) -> R + Clone,
R: IntoFuture<Item = ServiceResponse<B>, Error = Error>,
[src]
self,
mw: F
) -> AppRouter<T, Out, B, impl NewService<Request = ServiceRequest<Out>, Response = ServiceResponse<B>, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<Out>, &mut AppRouting<Out>) -> R + Clone,
R: IntoFuture<Item = ServiceResponse<B>, Error = Error>,
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 actix_service::Service; use actix_web::{web, App}; use actix_web::http::{header::CONTENT_TYPE, HeaderValue}; fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new() .wrap_fn(|req, srv| srv.call(req).map(|mut res| { res.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("text/plain"), ); res })) .route("/index.html", web::get().to(index)); }
pub fn chain<C, F, P>(
self,
chain: F
) -> App<In, P, impl NewService<Request = ServiceRequest<In>, Response = ServiceRequest<P>, Error = Error, InitError = ()>> where
C: NewService<Request = ServiceRequest<Out>, Response = ServiceRequest<P>, Error = Error, InitError = ()>,
F: IntoNewService<C>,
[src]
self,
chain: F
) -> App<In, P, impl NewService<Request = ServiceRequest<In>, Response = ServiceRequest<P>, Error = Error, InitError = ()>> where
C: NewService<Request = ServiceRequest<Out>, Response = ServiceRequest<P>, Error = Error, InitError = ()>,
F: IntoNewService<C>,
Register a request modifier. It can modify any request parameters including request payload type.
pub fn configure<F>(self, f: F) -> AppRouter<T, Out, Body, AppEntry<Out>> where
F: Fn(&mut RouterConfig<Out>),
[src]
F: Fn(&mut RouterConfig<Out>),
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 actix_web::{web, middleware, App, HttpResponse}; // this function could be located in different module fn config<P>(cfg: &mut web::RouterConfig<P>) { cfg.service(web::resource("/test") .route(web::get().to(|| HttpResponse::Ok())) .route(web::head().to(|| HttpResponse::MethodNotAllowed())) ); } fn main() { let app = App::new() .wrap(middleware::Logger::default()) .configure(config) // <- register resources .route("/index.html", web::get().to(|| HttpResponse::Ok())); }
pub fn route(
self,
path: &str,
route: Route<Out>
) -> AppRouter<T, Out, Body, AppEntry<Out>>
[src]
self,
path: &str,
route: Route<Out>
) -> AppRouter<T, Out, Body, AppEntry<Out>>
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 actix_web::{web, App, HttpResponse}; fn index(data: web::Path<(String, String)>) -> &'static str { "Welcome!" } fn main() { let app = App::new() .route("/test1", web::get().to(index)) .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed())); }
pub fn service<F>(self, service: F) -> AppRouter<T, Out, Body, AppEntry<Out>> where
F: HttpServiceFactory<Out> + 'static,
[src]
F: HttpServiceFactory<Out> + 'static,
Register http service.
Http service is any type that implements HttpServiceFactory
trait.
Actix web 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 hostname(self, val: &str) -> Self
[src]
Set server host name.
Host name is used by application router as a hostname for url generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo. html#method.host) documentation for more information.
By default host name is set to a "localhost" value.
pub fn enable_encoding(
self
) -> AppRouter<impl NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Decoder<Payload<Out>>>, Error = Error, InitError = ()>, Decoder<Payload<Out>>, Encoder<Body>, impl NewService<Request = ServiceRequest<Decoder<Payload<Out>>>, Response = ServiceResponse<Encoder<Body>>, Error = Error, InitError = ()>> where
Out: Stream<Item = Bytes, Error = PayloadError>,
[src]
self
) -> AppRouter<impl NewService<Request = ServiceRequest<In>, Response = ServiceRequest<Decoder<Payload<Out>>>, Error = Error, InitError = ()>, Decoder<Payload<Out>>, Encoder<Body>, impl NewService<Request = ServiceRequest<Decoder<Payload<Out>>>, Response = ServiceResponse<Encoder<Body>>, Error = Error, InitError = ()>> where
Out: Stream<Item = Bytes, Error = PayloadError>,
Enable content compression and decompression.
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto 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.