Swiftpack.co - miguelfermin/async-http-server as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by miguelfermin.
miguelfermin/async-http-server 0.1.0
Simple HTTP Server built on SwiftNIO.
⭐️ 1
🕓 4 weeks ago
macOS
.package(url: "https://github.com/miguelfermin/async-http-server.git", from: "0.1.0")

AsyncHTTPServer

Simple HTTP Server built on top of SwiftNIO.

How to use


import AsyncHTTPServer

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)

let server = HTTPServer(eventLoopGroup: eventLoopGroup)

defer {
    try? eventLoopGroup.syncShutdownGracefully()
}

try server.listenAndServe(host: "localhost", port: 8000)

server.post("/v1/todo", function: createTodo)
server.get("/v1/todo", function: getTodoList)
server.get("/v1/todo/:id", function: getTodo)

func getTodoList(request: Request) async throws -> [TodoResponse] {
    ...
}

func getTodo(request: Request) async throws -> TodoResponse {
    guard let id = request.params["id"] else {
        throw TodoError(code: 14, message: "id required", statusCode: 400)
    }
    ...
}

func createTodo(request: Request) async throws -> CreateTodoResponse {
    let todo: CreateTodoRequest = try request.decodedBody()
    
    ...
}

References:

GitHub

link
Stars: 1
Last commit: 4 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics