This PR contains the work done to provide optimizations to the current service, such as a health-check endpoint, pre-renders static HTML pages, and hardens the error page's CSP. To provide further details about the work: * Added the `HealthController` controller serving GET `/health` with a static JSON payload. * Added the `CachedHTMLResponse` response, which renders a static HTMLDocument to bytes once and reuses them per request (no Content-Length, so responses stay compressible). * Integrated the response into the `RootController` and the `NotFoundMiddleware` middleware to avoid re-rendering on hot paths. * Added a `RouterMethods.addRoutes(_:)` extension and switched the router in App+build to use it. * Moved the inline style from the `ErrorPage` page into a dedicated style file so the CSP needs no inline-style escape hatch. * Fixed the `IndexPage` page path inconsistencies. * Written the `README` file. Reviewed-on: rock-n-code/loud-amsterdam#9 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
55 lines
1.9 KiB
Swift
55 lines
1.9 KiB
Swift
import Elementary
|
|
import Hummingbird
|
|
import NIOCore
|
|
|
|
/// A pre-rendered HTTP response for a fully static HTML page.
|
|
///
|
|
/// The document is rendered to bytes once, at initialization, and every ``response()`` reuses those
|
|
/// bytes instead of re-rendering. This suits pages whose markup never changes between requests — the
|
|
/// landing page and the not-found page — avoiding a per-request Elementary render on hot paths.
|
|
///
|
|
/// The body is written as an unsized stream (no `Content-Length`), mirroring `HTMLResponse`, so the
|
|
/// response-compression middleware downstream treats it exactly as it would a freshly rendered page.
|
|
struct CachedHTMLResponse: Sendable {
|
|
|
|
// MARK: Properties
|
|
|
|
/// The status applied to every response.
|
|
private let status: HTTPResponse.Status
|
|
/// The page rendered to bytes once.
|
|
private let buffer: ByteBuffer
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Renders the given document to bytes once.
|
|
/// - Parameters:
|
|
/// - status: the status applied to every response. Defaults to `.ok`.
|
|
/// - document: the static HTML document to render and cache.
|
|
init(
|
|
status: HTTPResponse.Status = .ok,
|
|
_ document: some HTMLDocument
|
|
) {
|
|
self.status = status
|
|
self.buffer = ByteBuffer(string: document.render())
|
|
}
|
|
|
|
// MARK: Methods
|
|
|
|
/// Builds a response from the cached, pre-rendered bytes.
|
|
///
|
|
/// Mirrors the `text/html; charset=utf-8` content type `HTMLResponse` produces, and leaves the
|
|
/// `Content-Length` unset so small pages remain eligible for compression.
|
|
/// - Returns: the response carrying the cached HTML body.
|
|
func response() -> Response {
|
|
Response(
|
|
status: status,
|
|
headers: [.contentType: "text/html; charset=utf-8"],
|
|
body: .init { [buffer] writer in
|
|
try await writer.write(buffer)
|
|
try await writer.finish(nil)
|
|
}
|
|
)
|
|
}
|
|
|
|
}
|