2026-06-28 05:07:19 +00:00
|
|
|
import Hummingbird
|
|
|
|
|
|
|
|
|
|
/// Serves the website's root routes.
|
|
|
|
|
///
|
|
|
|
|
/// The controller exposes its routes as a `RouteCollection` so they can be added to a router
|
|
|
|
|
/// (or a sub-group) by the application that composes it:
|
|
|
|
|
///
|
|
|
|
|
/// ```swift
|
|
|
|
|
/// router.addRoutes(RootController<AppRequestContext>().routes)
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// - 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-06-28 14:18:04 +00:00
|
|
|
public struct RootController<Context: RequestContext>: Sendable {
|
|
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
|
|
/// The landing page, rendered once at initialization and reused for every request.
|
|
|
|
|
private let cache: CachedHTMLResponse
|
2026-06-28 05:07:19 +00:00
|
|
|
|
|
|
|
|
// MARK: Initializers
|
|
|
|
|
|
|
|
|
|
/// Creates a root controller.
|
2026-06-28 14:18:04 +00:00
|
|
|
public init() {
|
|
|
|
|
self.cache = .init(IndexPage())
|
|
|
|
|
}
|
2026-06-28 05:07:19 +00:00
|
|
|
|
2026-06-28 14:18:04 +00:00
|
|
|
// MARK: Computed
|
2026-06-28 05:07:19 +00:00
|
|
|
|
|
|
|
|
/// The routes served by the controller.
|
|
|
|
|
///
|
|
|
|
|
/// Serves a `GET` request for the root path (`/`) by rendering the ``IndexPage``.
|
|
|
|
|
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.
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - request: the incoming request.
|
|
|
|
|
/// - context: the context the request is resolved against.
|
2026-06-28 14:18:04 +00:00
|
|
|
/// - Returns: the cached ``IndexPage`` response.
|
2026-06-28 05:07:19 +00:00
|
|
|
@Sendable
|
|
|
|
|
func index(
|
|
|
|
|
request: Request,
|
|
|
|
|
context: some RequestContext
|
2026-06-28 14:18:04 +00:00
|
|
|
) -> Response {
|
|
|
|
|
cache.response()
|
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 = "/"
|
|
|
|
|
}
|
|
|
|
|
}
|