Titan is an extensible, powerful & easy-to-use microframework for server-side Swift.
Write and run production web apps & services under Linux or Docker in a convenient way.
Swift 3 is no longer supported, but exists under the 0.7.x tags.
- very modular with a light-weight core for routing & JSON
- add features with plug & play middleware packages
- functional design which makes it easy to write own middleware
- use different webservers like Kitura or Swift-Server/HTTP
- incredibly fast due to its light-weight design
- built for latest Swift 4, Docker & Linux
- conceptually similar to the powerful & modular frameworks Express.js or Flask
The following example has the following features:
- some routes to demo parameters, different request methods & wildcard routes
- demo of how simply it is to have a middleware manipulate the response
- uses Kitura as high-performance webserver
Package.swift:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "mywebapp",
products: [
.executable(name: "mywebapp", targets: ["mywebapp"]),
],
dependencies: [
.package(url: "https://github.com/bermudadigitalstudio/TitanKituraAdapter.git", from: "0.8.0"),
.package(url: "https://github.com/bermudadigitalstudio/Titan", from: "0.8.0")
],
targets: [
.target(
name: "mywebapp",
dependencies: [
"TitanKituraAdapter",
"Titan"
]),
]
)
main.swift:
import Titan
import TitanKituraAdapter
let app = Titan()
/// The Response is set to 404 by default.
/// if no subsequent routing function is called, a 404 will be returned
app.addFunction(DefaultTo404)
/// Hello World, req is sent to next matching route
app.get("/") { req, _ in
return (req, Response(200, "Hello World")) // here we "overwrite" the 404 that was returned in the previous func.
}
/// 2 parameters in URL
app.delete("/item/*/subitem/*") { req, param1, param2, _ in
let text = "I will delete \(param2) in \(param1)"
return(req, Response(200, text))
}
/// parse JSON sent via POST, return 400 on parsing error
app.post("/data") { req, _ in
guard let json = req.json as? [String: Any] else {
return (req, Response(400))
}
return(req, Response(200, "I received \(json)"))
}
/// let’s manipulate the response of all GET routes
/// and yes, that is already a simple example for a middleware!
app.get("*") { req, res in
var newRes = res.copy() // res is a constant, so we need to copy
newRes.body += " and hello from the middleware!"
return (req, newRes) // will return "Hello World and hello from the middleware!"
}
// start the Kitura webserver on port 8000
TitanKituraAdapter.serve(app.app, on: 8000)
You can now run the webserver and open http://localhost:8000 or http://localhost:8000/item/foo/subitem/bar or send JSON via POST to http://localhost:8000/data
Execute Scripts/test.sh
to run all unit-tests inside a Docker container.
Titan is maintained by Thomas Catterall (@swizzlr), Johannes Erhardt (@johanneserhardt) and Sebastian Kreutzberger (@skreutzberger).
Contributions are more than welcomed. You can either work on existing Github issues or discuss with us your ideas in a new Github issue. Thanks 🙌
Titan was initially developed in a project run with Bermuda Digital Studio (BDS) Germany. BDS is a team devoted to enable digital product management, design and development for the retail business of the renewable energies corp innogy SE. The goal is to digitize and disrupt energy.
Titan Framework is released under the Apache 2.0 License.