docs/Classes/Kitura.html
public class Kitura
Facilities for creating, starting and stopping Kitura-based servers.
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
`
addHTTPServer(onPort:onAddress:with:withSSL:keepAlive:allowPortReuse:options:)
`
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.
let router = Router()
Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
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
| 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.
|
The created HTTPServer.
`
addHTTPServer(onUnixDomainSocket:with:withSSL:keepAlive:options:)
`
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.
let router = Router()
Kitura.addHTTPServer(onUnixDomainSocket: "/tmp/mySocket", with: router)
Swift
@discardableResult
public class func addHTTPServer(onUnixDomainSocket socketPath: String,
with delegate: ServerDelegate,
withSSL sslConfig: SSLConfig?=nil,
keepAlive keepAliveState: KeepAliveState = .unlimited,
options: ServerOptions? = nil) -> HTTPServer
| 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.
|
The created HTTPServer.
`
addFastCGIServer(onPort:onAddress:with:allowPortReuse:)
`
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.
let router = Router()
Kitura.addFastCGIServer(onPort: 8080, onAddress: "localhost", with: router)
Swift
@discardableResult
public class func addFastCGIServer(onPort port: Int,
onAddress address: String? = nil,
with delegate: ServerDelegate,
allowPortReuse: Bool = false) -> FastCGIServer
| 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.
|
The created FastCGIServer.
Start Servers
`
run(exitOnFailure:)
`
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.
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.
Swift
public class func run(exitOnFailure: Bool = true)
| 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()
`
Start all registered servers and return.
Make all registered servers start listening on their port.
let router = Router()
Kitura.addHTTPServer(onPort: 8080, onAddress: "localhost", with: router)
Kitura.start()
Swift
public class func start()
`
wait()
`
Wait on all registered servers.
let failures = Kitura.startWithStatus()
if failures == 0 {
Kitura.wait()
else {
// handle failures
}
Swift
public class func wait()
`
startWithStatus()
`
Start all registered servers and return the number of servers that failed to start.
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.
Swift
public class func startWithStatus() -> Int
Stop Servers
`
stop(unregister:)
`
Stop all registered servers.
Make all registered servers stop listening on their port.
let router = Router()
Kitura.addHTTPServer(onPort: 8080, with: router, address: "localhost")
Kitura.start()
Kitura.stop()
Swift
public class func stop(unregister: Bool = true)
| unregister |
If servers should be unregistered after they are stopped (default true).
|
`
logTo(_:)
`
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)
Swift
public static func logTo(_ logger: Logging.Logger)