2026-07-23 01:04:37 +00:00
|
|
|
import Foundation
|
2026-06-28 05:07:19 +00:00
|
|
|
import Hummingbird
|
2026-07-22 21:26:55 +00:00
|
|
|
import Infrastructure
|
2026-06-28 05:07:19 +00:00
|
|
|
|
|
|
|
|
/// Serves the website's root routes.
|
|
|
|
|
///
|
2026-07-30 06:33:57 +00:00
|
|
|
/// The controller exposes its routes through its `RouterController` conformance, so the application that composes it registers them declaratively:
|
2026-06-28 05:07:19 +00:00
|
|
|
///
|
|
|
|
|
/// ```swift
|
2026-07-19 00:47:49 +00:00
|
|
|
/// router.addController {
|
|
|
|
|
/// RootController<AppRequestContext>()
|
|
|
|
|
/// }
|
2026-06-28 05:07:19 +00:00
|
|
|
/// ```
|
|
|
|
|
///
|
2026-07-30 06:33:57 +00:00
|
|
|
/// - Note: `Context` is the request context the routes are resolved against, and must match the context of the router the routes are added to.
|
2026-07-19 00:47:49 +00:00
|
|
|
public struct RootController<Context: LocalizedRequestContext> {
|
2026-06-28 14:18:04 +00:00
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
2026-06-29 23:08:36 +00:00
|
|
|
/// The landing page, rendered once per supported language and reused for every request.
|
|
|
|
|
private let responses: LocalizedHTMLCollectionResponse
|
2026-06-28 05:07:19 +00:00
|
|
|
|
|
|
|
|
// MARK: Initializers
|
|
|
|
|
|
|
|
|
|
/// Creates a root controller.
|
2026-07-30 06:33:57 +00:00
|
|
|
/// - Parameter assetVersion: the version token appended to the page's asset URLs, or `nil` (the default) to leave them unversioned.
|
2026-07-23 01:04:37 +00:00
|
|
|
public init(
|
|
|
|
|
assetVersion: String? = nil
|
|
|
|
|
) {
|
|
|
|
|
self.responses = .init(bundle: .module) {
|
|
|
|
|
IndexPage(
|
|
|
|
|
locale: $0,
|
|
|
|
|
assetVersion: assetVersion
|
|
|
|
|
)
|
2026-07-19 08:56:35 +00:00
|
|
|
}
|
2026-06-28 14:18:04 +00:00
|
|
|
}
|
2026-06-28 05:07:19 +00:00
|
|
|
|
2026-07-19 00:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - RouteController
|
|
|
|
|
|
|
|
|
|
extension RootController: RouterController {
|
|
|
|
|
|
|
|
|
|
// MARK: Properties
|
2026-06-28 05:07:19 +00:00
|
|
|
|
|
|
|
|
public var routes: RouteCollection<Context> {
|
|
|
|
|
let routes = RouteCollection(context: Context.self)
|
|
|
|
|
|
|
|
|
|
routes.get(
|
|
|
|
|
.Root.index,
|
|
|
|
|
use: index
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return routes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
|
|
private extension RootController {
|
2026-06-28 14:18:04 +00:00
|
|
|
|
2026-06-28 05:07:19 +00:00
|
|
|
// MARK: Methods
|
|
|
|
|
|
|
|
|
|
/// Handles a request for the landing page.
|
2026-06-29 23:08:36 +00:00
|
|
|
///
|
2026-07-30 06:33:57 +00:00
|
|
|
/// Renders the ``IndexPage`` in the language stored on the context by ``LocalizationMiddleware``, falling back to the default language.
|
2026-06-28 05:07:19 +00:00
|
|
|
/// - Parameters:
|
|
|
|
|
/// - request: the incoming request.
|
|
|
|
|
/// - context: the context the request is resolved against.
|
2026-06-29 23:08:36 +00:00
|
|
|
/// - Returns: the cached ``IndexPage`` response for the context's language.
|
2026-06-28 05:07:19 +00:00
|
|
|
@Sendable
|
|
|
|
|
func index(
|
|
|
|
|
request: Request,
|
2026-06-29 23:08:36 +00:00
|
|
|
context: Context
|
2026-06-28 14:18:04 +00:00
|
|
|
) -> Response {
|
2026-07-23 01:04:37 +00:00
|
|
|
responses.response(
|
|
|
|
|
for: context.language,
|
|
|
|
|
request: request
|
|
|
|
|
)
|
2026-06-28 05:07:19 +00:00
|
|
|
}
|
2026-06-28 14:18:04 +00:00
|
|
|
|
2026-06-28 05:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
|
|
2026-06-28 14:18:04 +00:00
|
|
|
private extension RouterPath {
|
2026-06-28 05:07:19 +00:00
|
|
|
/// A namespace for the ``RootController`` route paths.
|
|
|
|
|
enum Root {
|
|
|
|
|
/// The path of the landing page.
|
|
|
|
|
static let index: RouterPath = "/"
|
|
|
|
|
}
|
|
|
|
|
}
|