A tiny Sinatra-like web framework for Swift.
Swiftra is a small wrapper on SwiftNIO.
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")
Add https://github.com/mtgto/Swiftra
to your dependencies of Swift Package.
Add pod 'Swiftra', '~> 0.3'
to your Podfile.
GET
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
Thanks a lot A Β΅Tutorial on SwiftNIO 2.
Apache License 2.0.
See LICENSE.txt
.
@mtgto
link |
Stars: 2 |
Last commit: 2 years ago |
@resultHandler
)Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics