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().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. public struct RootController: Sendable { // MARK: Properties /// The landing page, rendered once per supported language and reused for every request. private let responses: LocalizedHTMLCollectionResponse // MARK: Initializers /// Creates a root controller. public init() { self.responses = .init { IndexPage(locale: $0) } } // MARK: Computed /// The routes served by the controller. /// /// Serves a `GET` request for the root path (`/`) by rendering the ``IndexPage`` in the /// language negotiated for the request. public var routes: RouteCollection { let routes = RouteCollection(context: Context.self) routes.get( .Root.index, use: index ) return routes } } // MARK: - Helpers private extension RootController { // MARK: Methods /// Handles a request for the landing page. /// /// Renders the ``IndexPage`` in the language stored on the context by ``LocalizationMiddleware``, /// falling back to the default language. /// - Parameters: /// - request: the incoming request. /// - context: the context the request is resolved against. /// - Returns: the cached ``IndexPage`` response for the context's language. @Sendable func index( request: Request, context: Context ) -> Response { responses.response(for: context.language) } } // MARK: - Constants private extension RouterPath { /// A namespace for the ``RootController`` route paths. enum Root { /// The path of the landing page. static let index: RouterPath = "/" } }