Plugs on value types.
A plug is a function from connection to connection. Every step returns a new copy and never mutates shared state.
Composable HTTP middleware for Swift.
Inspired by Elixir's Plug. Each step takes a connection and returns a new one, so a request reads from top to bottom.
Nexus 2.0 · Swift 6 · Hummingbird or Vapor
import Nexus
import NexusRouter
let router = Router {
GET("/hello/:name") { conn in
conn.text("Hello, \(conn.params["name"] ?? "world")!")
}
}
let app = buildPipeline {
requestId()
requestLogger()
router
}
Three plugs. One response.
Part of Roost.
One Swift stack.
WHAT YOU GET
A plug is a function from connection to connection. Every step returns a new copy and never mutates shared state.
Chain plugs into pipelines, route with path parameters and scopes, and halt early instead of throwing HTTP errors.
Signed sessions, CSRF protection, static files, CORS, Basic auth, compression, request IDs, and logging ship as plugs.
NexusTest builds a connection in one line, so you call any plug or pipeline directly and check the result.
A CLOSER LOOK
Three short files. The whole idea.
Without a token, the plug answers 401 and halts, so later plugs are skipped. With one, it assigns the user to a new copy of the connection.
RequireToken.swiftimport Nexus
enum CurrentUser: AssignKey { typealias Value = String }
let requireToken: Plug = { conn in
guard let auth = conn.getReqHeader("Authorization"),
auth.hasPrefix("Bearer ") else {
return conn.respond(status: .unauthorized, body: .string("Missing token"))
}
let token = String(auth.dropFirst("Bearer ".count))
return conn.assign(CurrentUser.self, value: token)
}
Path parameters arrive on the connection. Scopes run their own plugs first, so the admin routes sit behind Basic auth.
API.swiftimport Nexus
import NexusRouter
struct User: Encodable, Sendable { let id: String; let name: String }
let api = Router {
GET("/users/:id") { conn in
let id = conn.params["id"] ?? ""
return try conn.json(value: User(id: id, name: "Ada"))
}
scope("/admin", through: [basicAuth { _, pass in pass == "s3cret" }]) {
GET("/stats") { conn in try conn.json(value: ["users": 1]) }
}
}
let app = pipeline([requestId(), requestLogger(), api.asPlug()])
NexusTest builds the connection. Call the plug or the whole app as a function and check what comes back.
AppTests.swiftimport Testing
import Nexus
import NexusTest
@Test func rejectsMissingToken() async throws {
let result = try await requireToken(TestConnection.build(path: "/me"))
#expect(result.isHalted)
#expect(result.response.status == .unauthorized)
}
@Test func findsUser() async throws {
let result = try await app(TestConnection.build(path: "/users/42"))
#expect(result.response.status == .ok)
#expect(result.requestId != nil)
}
INSTALL
Nexus is the core. Add NexusRouter for routing, NexusHummingbird or NexusVapor to serve requests, and NexusTest to your test target.
Swift 6.0+ · macOS 14+, iOS 17+ or Linux
Read the guide.package(url: "https://github.com/roost-framework/Nexus.git", from: "2.0.0")
// in your target's dependencies
.product(name: "Nexus", package: "Nexus"),
.product(name: "NexusRouter", package: "Nexus"),
.product(name: "NexusHummingbird", package: "Nexus")
// in your test target
.product(name: "NexusTest", package: "Nexus")
Build a pipeline
you can read.