Hearken Documentation Beta

Class Server

public final class Server

An HTTP server.

An instance of Hearken can be easily created by initializing a Server:

import Hearken

let server = Server()

Simple routes with a response instance can be added by using a custom subscript on Server:

server[.GET, "/api/health"] = HTTPResponse(status: .ok)

You can also assign closures to routes by using another custom subscript on Server if you need more control over your route:

server[.POST, "/api/hello"] = { request in
    print(request)

    return .init(status: .ok,
                 headers: [.contentType(.json)],
                 body: """
                       {
                           "hello": "world"
                       }
                       """)
}

The Server can be started asynchronously by calling start() and stopped by calling stop():

try server.start()
try server.stop()

The Server can also be started synchronously using syncStart() if you want to block the current thread:

try server.syncStart()

Initializers

init(port:​logger:​)

init(port: Int, logger: LoggerType)

init(port:​is​Logging​Enabled:​)

public convenience init(port: Int = 8888, isLoggingEnabled: Bool = false)

Creates an instance of an HTTP Server.

Parameters

port Int
  • port: The port to bind to. Defaults to 8888.
is​Logging​Enabled Bool
  • isLoggingEnabled: Enables logging to stdout. Defaults to false.

Properties

group

let group

router

let router

logger

let logger: LoggerType

request​Handler

let requestHandler: RequestHandler

host

let host

port

let port: Int

is​Running

var isRunning

Methods

start()

func start() throws

Starts the Server asynchronously.

sync​Start()

func syncStart() throws

Starts the Server synchronously.

Use this if you want to block the current thread.

stop()

func stop() throws

Stops the server and closes all connections gracefully.

start​Channel()

@discardableResult func startChannel() throws -> Channel