# actix_web::server::HttpServer - Rust
API documentation for the Rust `HttpServer` struct in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, HttpServer

# Struct actix_web::server::HttpServer [−] [src]

    pub struct HttpServer<H> where
        H: IntoHttpHandler + 'static,  { /* fields omitted */ }

An HTTP Server

## Methods

###

`impl<H> HttpServer<H> where H: IntoHttpHandler + 'static,`

[src]

####

`pub fn new<F, U>(factory: F) -> Self where F: Fn() -> U + Sync + Send + 'static, U: IntoIterator<Item = H> + 'static,`

[src]

Create new http server with application factory

####

`pub fn workers(self, num: usize) -> Self`

[src]

Set number of workers to start.

By default http server uses number of available logical cpu as threads count.

####

`pub fn backlog(self, num: i32) -> Self`

[src]

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.

####

`pub fn keep_alive<T: Into<KeepAlive>>(self, val: T) -> Self`

[src]

Set server keep-alive setting.

By default keep alive is set to a `Os`.

####

`pub fn server_hostname(self, val: String) -> Self`

[src]

Set server host name.

Host name is used by application router aa a hostname for url generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo. html#method.host) documentation for more information.

####

`pub fn system_exit(self) -> Self`

[src]

Stop actix system.

`SystemExit` message stops currently running system.

####

`pub fn signals(self, addr: Addr<ProcessSignals>) -> Self`

[src]

Set alternative address for `ProcessSignals` actor.

####

`pub fn disable_signals(self) -> Self`

[src]

Disable signal handling

####

`pub fn shutdown_timeout(self, sec: u16) -> Self`

[src]

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.

####

`pub fn no_http2(self) -> Self`

[src]

Disable `HTTP/2` support

####
ⓘImportant traits for Vec<u8>

### Important traits for Vec<u8>

`impl Write for Vec<u8>`

`pub fn addrs(&self) -> Vec<SocketAddr>`

[src]

Get addresses of bound sockets.

####
ⓘImportant traits for Vec<u8>

### Important traits for Vec<u8>

`impl Write for Vec<u8>`

`pub fn addrs_with_scheme(&self) -> Vec<(SocketAddr, &str)>`

[src]

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.

####

`pub fn listen(self, lst: TcpListener) -> Self`

[src]

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.

####

`pub fn listen_tls(self, lst: TcpListener, acceptor: TlsAcceptor) -> Self`

[src]

Use listener for accepting incoming tls connection requests

HttpServer does not change any configuration for TcpListener, it needs to be configured before passing it to listen() method.

####

`pub fn listen_ssl( self, lst: TcpListener, builder: SslAcceptorBuilder ) -> Result<Self>`

[src]

Use listener for accepting incoming tls connection requests

This method sets alpn protocols to "h2" and "http/1.1"

####

`pub fn bind<S: ToSocketAddrs>(self, addr: S) -> Result<Self>`

[src]

The socket address to bind

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

####

`pub fn bind_tls<S: ToSocketAddrs>( self, addr: S, acceptor: TlsAcceptor ) -> Result<Self>`

[src]

The ssl socket address to bind

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

####

`pub fn bind_ssl<S: ToSocketAddrs>( self, addr: S, builder: SslAcceptorBuilder ) -> Result<Self>`

[src]

Start listening for incoming tls connections.

This method sets alpn protocols to "h2" and "http/1.1"

###

`impl<H: IntoHttpHandler> HttpServer<H>`

[src]

####

`pub fn start(self) -> Addr<Self>`

[src]

Start listening for incoming connections.

This method starts number of http handler workers in separate threads. For each address this method starts separate thread which does `accept()` in a loop.

This methods panics if no socket addresses get bound.

