Server-Side Swift
54.1 Vapor
Vapor ecosystem: Fluent ORM (PostgreSQL, MySQL, SQLite, MongoDB), Leaf templates, JWT, WebSockets, queues, middleware.
// Package: .package(url: "https://github.com/vapor/vapor.git", from: "4.99.0")
import Vapor
struct Todo: Content { // Content = Codable for request/response bodies
var id: UUID?
var title: String
var done: Bool
}
func routes(_ app: Application) throws {
app.get("hello") { _ in "Hello, Vapor!" }
app.get("todos", ":id") { req async throws -> Todo in
guard let id = req.parameters.get("id", as: UUID.self) else { throw Abort(.badRequest) }
return Todo(id: id, title: "Learn Swift", done: false)
}
app.post("todos") { req async throws -> Todo in
var todo = try req.content.decode(Todo.self)
todo.id = UUID()
return todo
}
}
@main
enum Entrypoint {
static func main() async throws {
let app = try await Application.make(.detect())
try routes(app)
try await app.execute()
try await app.asyncShutdown()
}
}54.2 Hummingbird and the SSWG Ecosystem
Swift Server Work Group (SSWG) libraries: SwiftNIO (non-blocking networking), AsyncHTTPClient, swift-log, swift-metrics, swift-distributed-tracing, swift-service-lifecycle, PostgresNIO/MySQLNIO/RediStack, gRPC Swift, Soto (AWS SDK), swift-openapi-generator (server stubs), Distributed Actors (distributed actor for clustering).
Deployment: Docker images (swift:6.0 builder → swift:slim runtime, or static Linux SDK binaries in scratch/distroless images), Kubernetes, AWS Lambda (swift-aws-lambda-runtime), Google Cloud Run.
import Hummingbird
let router = Router()
router.get("hello") { _, _ in "Hello, Hummingbird!" }
let app = Application(router: router, configuration: .init(address: .hostname("0.0.0.0", port: 8080)))
try await app.runService()