Type Aliases

The following type aliases are available globally.

ServerOptions

  • Bridge ServerOptions from KituraNet so that you only need to import Kitura to access it.

    ServerOptions allows customization of default connection policies, including:

    • requestSizeLimit: Defines the maximum size of an incoming request body, in bytes. If requests are received that are larger than this limit, they will be rejected and the connection will be closed. A value of nil means no limit.
    • connectionLimit: Defines the maximum number of concurrent connections that a server should accept. Clients attempting to connect when this limit has been reached will be rejected. A value of nil means no limit.

    The server can optionally respond to the client with a message in either of these cases. This message can be customized by defining requestSizeResponseGenerator and connectionResponseGenerator.

    Example usage:

    let port = 8080
    let router = Router()
    let connectionResponse: (Int, String) -> (HTTPStatusCode, String)? = { (limit, client) in
        Log.debug("Rejecting request from \(client): Connection limit \(limit) reached")
        return (.serviceUnavailable, "Service busy - please try again later.\r\n")
    }
    let serverOptions = ServerOptions(requestSizeLimit: 1000, connectionLimit: 10, connectionResponseGenerator: connectionResponse)
    Kitura.addHTTPServer(onPort: port, with: router, options: serverOptions)
    

    Declaration

    Swift

    public typealias ServerOptions = KituraNet.ServerOptions
  • The definition of the closure type that is used by the Router class when routing HTTP requests to closure.

    Declaration

    Swift

    public typealias RouterHandler = (RouterRequest, RouterResponse, @escaping () -> Void) throws -> Void

    Parameters

    request

    The RouterRequest object used to work with the incoming HTTP request.

    response

    The RouterResponse object used to respond to the HTTP request.

    next

    The closure called to invoke the next handler or middleware associated with the request.

  • A type alias declaration to describe a handler for named parameters when using Router.parameter(...). The example below shows two ways to use it, both as a function named handler to handle the “id” parameter and as a closure to handle the “name” parameter.

    Usage Example:

    let router = Router()
    func handler(request: RouterRequest, response: RouterResponse, param: String, next: @escaping () -> Void) throws -> Void {
        //Code to handle id parameter here
        next()
    }
    router.parameter("id", handler: handler)
    
    router.parameter("name") { request, response, param, next in
        //Code to handle name parameter here
        next()
    }
    router.get("/item/:id") { request, response, next in
        //This will be reached after the id parameter is handled by `handler`
    }
    router.get("/user/:name") { request, response, next in
        //This will be reached after the name parameter is handled by the closure above
    }
    

    Declaration

    Swift

    public typealias RouterParameterHandler = (RouterRequest, RouterResponse, String, @escaping () -> Void) throws -> Void

    Parameters

    request

    The RouterRequest object used to work with the incoming HTTP request.

    response

    The RouterResponse object used to respond to the HTTP request.

    param

    The named parameter to be handled.

    next

    The closure called to invoke the next handler or middleware associated with the request.

RouterResponse

  • Type alias for “Before flush” (i.e. before headers and body are written) lifecycle handler.

    Declaration

    Swift

    public typealias LifecycleHandler = () -> Void
  • Type alias for written data filter, i.e. pre-write lifecycle handler.

    Declaration

    Swift

    public typealias WrittenDataFilter = (Data) -> Data