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, with: router)
Kitura.run()
-
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()
orKitura.start()
are called.Usage Example:
let router = Router() Kitura.addHTTPServer(onPort: 8080, with: router)
Declaration
Swift
public class func addHTTPServer(onPort port: Int, with delegate: ServerDelegate, withSSL sslConfig: SSLConfig?=nil, keepAlive keepAliveState: KeepAliveState = .unlimited, allowPortReuse: Bool = false) -> HTTPServer
Parameters
onPort
The port 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.allowPortReuse
Determines whether the listener port may be shared with other Kitura instances (
SO_REUSEPORT
). Defaults tofalse
. 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
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()
orKitura.start()
are called.Usage Example:
let router = Router() Kitura.addFastCGIServer(onPort: 8080, with: router)
Declaration
Swift
public class func addFastCGIServer(onPort port: Int, with delegate: ServerDelegate, allowPortReuse: Bool = false) -> FastCGIServer
Parameters
onPort
The port to listen on.
with
The
ServerDelegate
to use.allowPortReuse
Determines whether the listener port may be shared with other Kitura instances (
SO_REUSEPORT
). Defaults tofalse
. 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 the Kitura framework.
Usage Example:
Make all registered servers start listening on their port.
let router = Router() Kitura.addHTTPServer(onPort: 8080, with: router) Kitura.run()
Note
This function never returns - it should be the last call in yourmain.swift
file.Declaration
Swift
public class func run()
-
Start all registered servers and return.
Usage Example:
Make all registered servers start listening on their port.
let router = Router() Kitura.addHTTPServer(onPort: 8080, with: router) Kitura.start()
Declaration
Swift
public class func start()
-
Stop all registered servers.
Usage Example:
Make all registered servers stop listening on their port.
let router = Router() Kitura.addHTTPServer(onPort: 8080, with: router) 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).