This is a swift http server (library) mainly depends on IBM BlueSocket (https://github.com/IBM-Swift/BlueSocket).
$ swift --version
Swift version 4.2.3 (swift-4.2.3-RELEASE)
Target: x86_64-unknown-linux-gnu
$ swift --version
Swift version 5.5 (swift-5.5-RELEASE)
Target: x86_64-unknown-linux-gnu
- BlueSocket: https://github.com/IBM-Swift/BlueSocket
swift build
swift test
Add it to dependency (package.swift)
dependencies: [
.package(url: "https://github.com/bjtj/swift-http-server.git", from: "0.1.20"),
],
Import package into your code
import SwiftHttpServer
let server = HttpServer(port: 0)
class GetHandler: HttpRequestHandler {
var dumpBody: Bool = true
func onHeaderCompleted(header: HttpHeader, request: HttpRequest, response: HttpResponse) throws {
}
func onBodyCompleted(body: Data?, request: HttpRequest, response: HttpResponse) throws {
response.status = .ok
response.data = "Hello".data(using: .utf8)
}
}
try server.route(pattern: "/", handler: GetHandler())
let queue = DispatchQueue.global(qos: .default)
queue.async {
do {
try server.run()
} catch let error {
print(error)
}
}