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:
2026-06-27 10:59:32 +00:00
committed by javier
parent 08b210e7c6
commit 7542bc595f
25 changed files with 973 additions and 36 deletions
+49 -14
View File
@@ -1,34 +1,69 @@
import Configuration
import Foundation
import Hummingbird
import HummingbirdTesting
import Logging
import Testing
@testable import Website
@testable import WebsiteCore
private let reader = ConfigReader(providers: [
InMemoryProvider(values: [
"http.host": "127.0.0.1",
"http.port": "0",
"log.level": "trace",
])
])
@Suite
@Suite("App executable")
struct AppTests {
@Test
func hello() async throws {
// MARK: Constants
// Absolute path to the package's "Resources/Static" folder, derived from this
// file's location so the static files resolve regardless of the working directory.
private let staticFilesPath = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent() // Tests/App
.deletingLastPathComponent() // Tests
.deletingLastPathComponent() // package root
.appendingPathComponent(.Path.staticResources)
.path
// MARK: Functional tests
@Test(arguments: StaticFile.allCases)
func `static files to be served`(
staticFile file: StaticFile
) async throws {
let app = try await application(
reader: reader
reader: reader(
staticFilesPath: staticFilesPath
)
)
try await app.test(.router) { client in
try await client.execute(
uri: "/",
uri: "/\(file.relativePath)",
method: .get
) { response in
#expect(response.body == ByteBuffer(string: "Hello!"))
#expect(response.status == .ok)
#expect(response.headers[.contentType] == file.contentType)
}
}
}
}
// MARK: - Helpers
private extension AppTests {
// MARK: Methods
func reader(
staticFilesPath: String
) -> ConfigReader {
ConfigReader(providers: [
InMemoryProvider(values: [
.HTTP.host: .HTTP.host,
.HTTP.port: .HTTP.port,
.Log.level: .Log.level,
.Path.staticFiles: .init(stringLiteral: staticFilesPath),
])
])
}
}
@@ -0,0 +1,141 @@
import Foundation
import Testing
@testable import WebsiteCore
@Suite("StaticFile enumeration")
struct StaticFileTests {
// MARK: Type aliases
typealias File = StaticFile
typealias FileExtension = StaticFile.Extension
// MARK: Computed tests
@Test(arguments: zip(
File.allCases,
Self.contentTypes
))
func `content type`(
for file: File,
expects contentType: String
) {
#expect(file.contentType == contentType)
}
@Test(arguments: zip(
File.allCases,
Self.fileExtensions
))
func `file extension`(
for file: File,
expects `extension`: FileExtension
) {
#expect(file.fileExtension == `extension`)
}
@Test(arguments: zip(
File.allCases,
Self.fileNames
))
func `file name`(
for file: File,
expects fileName: String
) {
#expect(file.fileName == fileName)
}
@Test(arguments: zip(
File.allCases,
Self.relativePaths
))
func `relative path`(
for file: File,
expects relativePath: String
) {
#expect(file.relativePath == relativePath)
}
// MARK: Method tests
@Test(arguments: [
"",
".",
"Resources/Static"
])
func `path relative to`(
_ basePath: String
) {
for file in File.allCases {
let pathRelativeToBasePath = file.path(relativeTo: basePath)
if basePath.isEmpty {
#expect(pathRelativeToBasePath == file.relativePath)
} else {
#expect(pathRelativeToBasePath == "\(basePath)/\(file.relativePath)")
}
}
}
// MARK: CaseIterable tests
@Test
func `all cases`() {
#expect(File.allCases.count == 9)
}
}
// MARK: - Helpers
private extension StaticFileTests {
// MARK: Constants
static let contentTypes: [String] = [
"text/javascript",
"text/html",
"image/vnd.microsoft.icon",
"image/png",
"image/svg+xml",
"text/html",
"text/plain",
"application/manifest+json",
"text/css"
]
static let fileExtensions: [FileExtension] = [
.js,
.html,
.ico,
.png,
.svg,
.html,
.txt,
.webmanifest,
.css
]
static let fileNames: [String] = [
"app",
"404",
"favicon",
"icon",
"icon",
"index",
"robots",
"site",
"style"
]
static let relativePaths: [String] = [
"js/app.js",
"404.html",
"favicon.ico",
"icon.png",
"icon.svg",
"index.html",
"robots.txt",
"site.webmanifest",
"css/style.css"
]
}
@@ -1 +0,0 @@