Kitura

public class Kitura

Facilities for creating, starting and stopping Kitura-based servers.

Usage Example:

In this example, a Router is created, and a single route registered that responds to an HTTP GET request on “/” with a plain text response. An HTTP server is created on port 8080, and is started with the Kitura.run() function (note that this function does not return). The route can then be accessed by visiting http://localhost:8080.

let router = Router()
router.get("/") { request, response, next in
    response.send("Hello world")
    next()
}
Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
Kitura.run()

Create Server

  • Add an HTTPServer on a port with a delegate.

    The server is only registered with the framework, it does not start listening on the port until Kitura.run() or Kitura.start() are called.

    Usage Example:

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
    

    Declaration

    Swift

    @discardableResult
    public class func addHTTPServer(onPort port: Int,
                                    onAddress address: String? = nil,
                                    with delegate: ServerDelegate,
                                    withSSL sslConfig: SSLConfig?=nil,
                                    keepAlive keepAliveState: KeepAliveState = .unlimited,
                                    allowPortReuse: Bool = false,
                                    options: ServerOptions? = nil) -> HTTPServer

    Parameters

    onPort

    The port to listen on.

    onAddress

    The address to listen on, for example “localhost”. The default is nil, which listens on all addresses.

    with

    The ServerDelegate to use.

    withSSL

    The sslConfig to use.

    keepAlive

    The maximum number of additional requests to permit per Keep-Alive connection. Defaults to .unlimited. If set to .disabled, Keep-Alive will not be permitted.

    allowPortReuse

    Determines whether the listener port may be shared with other Kitura instances (SO_REUSEPORT). Defaults to false. If the specified port is already in use by another listener that has not allowed sharing, the server will fail to start.

    options

    Allows customization of default policies for this server.

    Return Value

    The created HTTPServer.

  • Add an HTTPServer on a Unix domain socket path with a delegate.

    The server is only registered with the framework, it does not start listening on the Unix socket until Kitura.run() or Kitura.start() are called.

    Usage Example:

     let router = Router()
     Kitura.addHTTPServer(onUnixDomainSocket: "/tmp/mySocket", with: router)
    

    Declaration

    Swift

    @discardableResult
    public class func addHTTPServer(onUnixDomainSocket socketPath: String,
                                    with delegate: ServerDelegate,
                                    withSSL sslConfig: SSLConfig?=nil,
                                    keepAlive keepAliveState: KeepAliveState = .unlimited,
                                    options: ServerOptions? = nil) -> HTTPServer

    Parameters

    onUnixDomainSocket

    The path of the Unix domain socket to listen on.

    with

    The ServerDelegate to use.

    withSSL

    The sslConfig to use.

    keepAlive

    The maximum number of additional requests to permit per Keep-Alive connection. Defaults to .unlimited. If set to .disabled, Keep-Alive will not be permitted.

    Return Value

    The created HTTPServer.

  • Add a FastCGIServer on a port with a delegate.

    The server is only registered with the framework, it does not start listening on the port until Kitura.run() or Kitura.start() are called.

    Usage Example:

     let router = Router()
     Kitura.addFastCGIServer(onPort: 8080, onAddress: "localhost", with: router)
    

    Declaration

    Swift

    @discardableResult
    public class func addFastCGIServer(onPort port: Int,
                                       onAddress address: String? = nil,
                                       with delegate: ServerDelegate,
                                       allowPortReuse: Bool = false) -> FastCGIServer

    Parameters

    onPort

    The port to listen on.

    onAddress

    The address to listen on, for example “localhost”. The default is nil, which listens on all addresses.

    with

    The ServerDelegate to use.

    allowPortReuse

    Determines whether the listener port may be shared with other Kitura instances (SO_REUSEPORT). Defaults to false. If the specified port is already in use by another listener that has not allowed sharing, the server will fail to start.

    Return Value

    The created FastCGIServer.

Start Servers

  • Start the Kitura framework. By default, the Kitura framework process will exit if one or more of the servers fails to start. To prevent the Kitura framework process from exiting with set the exitOnFailure parameter to false.

    Usage Example:

    Make all registered servers start listening on their port.

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
     Kitura.run()
    

    Make all registered servers start listening on their port and exit if any fail to start.

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, with: router)
     Kitura.run(exitOnFailure: false)
    

    Note

    This function never returns - it should be the last call in your main.swift file.

    Declaration

    Swift

    public class func run(exitOnFailure: Bool = true)

    Parameters

    exitOnFailure

    Determines whether the Kitura process can return a non-zero exit code should any of the servers fail to start. Defaults to true, indicating it will exit if any of the servers fail to start.

  • Start all registered servers and return.

    Usage Example:

    Make all registered servers start listening on their port.

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
     Kitura.start()
    

    Declaration

    Swift

    public class func start()
  • Wait on all registered servers.

    Usage Example:

     let failures = Kitura.startWithStatus()
     if failures == 0 {
       Kitura.wait()
     else {
       // handle failures
     }
    

    Declaration

    Swift

    public class func wait()
  • Start all registered servers and return the number of servers that failed to start.

    Usage Example:

    Make all registered servers start listening on their port.

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
     Kitura.startWithStatus() // Returns the number of failed server starts.
    

    Declaration

    Swift

    public class func startWithStatus() -> Int

Stop Servers

  • Stop all registered servers.

    Usage Example:

    Make all registered servers stop listening on their port.

     let router = Router()
     Kitura.addHTTPServer(onPort: 8080, with: router, address: "localhost")
     Kitura.start()
     Kitura.stop()
    

    Declaration

    Swift

    public class func stop(unregister: Bool = true)

    Parameters

    unregister

    If servers should be unregistered after they are stopped (default true).

  • Instructs Kitura and LoggerAPI to log messages to a Swift Logger that you provide. Usage example:

    import Kitura
    import Logging
    
    var logger = Logger(label: "MyLogger")
    logger.logLevel = .debug
    Kitura.logTo(logger)
    

    Declaration

    Swift

    public static func logTo(_ logger: Logging.Logger)