pub struct HttpServer<F, I, S, B> where
    F: 'static + Fn() -> I + Send + Clone,
    I: IntoServiceFactory<S, Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>,
    S: ServiceFactory<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>, Config = AppConfig>,
    B: MessageBody,
    <S as ServiceFactory<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>>::Error: Into<Error>,
    <S as ServiceFactory<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>>::InitError: Debug,
    <S as ServiceFactory<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>>::Response: Into<Response<B>>, 
{ /* 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

Create new HTTP server with application factory

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 connection data can be accessed in middleware and handlers.

Connection Types
  • actix_tls::accept::openssl::TlsStream<actix_web::rt::net::TcpStream> when using openssl.
  • actix_tls::accept::rustls::TlsStream<actix_web::rt::net::TcpStream> when using rustls.
  • actix_web::rt::net::TcpStream when no encryption is used.

See the on_connect example for additional details.

Set number of workers to start.

By default, the number of available physical CPUs is used as the worker count.

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.

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.

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.

Set max number of threads for each worker’s blocking task thread pool.

One thread pool is set up per worker; not shared across workers.

By default set to 512 / workers.

Set server keep-alive setting.

By default keep alive is set to a 5 seconds.

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.

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.

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.

Stop Actix System after server shutdown.

Disable signal handling

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.

Get addresses of bound sockets.

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.

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.

The socket address to bind

To bind multiple addresses this method can be called multiple times.

Start listening for unix domain (UDS) connections on existing listener.

Start listening for incoming unix domain connections.

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Extract a subset of the possible types in a coproduct (or get the remaining possibilities) Read more

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Performs the indexed conversion.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

Consumes the current HList and returns an HList with the requested shape. Read more

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Attempts to convert self into T using TryInto<T>. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more