This method requires to run within properly configured `Actix` system.

    extern crate actix_web;
    use actix_web::{actix, server, App, HttpResponse};

    fn main() {
        let sys = actix::System::new("example");  // <- create Actix system

        server::new(|| App::new().resource("/", |r| r.h(|_: &_| HttpResponse::Ok())))
            .bind("127.0.0.1:0")
            .expect("Can not bind to 127.0.0.1:0")
            .start();
       sys.run();  // <- Run actix system, this method starts all async processes
    }

####

`pub fn run(self)`

[src]

Spawn new thread and start listening for incoming connections.

This method spawns new thread and starts new actix system. Other than that it is similar to `start()` method. This method blocks.

This methods panics if no socket addresses get bound.

ⓘThis example is not tested

    use actix_web::*;

    fn main() {
        HttpServer::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok())))
            .bind("127.0.0.1:0")
            .expect("Can not bind to 127.0.0.1:0")
            .run();
    }

###

`impl<H: IntoHttpHandler> HttpServer<H>`

[src]

####

`pub fn start_incoming<T, S>(self, stream: S, secure: bool) -> Addr<Self> where S: Stream<Item = T, Error = Error> + Send + 'static, T: AsyncRead + AsyncWrite + Send + 'static,`

[src]

Start listening for incoming connections from a stream.

This method uses only one thread for handling incoming connections.

## Trait Implementations

###

`impl<H> Actor for HttpServer<H> where H: IntoHttpHandler,`

[src]

#### `type Context = Context<Self>`

Actor execution context type

####

`fn started(&mut self, ctx: &mut Self::Context)` |

Method is called when actor get polled first time.

####

`fn stopping(&mut self, ctx: &mut Self::Context) -> Running` |

Method is called after an actor is in `Actor::Stopping` state. There could be several reasons for stopping. `Context::stop` get called by the actor itself. All addresses to current actor get dropped and no more evented objects left in the context. Read more

####

`fn stopped(&mut self, ctx: &mut Self::Context)` |

Method is called after an actor is stopped, it can be used to perform any needed cleanup work or spawning more actors. This is final state, after this call actor get dropped. Read more

####

`fn start(self) -> Addr<Self> where Self: Actor<Context = Context<Self>>,` |

Start new asynchronous actor, returns address of newly created actor. Read more

####

`fn start_default() -> Addr<Self> where Self: Actor<Context = Context<Self>> + Default,` |

Start new asynchronous actor, returns address of newly created actor.

####

`fn create<F>(f: F) -> Addr<Self> where F: FnOnce(&mut Context<Self>) -> Self + 'static, Self: Actor<Context = Context<Self>>,` |

Use `create` method, if you need `Context` object during actor initialization. Read more

###

`impl<H: IntoHttpHandler> Handler<Signal> for HttpServer<H>`

[src]

Signals support Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system message to `System` actor.

#### `type Result = ()`

The type of value that this handle will return

####

`fn handle(&mut self, msg: Signal, ctx: &mut Context<Self>)`

[src]

Method is called for every message received by this Actor

###

`impl<H: IntoHttpHandler> Handler<PauseServer> for HttpServer<H>`

[src]

#### `type Result = ()`

The type of value that this handle will return

####

`fn handle(&mut self, _: PauseServer, _: &mut Context<Self>)`

[src]

Method is called for every message received by this Actor

###

`impl<H: IntoHttpHandler> Handler<ResumeServer> for HttpServer<H>`

[src]

#### `type Result = ()`

The type of value that this handle will return

####

`fn handle(&mut self, _: ResumeServer, _: &mut Context<Self>)`

[src]

Method is called for every message received by this Actor

###

`impl<H: IntoHttpHandler> Handler<StopServer> for HttpServer<H>`

[src]

#### `type Result = Response<(), ()>`

The type of value that this handle will return

####

`fn handle(&mut self, msg: StopServer, ctx: &mut Context<Self>) -> Self::Result`

[src]

Method is called for every message received by this Actor

## Auto Trait Implementations

###

`impl<H> !Send for HttpServer<H>`

###

`impl<H> !Sync for HttpServer<H>`

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)