Declarative
Server-Side Swift.

Declarative at Every Layer.

  1. Endpoints

    Declare inputs, limits and documentation in one place.

  2. Real-time

    Stream server-sent events or WebSocket messages from a regular handler.

  3. Pages

    Compose pages from stacks, spacers and modifiers.

  4. Services

    Declare services and their dependencies. Startup order and graceful shutdown follow from the declaration.

@Handler(method: .GET, path: "greet/:name")
struct GetGreet {

  @PathParameter("name")
  var name: String

  func handle() async throws -> String {
      "Hello, \(name)!"
  }
}
@Handler(method: .GET, path: "prices")
struct GetPrices {

    func handle() async throws -> SSEResponse {
        SSEResponse { continuation in
            for await price in priceFeed {
                await continuation.yield(data: price, event: "price")
            }
            continuation.finish()
        }
    }
}
struct ProfileCard: View {
    let name: String

    var body: some View {
        HStack {
            Image()
                .src("/avatar.jpg")
                .frame(width: ._13, height: ._13)
                .borderRadius(.percent(._100))

            Text(string: name)
                .fontSize(.headline)

            Spacer()
        }
        .gap(._6)
        .padding(._7)
        .backgroundColor(.secondaryGroupedBackground)
    }
}
let app = WebServiceApp {
    GreetService {
        HTTP(host: "0.0.0.0", port: 8080) {
            Logging.resolve
        }

        WebUI {
            HTTP.resolve
        }
    }
}

try await app.run()

Web Apps Without a Frontend Toolchain.

LikeButton.swift
struct LikeButton: View {
    let likes: Int

    var body: some View {
        HStack {
            Text(string: "\(likes) likes")
                .fontWeight(.bold)

            Button("Like")
                .type(.button)
                .title("Like this post")
                .hxPost("/like")
                .hxSwap(.outerHTML)
        }
        .gap(._4)
        .padding(._6)
        .backgroundColor(.secondaryGroupedBackground)
        .borderRadius(._5)
    }
}
  • WHATWG Complete

    Every element and attribute of the WHATWG spec, as Swift.

  • Batteries Included Design System

    Layout, type and color, built in.

  • First-Class HTMX Experience

    Typed htmx attributes update the page from the server.

Fast.

Static Response1

HTTP throughput for a static response.

Vapor 130,000 req/s
Swift WebServices 122,000 req/s
Hummingbird 104,000 req/s

With Cookie Parsing1

HTTP throughput on an endpoint that parses cookies.

Swift WebServices 114,000 req/s
Hummingbird 84,000 req/s
Vapor 73,000 req/s

Rendering a Dynamic Page3

A table rendered from data on every request.

WebUICore 54,618 pages/s
Elementary 30,789 pages/s
Vapor HTMLKit* 30,589 pages/s
LeafKit* 2,345 pages/s

Rendering a Static Page3

The same page on every request.

LeafKit* 142,678 pages/s
WebUICore 109,534 pages/s
Vapor HTMLKit* 98,023 pages/s
Elementary 77,066 pages/s

Parsing HTTP Dates2

RFC 9110 dates, as found in headers and cookies.

WebServiceHTTP 1,677,728 ops/s
Foundation 29,067 ops/s

Parsing Locales2

BCP 47 language tags, for localized responses.

WebUICore 1,340,929 ops/s
Foundation 229,338 ops/s

Use Only What You Need.

  • WebService Runs your app and its services.
  • WebServiceCore Service and plugin protocols.
  • WebServiceLogging Logging through swift-log.
  • WebServiceHTTP HTTP server and declarative endpoints.
  • WebServiceWS WebSockets.
  • WebServiceSSE Server-sent events.
  • WebUICore Typed HTML, complete to the WHATWG spec.
  • WebUI Design system and layout.
  • HTMX Typed htmx attributes.

Get Started.

.package(url: "https://github.com/antonlorani/swift-webservices.git", from: "0.36.0")
  1. HTTP throughput in 10-second runs with 10 threads and 256 connections. Endpoints are ordered by growing complexity, from a static response to cookie parsing. Methodology and source
  2. Operations per second. HTTP dates per RFC 9110, locales per BCP 47.
  3. Pages per second. WebUICore renders every page from scratch; entries marked * serve from a stateful cache.
  4. Licensed under LGPL-3.0 with a linking exception, so closed-source apps can use it.