This commit contains the latest updates from the generic Website template, which move the toolchain to Swift 6.4, gate analytics and structured data behind traits, and rework page routing: - Updated the Swift toolchain for all the package dependencies and the Website service to version 6.4. (345b305) - Adapted all the existing middlewares in the Infrastructure package to Swift 6.4. (26008df) - Moved test resource teardown in the Persistence and the Website library into defer blocks. (27984a9) - Gated analytics behind a trait and reworked structured data in the Infrastructure package. (c7d4769) - Enabled the Analytics trait in the Website service. (f6a6bbd) - Reworked page routing in the Website service. (d1cb2c1) - Gated the structured data features in the Infrastructure package behind a trait. (f0a8791) - Enabled the StructuredData trait of the Infrastructure package in the Website service. (bbfdeb3) - Fixed the asset pin and a build warning in the Dockerfile of the Website service. (6ed0f9b) The root README change in 345b305 was left out, as this project no longer carries that file. From d1cb2c1, the RootController rework was left out, as the prototype serves a session-aware landing page and routes its other pages through their own controllers; the view-event reporter was appended to the prototype's shared script instead. The prototype code was adapted to the stricter Swift 6.4 settings: `String(format:)` was swapped for safe formatting, the NoIndex middleware takes a `@concurrent` next handler, and the tests flagged by warnings-as-errors were tightened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6.2 KiB
6.2 KiB
Infrastructure
The shared Hummingbird toolkit the platform's services build on: declarative routing, hardened HTTP middlewares, pre-rendered and pre-compressed localized responses, and page and asset scaffolding.
Overview
| Role | Types |
|---|---|
| Routing | RouterController, RouteCollectionBuilder, the addController extension on RouterMethods |
| Middlewares | SecurityHeadersMiddleware, HTTPSRedirectMiddleware, TrailingSlashRedirectMiddleware, VaryMiddleware, CompressionMiddleware, RateLimitMiddleware, NotFoundMiddleware |
| Pages and assets | Page, Asset, AssetExtension, FingerprintAssets |
| Link previews | SocialCard, its locale and the alternateLocales of its other language editions, its Image and Style, and the Tag meta tags it derives |
| Structured data | Opt-in StructuredData trait: StructuredData, its graph types (Node, Property, Value, Address, Organization), the open Name and Kind vocabularies, and the site-wide Organization/WebSite initializer |
| Analytics | Opt-in Analytics trait: Analytics, the Events a page reports (with reporting(_:on:) to tag an element, for a click or a view), the tracker script Attributes it derives, the optional session recorder script paired with it, and the origin its preconnect hint targets |
| Responses | CachedHTMLResponse (rendered and gzipped once), LocalizedHTMLCollectionResponse (one of those per language) |
| Localization | The Negotiate extension resolving a request's language from its lang override, path prefix, and Accept-Language |
| Constants | The HTTPField.Name header names, Int.RateLimit limits, and String.Security header values the middlewares default to |
Design rules
The package holds only what every service can reuse. Anything a service owns is injected, never referenced.
- No site-specific content. No page markup, no asset catalog, no
Bundle.modulelookups. What a type needs, it takes as a parameter: thebundle:whose String Catalog names the supported languages, thedocument:closure that builds a page for a locale, aPageconformer'smetadataand its optional head concerns (summary,canonicalURL,socialCard,structuredData,analytics). URLs arrive absolute and fully formed — composing them stays with the page. The same holds for request semantics: only the caller knows whether it negotiates the language or pins it by route, sovariesOnAcceptLanguage:declares whether the responses carryVary: Accept-Language. - Types own their format;
Pagerenders generically. Each head concern derives its own render-ready form —SocialCard.tags,StructuredData.payload,Analytics.attributes— whichPageapplies without knowing the vocabulary. A page'sscriptsand the tracker render asdeferred head tags, the tracker preceded by apreconnectto its cross-origin host. - Nodes are joined by
@id, not repetition. Identifiers come from helpers, never hand-spelled:organizationID(forSiteURL:)andwebsiteID(forSiteURL:)name the site-wide nodes, andOrganization.foundertakes one back. A service adds a helper for each node it owns. - Services fill the gaps once, via extensions. A service restores its convenient call sites retroactively — the Website's
*+Defaultsare the pattern. The open schema.org vocabularies work the same way: the package declares the sharedProperty.NameandNode.Kindconstants, a service adds its own. - Method structs. Single-operation types such as
FingerprintAssetstake lifetime-fixed configuration ininitand per-call inputs incallAsFunction. - Wrap upstream, do not restate it. Where a Hummingbird middleware is almost right, the package delegates to it and adds only the missing decision —
CompressionMiddlewarepasses everything toResponseCompressionMiddlewareexcept responses that already name an encoding, which it would otherwise compress twice. - Ask where the answer is used. Work every request pays for must be work every request needs. The request's language is negotiated by the responders that read it, not stamped onto a context on the way past.
Layout
Sources are split by visibility, then by kind, one type per file:
Sources/
├── Public/ public API
│ ├── Builders/ RouteCollectionBuilder
│ ├── Enumerations/ AssetExtension
│ ├── Extensions/ addController, Analytics.Event tagging and reporting, and the request-language negotiation, plus the default header names, rate limits, and header values
│ ├── Methods/ FingerprintAssets
│ ├── Middlewares/ the seven HTTP middlewares
│ ├── Protocols/ Asset, Page, RouterController
│ ├── Responses/ CachedHTMLResponse, LocalizedHTMLCollectionResponse
│ └── Types/ Analytics, SocialCard, StructuredData — each nesting its own types in a folder of that name
└── Internal/
├── Extensions/ implementation details (the String separators)
└── Types/ implementation details (FNV1aHash)
Tests/
├── Cases/ the test suites, mirroring the Sources/ layout
├── Catalogs/ the String Catalog fixture, copied verbatim so it loads on Linux
└── Utils/ stubs (StubAsset, StubPage, …) and the suite Tag constants
Testing
Every suite carries a tag for the kind of API it exercises — .asset, .extension, .middleware, .protocol, .type — declared in Tests/Utils/Extensions/Tag+Constants.swift, so a run can be sliced by kind. A new suite takes the tag matching its subject, or adds one when none fits.
Run swift test --traits Analytics,StructuredData to include the trait-gated suites.
Requirements
- Swift 6.4 toolchain (
swift-tools-version:6.4). - macOS 15, matching the sibling packages. The services deploy to Linux containers; the packages declare no UI platforms.
- Package dependencies: the local
Localizationpackage,elementary,hummingbird, and — forCompressionMiddlewareand the responses' gzip —hummingbird-compressionandcompress-nio.