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,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),
|
||||
])
|
||||
])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user