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:isLoggingEnabled:)
public convenience init(port: Int = 8888, isLoggingEnabled: Bool = false)
Creates an instance of an HTTP
Server
.
Parameters
Name | Type | Description |
---|---|---|
port | Int |
|
isLoggingEnabled | Bool |
|
Properties
group
let group
router
let router
logger
let logger: LoggerType
requestHandler
let requestHandler: RequestHandler
host
let host
port
let port: Int
isRunning
var isRunning
Methods
start()
func start() throws
Starts the Server
asynchronously.
syncStart()
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.
startChannel()
@discardableResult func startChannel() throws -> Channel