Part of the Roost family

One connection.
Many small steps.

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

App.swift
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
}
GET /hello/nexus200 OK
Hello, nexus!

Three plugs. One response.

Part of Roost.
One Swift stack.

Roost The web framework 2.1 Spectro PostgreSQL & migrations 2.1 ESW HTML, compiled to Swift 1.6

WHAT YOU GET

Small pieces.
Clear flow.

Plugs on value types.

A plug is a function from connection to connection. Every step returns a new copy and never mutates shared state.

Compose, route, halt.

Chain plugs into pipelines, route with path parameters and scopes, and halt early instead of throwing HTTP errors.

Batteries included.

Signed sessions, CSRF protection, static files, CORS, Basic auth, compression, request IDs, and logging ship as plugs.

Test without a server.

NexusTest builds a connection in one line, so you call any plug or pipeline directly and check the result.

A CLOSER LOOK

Plug, route,
test.

Three short files. The whole idea.

Halt, don't throw.

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.swift
import 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)
}

Routes are plugs too.

Path parameters arrive on the connection. Scopes run their own plugs first, so the admin routes sit behind Basic auth.

API.swift
import 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()])

No port required.

NexusTest builds the connection. Call the plug or the whole app as a function and check what comes back.

AppTests.swift
import 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

Add it to
your package.

Pick the pieces you need.

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.swift
.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.

Explore Nexus on GitHub