This PR contains the work done to replace the use of static _HTML_ files with type-safe HTML rendered server-side via **Elementary** through **Hummingbird**. To provide further details about the work done: * Added the **Elementary** dependencies. * Added the `IndexPage` and `ErrorPage` pages, ported from the old HTML boilerplate; removed the static files. * Added the `RootController` controller serving GET / using the `IndexPage` page, wired into the router. * Reworked the `NotFoundMiddleware` middleare to render `ErrorPage` page directly; non-notFound errors still propagate. * the `FileMiddleware` middleware no longer searches for any static `index.html` file. * Simplified the `StaticFile` enumeration, dropped unused constants and now-unnecessary throws. Reviewed-on: rock-n-code/loud-amsterdam#5 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
72 lines
1.7 KiB
Swift
72 lines
1.7 KiB
Swift
import Hummingbird
|
|
import HummingbirdElementary
|
|
|
|
/// 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.
|
|
public struct RootController<Context: RequestContext> : Sendable{
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Creates a root controller.
|
|
public init() {}
|
|
|
|
// MARK: Properties
|
|
|
|
/// 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 {
|
|
|
|
// MARK: Methods
|
|
|
|
/// Handles a request for the landing page.
|
|
/// - Parameters:
|
|
/// - request: the incoming request.
|
|
/// - context: the context the request is resolved against.
|
|
/// - Returns: an HTML response that renders the ``IndexPage``.
|
|
@Sendable
|
|
func index(
|
|
request: Request,
|
|
context: some RequestContext
|
|
) -> HTMLResponse {
|
|
.init {
|
|
IndexPage()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Constants
|
|
|
|
extension RouterPath {
|
|
/// A namespace for the ``RootController`` route paths.
|
|
enum Root {
|
|
/// The path of the landing page.
|
|
static let index: RouterPath = "/"
|
|
}
|
|
}
|