HTTP component
HTTP component With Swift
Installation
Swift
Download and install Swift
Swift Package
mkdir MyApp
cd MyApp
swift package init --type executable // Creates an executable app named "MyApp"
Package.swift
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(name: "chaqmoq-http", url: "https://github.com/chaqmoq/http.git", .branch("master"))
],
targets: [
.target(name: "MyApp", dependencies: [
.product(name: "HTTP", package: "chaqmoq-http"),
]),
.testTarget(name: "MyAppTests", dependencies: [
.target(name: "MyApp")
])
]
)
Build
swift build -c release
Usage
import HTTP
let server = Server()
server.onStart = { _ in
print("Server has started")
}
server.onStop = {
print("Server has stopped")
}
server.onError = { error, _ in
print("Error: \(error)")
}
server.onReceive = { request, _ in
// Return String, Response, EventLoopFuture<String>, EventLoopFuture<Response>, etc
}
try server.start()
onReceive
// String
server.onReceive = { request, _ in
"Hello World"
}
// Response
server.onReceive = { request, _ in
Response("Hello World")
}
// EventLoopFuture<String>
server.onReceive = { request, eventLoop in
// Some async operation that returns EventLoopFuture<String>
let promise = eventLoop.makePromise(of: String.self)
eventLoop.execute {
promise.succeed("Hello World")
}
return promise.futureResult
}
// EventLoopFuture<Response>
server.onReceive = { request, eventLoop in
// Some async operation that returns EventLoopFuture<Response>
let promise = eventLoop.makePromise(of: Response.self)
eventLoop.execute {
promise.succeed(Response("Hello World"))
}
return promise.futureResult
}
Tests
swift test --enable-test-discovery --sanitize=thread