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

# [−] [src] Module actix_web::server

Http server module

The module contains everything necessary to setup HTTP server.

In order to start HTTP server, first you need to create and configure it using factory that can be supplied to new.

## Factory

Factory is a function that returns Application, describing how to serve incoming HTTP requests.

As the server uses worker pool, the factory function is restricted to trait bounds `Send + Clone + 'static` so that each worker would be able to accept Application without a need for synchronization.

If you wish to share part of state among all workers you should wrap it in `Arc` and potentially synchronization primitive like RwLock If the wrapped type is not thread safe.

Note though that locking is not advisable for asynchronous programming and you should minimize all locks in your request handlers

## HTTPS Support

Actix-web provides support for major crates that provides TLS. Each TLS implementation is provided with AcceptorService that describes how HTTP Server accepts connections.

For `bind` and `listen` there are corresponding `bind_ssl|tls|rustls` and `listen_ssl|tls|rustls` that accepts these services.

NOTE: `native-tls` doesn't support `HTTP2` yet

## Signal handling and shutdown

By default HTTP Server listens for system signals and, gracefully shuts down at most after 30 seconds.

Both signal handling and shutdown timeout can be controlled using corresponding methods.

If worker, for some reason, unable to shut down within timeout it is forcibly dropped.

## Example

ⓘThis example is not tested

    extern crate actix;
    extern crate actix_web;
    extern crate rustls;

    use actix_web::{http, middleware, server, App, Error, HttpRequest, HttpResponse, Responder};
    use std::io::BufReader;
    use rustls::internal::pemfile::{certs, rsa_private_keys};
    use rustls::{NoClientAuth, ServerConfig};

    fn index(req: &HttpRequest) -> Result<HttpResponse, Error> {
       Ok(HttpResponse::Ok().content_type("text/plain").body("Welcome!"))
    }

    fn load_ssl() -> ServerConfig {
       use std::io::BufReader;

       const CERT: &'static [u8] = include_bytes!("../cert.pem");
       const KEY: &'static [u8] = include_bytes!("../key.pem");

       let mut cert = BufReader::new(CERT);
       let mut key = BufReader::new(KEY);

       let mut config = ServerConfig::new(NoClientAuth::new());
       let cert_chain = certs(&mut cert).unwrap();
       let mut keys = rsa_private_keys(&mut key).unwrap();
       config.set_single_cert(cert_chain, keys.remove(0)).unwrap();

       config
    }

    fn main() {
       let sys = actix::System::new("http-server");
       // load ssl keys
       let config = load_ssl();

        // create and start server at once
        server::new(|| {
            App::new()
                // register simple handler, handle all methods
                .resource("/index.html", |r| r.f(index))
                }))
        }).bind_rustls("127.0.0.1:8443", config)
        .unwrap()
        .start();

        println!("Started http server: 127.0.0.1:8080");
        //Run system so that server would start accepting connections
        let _ = sys.run();
    }

## Structs

HttpServer | An HTTP Server
OpensslAcceptor | Support `SSL` connections via openssl package
PauseServer | Pause accepting incoming connections
Request | Request's context
ResumeServer | Resume accepting incoming connections
ServerFlags | Flags that can be used to configure HTTP Server.
ServerSettings | Various server settings
StopServer | Stop incoming connection processing, stop all workers and exit.

## Enums

AcceptorError | Errors produced by `AcceptorError` service.
HttpDispatchError | A set of errors that can occur during dispatching http requests
KeepAlive | Server keep-alive setting

## Traits

HttpHandler | Low level http request handler
HttpHandlerTask | Low level http request handler
IntoHttpHandler | Conversion helper trait

## Functions

new | Create new http server with application factory.
openssl_acceptor_with_flags | Configure `SslAcceptorBuilder` with custom server flags.

# 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`)