SwiftHttpServer

master

Swift HTTP Server
bjtj/swift-http-server

SwiftHttpServer

This is a swift http server (library) mainly depends on IBM BlueSocket (https://github.com/IBM-Swift/BlueSocket).

Github Build Status Build Status

Swift version

$ 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

Dependencies

Build, Test

swift build
swift test

How to use it?

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

Example

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)
    }
}

Description

  • Swift Tools 4.2.0
View More Packages from this Author

Dependencies

Last updated: Thu Jan 19 2023 14:43:55 GMT-0500 (GMT-05:00)