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>
This commit is contained in:
@@ -1,24 +1,37 @@
|
||||
import Configuration
|
||||
import Hummingbird
|
||||
import Logging
|
||||
import WebsiteCore
|
||||
|
||||
/// Build application
|
||||
/// - Parameter reader: configuration reader
|
||||
/// 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",
|
||||
forKey: .Log.level,
|
||||
as: Logger.Level.self,
|
||||
default: .info
|
||||
)
|
||||
let serverName = reader.string(
|
||||
forKey: "http.serverName",
|
||||
default: "LoudWebsite"
|
||||
forKey: .HTTP.serverName,
|
||||
default: .Server.name
|
||||
)
|
||||
let staticFilesPath = reader.string(
|
||||
forKey: .Path.staticFiles,
|
||||
default: .Path.staticResources
|
||||
)
|
||||
|
||||
return Application(
|
||||
router: try router(),
|
||||
router: router(
|
||||
staticFilesPath: staticFilesPath,
|
||||
logLevel: logLevel
|
||||
),
|
||||
configuration: ApplicationConfiguration(
|
||||
reader: reader.scoped(to: "http")
|
||||
),
|
||||
@@ -34,7 +47,11 @@ func application(
|
||||
// Request context used by application
|
||||
private typealias AppRequestContext = BasicRequestContext
|
||||
|
||||
/// Build logger
|
||||
/// 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
|
||||
@@ -46,16 +63,26 @@ private func logger(
|
||||
return logger
|
||||
}
|
||||
|
||||
/// Build router
|
||||
private func router() throws -> Router<AppRequestContext> {
|
||||
/// 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(.info)
|
||||
}
|
||||
|
||||
router.get("/") { _, _ in
|
||||
return "Hello!"
|
||||
LogRequestsMiddleware(logLevel)
|
||||
FileMiddleware(
|
||||
staticFilesPath,
|
||||
searchForIndexHtml: false
|
||||
)
|
||||
}
|
||||
|
||||
return router
|
||||
|
||||
Reference in New Issue
Block a user