Swiftpack.co - Package - mtgto/Swiftra
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

Swiftra

Cocoapods Platforms build status

A tiny Sinatra-like web framework for Swift.

Swiftra is a small wrapper on SwiftNIO.

Example

import Swiftra

struct ExampleError: Error {}

let app = App {
    get("/") { req in
        .text("Hello, world!")
    }

    get("/html") { req in
        .text("<html><body>Hello from Swiftra</body></html>", contentType: ContentType.textHtml.withCharset())
    }

    // path parameters
    get("/hello/:name") { req in
        .text("Hello \(req.params("name", default: "guest"))")
    }

    // convert Encodable value into JSON
    get("/json") { req in
        Response(json: ["Hello": "World!"])!
    }

    // asynchronous
    futureGet("/future") { req in
        let promise = req.makePromise(of: String.self)
        _ = req.eventLoop.scheduleTask(in: .seconds(1)) {
            promise.succeed("Hello from future")
        }
        return promise.futureResult.map { .text($0) }
    }

    // No route matches
    notFound { req in
        .text("Not Found", status: .notFound)
    }

    // Example of error handling. See also below `error`
    get("/error") { req in
        throw ExampleError()
    }

    // unhandled error handler
    error { req, error in
        .text("Error", status: .internalServerError)
    }
}

// You can add routes
app.addRoutes {
    get("/addRoute") { req in
        .text("New route is added")
    }
}

// Set default response headers
app.defaultHeaders = [("Server", "SwiftraExample/1.0.0")]

try! app.start(1337)
// You can customize bind address
// try! app.start(1337, host: "localhost")

Installation

Swift Package Manager (SPM)

Add https://github.com/mtgto/Swiftra to your dependencies of Swift Package.

Cocoapods

Add pod 'Swiftra', '~> 0.3' to your Podfile.

Features

  • Routing
  • Access path parameter(s)
  • Access query paramter(s)

Roadmap to v1.0.0

  • ☐ Helper to access components of request header
    • ☑ query string
    • ☐ fragment
    • ☐ Cookie
  • ☐ Helper to access request body
    • ☐ x-www-form-urlencoded data
    • ☐ JSON request
  • ☐ HTTP methods except for GET
  • ☑ Error handling
  • ☐ File upload
    • ☐ Large file upload
  • ☐ More performance

Contributing

Format swift code with swift-format before send your contribution.

$ swift-format --configuration .swift-format --in-place YourAwesomeCode.swift

pre-commit is recommended.

$ brew install pre-commit
$ pre-commit install

Acknowledgements

Thanks a lot A µTutorial on SwiftNIO 2.

License

Apache License 2.0.

See LICENSE.txt.

Author

@mtgto

Github

link
Stars: 0

Dependencies

Releases

0.3.1 - 2021-01-01T14:50:08

Changed

  • Call error handler for future handler
  • Add missing argument of headers to create json response

0.3.0 - 2020-12-28T07:27:23

Added

  • Add accessor to set response headers
  • Support default response headers

Changed

  • Set charset=utf-8 to Content-Type header by default

0.2.1 - 2020-12-26T12:34:36

Changed

  • Fix status code of response code is always 200 OK

- 2020-12-25T23:28:10

Added

  • Support query params
  • Add error handler
  • Add default handler to handle no route is matched
  • Add tests of Request

0.1.1 - 2020-12-20T03:51:06

Changed

  • Fix to create EventLoopGroup each app start

0.1.0 - 2020-12-20T03:12:06

First Release