Resource‑centric Swift server framework

This pitch outlines an idea for a resource‑centric Swift server framework, intended for discussion and design exploration within SSWG. The core idea is to treat HTTP resources as first‑class types, rather than routing strings to handlers, addressing a common limitation of route‑centric frameworks where the HTTP resource model is implicit or obscured.

Inspired by the HTTP specification, the framework would model resources and their representations, with supported HTTP methods emerging naturally from resource capabilities.

Rather than registering routes, developers declare and compose resources hierarchically. Each resource explicitly defines which HTTP methods it supports and how representations are produced or consumed. The tree composition of URLs is inspired by SwiftUI.


Minimal example: product catalog microservice

@main
struct CatalogService: HTTPService {
    var resources: some ResourceTree {
        ResourceCollection<Product>("products") {

            /// This handles GET /products
            func get() async throws -> [Product] {
                ...
            }

            /// This handles POST /products
            func post(_ product: ProductDraft) async throws -> Product {
                ...
            }
        }
    }
}

struct Product: Resource {
    let id: UUID
    let name: String
    let price: Decimal
}

struct ProductDraft: Codable {
    let name: String
    let price: Decimal
}

Here, /products is a concrete resource. Its supported HTTP methods and representations are defined directly in code, making HTTP semantics explicit rather than encoded in external routing configuration.

The API is work in progress, suggestions are welcome.

2 Likes

As web framework styles often end up betting a matter of personal taste and preference I don’t think there’s anything the SSWG needs to be “reviewing” here in a pitch/review sense.

Sure, go for it if that’s a style of framework you’re interested in.

2 Likes

I’m sure you could model this on top of Vapor or Hummingbird. I don’t think you need a new server implementation. Both have a concept of route collections which could be used to collect the series of endpoints generated for each of your resource collection.

2 Likes