HTTPServer

public final class HTTPServer

Simple HTTP server generic on an HTTP responder that will be used to generate responses to incoming requests.

let server = try HTTPServer.start(hostname: hostname, port: port, responder: EchoResponder(), on: group).wait()
try server.onClose.wait()
  • Starts the server on the supplied hostname and port, using the supplied responder to generate HTTP responses for incoming requests.

    let server = try HTTPServer.start(hostname: hostname, port: port, responder: EchoResponder(), on: group).wait()
    try server.onClose.wait()
    

    Declaration

    Swift

    public static func start<R>(
        hostname: String,
        port: Int,
        responder: R,
        maxBodySize: Int = 1_000_000,
        backlog: Int = 256,
        reuseAddress: Bool = true,
        tcpNoDelay: Bool = true,
        supportCompression: Bool = false,
        serverName: String? = nil,
        upgraders: [HTTPProtocolUpgrader] = [],
        on worker: Worker,
        onError: @escaping (Error) -> () = { _ in }
    ) -> Future<HTTPServer> where R: HTTPServerResponder

    Parameters

    hostname

    Socket hostname to bind to. Usually localhost or ::1.

    port

    Socket port to bind to. Usually 8080 for development and 80 for production.

    responder

    Used to generate responses for incoming requests.

    maxBodySize

    Requests with bodies larger than this maximum will be rejected. Streaming bodies, like chunked bodies, ignore this maximum.

    backlog

    OS socket backlog size.

    reuseAddress

    When true, can prevent errors re-binding to a socket after successive server restarts.

    tcpNoDelay

    When true, OS will attempt to minimize TCP packet delay.

    supportCompression

    When true, HTTP server will support gzip and deflate compression.

    serverName

    If set, this name will be serialized as the Server header in outgoing responses.

    upgraders

    An array of HTTPProtocolUpgrader to check for with each request.

    worker

    Worker to perform async work on.

    onError

    Any uncaught server or responder errors will go here.

  • A future that will be signaled when the server closes.

    Declaration

    Swift

    public var onClose: Future<Void> { get }