Swiftpack.co - Watchants/snake-http as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Watchants.
Watchants/snake-http 0.5.1
🚀 Non-blocking, event-driven HTTP built on Swift NIO.
⭐️ 0
🕓 3 weeks ago
.package(url: "https://github.com/Watchants/snake-http.git", from: "0.5.1")

snake-http

🚀 Non-blocking, event-driven HTTP built on Swift NIO.

Installation


let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/Watchants/snake-http.git", from: "0.1.1"),
    ],
    ...
)

Usage

start

let bootstrap = Bootstrap(
    configuration: .init(host: "127.0.0.1", port: 8889),
    eventLoopGroup: .init(numberOfThreads: System.coreCount)
)
try bootstrap.start().wait()
bootstrap.printAddress()
try bootstrap.onClose.wait()

adding api

class GettingController: RequestController, MappingProtocol {
    
    let mapping: String = "/get"
    
    @RequestMapping("", { request, channel, token in
        let promise = channel.eventLoop.makePromise(of: MessageResponse.self)
        let response = MessageResponse(head: .ok, body: .init(json: []))
        promise.succeed(response)
        return promise.futureResult
    })
    var root: String
}

class UserController: RequestController, MappingProtocol {
    
    let mapping: String = "/user"
    
    @RequestMapping("/info", { request, channel, token in
        let promise = channel.eventLoop.makePromise(of: MessageResponse.self)
        let response = MessageResponse(head: .ok, body: .init(json: [
            "name": "tom",
            "age": 20,
        ]))
        promise.succeed(response)
        return promise.futureResult
    })
    var info: String
    
    @RequestMapping("/update", method: [.POST], { request, channel, token in
        let promise = channel.eventLoop.makePromise(of: MessageResponse.self)
        let response = MessageResponse(head: .ok, body: .init(json: []))
        promise.succeed(response)
        return promise.futureResult
    })
    var update: String
    
    @RequestMapping("/{id}", { request, channel, token in
        let promise = channel.eventLoop.makePromise(of: MessageResponse.self)
        let response = MessageResponse(head: .ok, body: .init(json: [
            "name": "tom",
            "age": 20,
        ]))
        promise.succeed(response)
        return promise.futureResult
    })
    var info: String
}

GitHub

link
Stars: 0
Last commit: 3 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

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