pub struct HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S>,
S: ServiceFactory<Config = AppConfig, Request = Request>,
<S as ServiceFactory>::Error: Into<Error>,
<S as ServiceFactory>::InitError: Debug,
<S as ServiceFactory>::Response: Into<Response<B>>,
B: MessageBody,{ /* private fields */ }Expand description
An HTTP Server.
Create new http server with application factory.
use actix_web::{web, App, HttpResponse, HttpServer};
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new()
.service(web::resource("/").to(|| HttpResponse::Ok())))
.bind("127.0.0.1:59090")?
.run()
.await
}Implementations§
Source§impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S>,
S: ServiceFactory<Config = AppConfig, Request = Request>,
<S as ServiceFactory>::Error: Into<Error> + 'static,
<S as ServiceFactory>::InitError: Debug,
<S as ServiceFactory>::Response: Into<Response<B>> + 'static,
<<S as ServiceFactory>::Service as Service>::Future: 'static,
B: MessageBody + 'static,
impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S>,
S: ServiceFactory<Config = AppConfig, Request = Request>,
<S as ServiceFactory>::Error: Into<Error> + 'static,
<S as ServiceFactory>::InitError: Debug,
<S as ServiceFactory>::Response: Into<Response<B>> + 'static,
<<S as ServiceFactory>::Service as Service>::Future: 'static,
B: MessageBody + 'static,
Sourcepub fn new(factory: F) -> HttpServer<F, I, S, B>
pub fn new(factory: F) -> HttpServer<F, I, S, B>
Create new http server with application factory
Sourcepub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
pub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
Sets function that will be called once before each connection is handled.
It will receive a &std::any::Any, which contains underlying connection type and an
Extensions container so that request-local data can be passed to middleware and handlers.
For example:
actix_tls::openssl::SslStream<actix_web::rt::net::TcpStream>when using openssl.actix_tls::rustls::TlsStream<actix_web::rt::net::TcpStream>when using rustls.actix_web::rt::net::TcpStreamwhen no encryption is used.
See on_connect example for additional details.
Sourcepub fn workers(self, num: usize) -> HttpServer<F, I, S, B>
pub fn workers(self, num: usize) -> HttpServer<F, I, S, B>
Set number of workers to start.
By default http server uses number of available logical cpu as threads count.
Sourcepub fn backlog(self, backlog: i32) -> HttpServer<F, I, S, B>
pub fn backlog(self, backlog: i32) -> HttpServer<F, I, S, B>
Set the maximum number of pending connections.
This refers to the number of clients that can be waiting to be served. Exceeding this number results in the client getting an error when attempting to connect. It should only affect servers under significant load.
Generally set in the 64-2048 range. Default value is 2048.
This method should be called before bind() method call.
Sourcepub fn max_connections(self, num: usize) -> HttpServer<F, I, S, B>
pub fn max_connections(self, num: usize) -> HttpServer<F, I, S, B>
Sets the maximum per-worker number of concurrent connections.
All socket listeners will stop accepting connections when this limit is reached for each worker.
By default max connections is set to a 25k.
Sourcepub fn max_connection_rate(self, num: usize) -> HttpServer<F, I, S, B>
pub fn max_connection_rate(self, num: usize) -> HttpServer<F, I, S, B>
Sets the maximum per-worker concurrent connection establish process.
All listeners will stop accepting connections when this limit is reached. It can be used to limit the global TLS CPU usage.
By default max connections is set to a 256.
Sourcepub fn keep_alive<T>(self, val: T) -> HttpServer<F, I, S, B>
pub fn keep_alive<T>(self, val: T) -> HttpServer<F, I, S, B>
Set server keep-alive setting.
By default keep alive is set to a 5 seconds.
Sourcepub fn client_timeout(self, val: u64) -> HttpServer<F, I, S, B>
pub fn client_timeout(self, val: u64) -> HttpServer<F, I, S, B>
Set server client timeout in milliseconds for first request.
Defines a timeout for reading client request header. If a client does not transmit the entire set headers within this time, the request is terminated with the 408 (Request Time-out) error.
To disable timeout set value to 0.
By default client timeout is set to 5000 milliseconds.
Sourcepub fn client_shutdown(self, val: u64) -> HttpServer<F, I, S, B>
pub fn client_shutdown(self, val: u64) -> HttpServer<F, I, S, B>
Set server connection shutdown timeout in milliseconds.
Defines a timeout for shutdown connection. If a shutdown procedure does not complete within this time, the request is dropped.
To disable timeout set value to 0.
By default client timeout is set to 5000 milliseconds.
Sourcepub fn server_hostname<T>(self, val: T) -> HttpServer<F, I, S, B>
pub fn server_hostname<T>(self, val: T) -> HttpServer<F, I, S, B>
Set server host name.
Host name is used by application router as a hostname for url generation. Check ConnectionInfo documentation for more information.
By default host name is set to a “localhost” value.
Sourcepub fn system_exit(self) -> HttpServer<F, I, S, B>
pub fn system_exit(self) -> HttpServer<F, I, S, B>
Stop actix system.
Sourcepub fn disable_signals(self) -> HttpServer<F, I, S, B>
pub fn disable_signals(self) -> HttpServer<F, I, S, B>
Disable signal handling
Sourcepub fn shutdown_timeout(self, sec: u64) -> HttpServer<F, I, S, B>
pub fn shutdown_timeout(self, sec: u64) -> HttpServer<F, I, S, B>
Timeout for graceful workers shutdown.
After receiving a stop signal, workers have this much time to finish serving requests. Workers still alive after the timeout are force dropped.
By default shutdown timeout sets to 30 seconds.
Sourcepub fn addrs(&self) -> Vec<SocketAddr>
pub fn addrs(&self) -> Vec<SocketAddr>
Get addresses of bound sockets.
Sourcepub fn addrs_with_scheme(&self) -> Vec<(SocketAddr, &str)>
pub fn addrs_with_scheme(&self) -> Vec<(SocketAddr, &str)>
Get addresses of bound sockets and the scheme for it.
This is useful when the server is bound from different sources with some sockets listening on http and some listening on https and the user should be presented with an enumeration of which socket requires which protocol.
Sourcepub fn listen(self, lst: TcpListener) -> Result<HttpServer<F, I, S, B>, Error>
pub fn listen(self, lst: TcpListener) -> Result<HttpServer<F, I, S, B>, Error>
Use listener for accepting incoming connection requests
HttpServer does not change any configuration for TcpListener, it needs to be configured before passing it to listen() method.
Sourcepub fn bind<A>(self, addr: A) -> Result<HttpServer<F, I, S, B>, Error>where
A: ToSocketAddrs,
pub fn bind<A>(self, addr: A) -> Result<HttpServer<F, I, S, B>, Error>where
A: ToSocketAddrs,
The socket address to bind
To bind multiple addresses this method can be called multiple times.
Sourcepub fn listen_uds(
self,
lst: UnixListener,
) -> Result<HttpServer<F, I, S, B>, Error>
pub fn listen_uds( self, lst: UnixListener, ) -> Result<HttpServer<F, I, S, B>, Error>
Start listening for unix domain (UDS) connections on existing listener.
Source§impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S>,
S: ServiceFactory<Config = AppConfig, Request = Request>,
<S as ServiceFactory>::Error: Into<Error>,
<S as ServiceFactory>::InitError: Debug,
<S as ServiceFactory>::Response: Into<Response<B>>,
<S as ServiceFactory>::Service: 'static,
B: MessageBody,
impl<F, I, S, B> HttpServer<F, I, S, B>where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S>,
S: ServiceFactory<Config = AppConfig, Request = Request>,
<S as ServiceFactory>::Error: Into<Error>,
<S as ServiceFactory>::InitError: Debug,
<S as ServiceFactory>::Response: Into<Response<B>>,
<S as ServiceFactory>::Service: 'static,
B: MessageBody,
Sourcepub fn run(self) -> Server ⓘ
pub fn run(self) -> Server ⓘ
Start listening for incoming connections.
This method starts number of http workers in separate threads.
For each address this method starts separate thread which does
accept() in a loop.
This methods panics if no socket address can be bound or an Actix system is not yet
configured.
use std::io;
use actix_web::{web, App, HttpResponse, HttpServer};
#[actix_rt::main]
async fn main() -> io::Result<()> {
HttpServer::new(|| App::new().service(web::resource("/").to(|| HttpResponse::Ok())))
.bind("127.0.0.1:0")?
.run()
.await
}Auto Trait Implementations§
impl<F, I, S, B> Freeze for HttpServer<F, I, S, B>
impl<F, I, S, B> !RefUnwindSafe for HttpServer<F, I, S, B>
impl<F, I, S, B> Send for HttpServer<F, I, S, B>where
<S as ServiceFactory>::Response: Sized,
<S as ServiceFactory>::Error: Sized,
S: Send,
B: Send,
impl<F, I, S, B> !Sync for HttpServer<F, I, S, B>
impl<F, I, S, B> Unpin for HttpServer<F, I, S, B>where
<S as ServiceFactory>::Response: Sized,
<S as ServiceFactory>::Error: Sized,
F: Unpin,
S: Unpin,
B: Unpin,
impl<F, I, S, B> !UnwindSafe for HttpServer<F, I, S, B>
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
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> 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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
&self to an expression for Diesel’s query builder. Read more