Files
ccn/Services/Website/Sources/App/App+build.swift
T
javier 7542bc595f Static file serving support for the Website service (#3)
This PR contains the work done to support the static files serving for the **Website** service, and also included the essential boilerplate assets from the **HTML5 boilerplate** project.

To provide further details about the work done:

* Serving: Added the `FileMiddleware` middlewqare to the router; `path`, `server name`, and `log level` now read from config with defaults.
* Library: Added the `StaticFile` enumeration with a `contentType` property, plus typed config-key/value constants and the `Configuration` dependency on WebsiteCore.
* Assets: Added the **HTML5 boilerplate** (HTML, CSS, JS, icons, manifest, robots) to the *Resources/Static* folder.
* Docker: Stage the Resources directory as read-only.
* Tests: Added a test plan, and a shared Xcode scheme.

Reviewed-on: rock-n-code/loud-amsterdam#3
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
2026-06-27 10:59:32 +00:00

90 lines
2.5 KiB
Swift

import Configuration
import Hummingbird
import Logging
import WebsiteCore
/// Builds the website application.
///
/// Reads the log level, server name, and static files location from the configuration,
/// then assembles the router, server configuration, and logger.
/// - Parameter reader: the configuration reader the values are read from.
/// - Returns: the configured application, ready to run as a service.
/// - Throws: an error if the router fails to build.
func application(
reader: ConfigReader
) async throws -> some ApplicationProtocol {
let logLevel = reader.string(
forKey: .Log.level,
as: Logger.Level.self,
default: .info
)
let serverName = reader.string(
forKey: .HTTP.serverName,
default: .Server.name
)
let staticFilesPath = reader.string(
forKey: .Path.staticFiles,
default: .Path.staticResources
)
return Application(
router: router(
staticFilesPath: staticFilesPath,
logLevel: logLevel
),
configuration: ApplicationConfiguration(
reader: reader.scoped(to: "http")
),
logger: logger(
serverName: serverName,
logLevel: logLevel
)
)
}
// MARK: - Helpers
// Request context used by application
private typealias AppRequestContext = BasicRequestContext
/// Builds the application's logger.
/// - Parameters:
/// - serverName: the label applied to the logger.
/// - logLevel: the minimum level the logger emits.
/// - Returns: the configured logger.
private func logger(
serverName: String,
logLevel: Logger.Level
) -> Logger {
var logger = Logger(label: serverName)
logger.logLevel = logLevel
return logger
}
/// Builds the application's router.
///
/// Registers the request-logging middleware and the static file middleware that serves
/// the contents of `staticFilesPath`.
/// - Parameters:
/// - staticFilesPath: the folder, relative to the working directory, the static files are served from.
/// - logLevel: the level the request-logging middleware logs at.
/// - Returns: the configured router.
private func router(
staticFilesPath: String,
logLevel: Logger.Level
) -> Router<AppRequestContext> {
let router = Router(context: AppRequestContext.self)
router.addMiddleware {
LogRequestsMiddleware(logLevel)
FileMiddleware(
staticFilesPath,
searchForIndexHtml: false
)
}
return router
}