Project updates from Template #1
@@ -50,7 +50,8 @@
|
|||||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||||
launchStyle = "0"
|
launchStyle = "0"
|
||||||
useCustomWorkingDirectory = "NO"
|
useCustomWorkingDirectory = "YES"
|
||||||
|
customWorkingDirectory = "/Users/logan/Documents/Development/Platforms/Röck+Cöde/Loud/Services/Website"
|
||||||
ignoresPersistentStateOnLaunch = "NO"
|
ignoresPersistentStateOnLaunch = "NO"
|
||||||
debugDocumentVersioning = "YES"
|
debugDocumentVersioning = "YES"
|
||||||
debugServiceExtension = "internal"
|
debugServiceExtension = "internal"
|
||||||
|
|||||||
@@ -41,10 +41,6 @@ pkg-clean: ## Remove the Swift build artifacts
|
|||||||
pkg-reset: ## Resets the complete SPM cache/build folder
|
pkg-reset: ## Resets the complete SPM cache/build folder
|
||||||
@swift package reset
|
@swift package reset
|
||||||
|
|
||||||
.PHONY: pkg-deps
|
|
||||||
pkg-deps: ## Lists the SPM package dependencies
|
|
||||||
@swift package show-dependencies
|
|
||||||
|
|
||||||
.PHONY: pkg-outdated
|
.PHONY: pkg-outdated
|
||||||
pkg-outdated: ## Lists the SPM package dependencies that can be updated
|
pkg-outdated: ## Lists the SPM package dependencies that can be updated
|
||||||
@swift package update --dry-run
|
@swift package update --dry-run
|
||||||
|
|||||||
@@ -55,6 +55,10 @@ let package = Package(
|
|||||||
name: "Configuration",
|
name: "Configuration",
|
||||||
package: "swift-configuration"
|
package: "swift-configuration"
|
||||||
),
|
),
|
||||||
|
.product(
|
||||||
|
name: "Hummingbird",
|
||||||
|
package: "hummingbird"
|
||||||
|
),
|
||||||
],
|
],
|
||||||
path: "Sources/Library"
|
path: "Sources/Library"
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -65,8 +65,9 @@ private func logger(
|
|||||||
|
|
||||||
/// Builds the application's router.
|
/// Builds the application's router.
|
||||||
///
|
///
|
||||||
/// Registers the request-logging middleware and the static file middleware that serves
|
/// Registers, in order, the request-logging middleware, the not-found middleware that serves
|
||||||
/// the contents of `staticFilesPath`.
|
/// the error page, and the static file middleware that serves the contents of `staticFilesPath`
|
||||||
|
/// (falling back to `index.html` for directory requests).
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - staticFilesPath: the folder, relative to the working directory, the static files are served from.
|
/// - staticFilesPath: the folder, relative to the working directory, the static files are served from.
|
||||||
/// - logLevel: the level the request-logging middleware logs at.
|
/// - logLevel: the level the request-logging middleware logs at.
|
||||||
@@ -79,9 +80,10 @@ private func router(
|
|||||||
|
|
||||||
router.addMiddleware {
|
router.addMiddleware {
|
||||||
LogRequestsMiddleware(logLevel)
|
LogRequestsMiddleware(logLevel)
|
||||||
|
NotFoundMiddleware(staticFilesPath)
|
||||||
FileMiddleware(
|
FileMiddleware(
|
||||||
staticFilesPath,
|
staticFilesPath,
|
||||||
searchForIndexHtml: false
|
searchForIndexHtml: true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import Foundation
|
||||||
|
import Hummingbird
|
||||||
|
import NIOCore
|
||||||
|
|
||||||
|
/// Serves a custom error page for requests that match neither a route nor a static file.
|
||||||
|
///
|
||||||
|
/// Placed ahead of `FileMiddleware` in the middleware chain, it catches the `.notFound` error
|
||||||
|
/// that bubbles up when no file exists for the requested path and responds with the preloaded
|
||||||
|
/// error page and a `404 Not Found` status.
|
||||||
|
public struct NotFoundMiddleware<Context: RequestContext> {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// The body of the error page served on a not-found response.
|
||||||
|
private let page: ByteBuffer
|
||||||
|
|
||||||
|
// MARK: Initializers
|
||||||
|
|
||||||
|
/// Creates a middleware that serves the error page (`404.html`) from the static files folder.
|
||||||
|
///
|
||||||
|
/// The page is read once, at construction. A minimal fallback body is used when the file is
|
||||||
|
/// missing.
|
||||||
|
/// - Parameter staticFilesPath: the folder, relative to the working directory, the static files are served from.
|
||||||
|
public init(
|
||||||
|
_ staticFilesPath: String
|
||||||
|
) {
|
||||||
|
let path = StaticFile.errorHTML.path(relativeTo: staticFilesPath)
|
||||||
|
|
||||||
|
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
|
||||||
|
self.init(page: .init(bytes: data))
|
||||||
|
} else {
|
||||||
|
self.init(page: .init(string: "404 Not Found"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a middleware that serves the given error page on a not-found response.
|
||||||
|
/// - Parameter page: the body of the error page.
|
||||||
|
init(
|
||||||
|
page: ByteBuffer
|
||||||
|
) {
|
||||||
|
self.page = page
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - RouterMiddleware
|
||||||
|
|
||||||
|
extension NotFoundMiddleware: RouterMiddleware {
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
public func handle(
|
||||||
|
_ request: Request,
|
||||||
|
context: Context,
|
||||||
|
next: (Request, Context) async throws -> Response
|
||||||
|
) async throws -> Response {
|
||||||
|
do {
|
||||||
|
return try await next(request, context)
|
||||||
|
} catch let error {
|
||||||
|
// Only intercept "not found"; let every other error propagate.
|
||||||
|
guard
|
||||||
|
let responseError = error as? any HTTPResponseError,
|
||||||
|
responseError.status == .notFound
|
||||||
|
else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
var headers = HTTPFields()
|
||||||
|
|
||||||
|
headers[.contentType] = StaticFile.errorHTML.contentType
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
status: .notFound,
|
||||||
|
headers: headers,
|
||||||
|
body: .init(byteBuffer: page)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import Foundation
|
|||||||
import Hummingbird
|
import Hummingbird
|
||||||
import HummingbirdTesting
|
import HummingbirdTesting
|
||||||
import Logging
|
import Logging
|
||||||
|
import NIOCore
|
||||||
import Testing
|
import Testing
|
||||||
|
|
||||||
@testable import Website
|
@testable import Website
|
||||||
@@ -21,9 +22,30 @@ struct AppTests {
|
|||||||
.deletingLastPathComponent() // package root
|
.deletingLastPathComponent() // package root
|
||||||
.appendingPathComponent(.Path.staticResources)
|
.appendingPathComponent(.Path.staticResources)
|
||||||
.path
|
.path
|
||||||
|
|
||||||
// MARK: Functional tests
|
// MARK: Functional tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func `landing page to be served at root`() async throws {
|
||||||
|
let file: StaticFile = .indexHTML
|
||||||
|
let app = try await application(
|
||||||
|
reader: reader(
|
||||||
|
staticFilesPath: staticFilesPath
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
try await app.test(.router) { client in
|
||||||
|
try await client.execute(
|
||||||
|
uri: "/",
|
||||||
|
method: .get
|
||||||
|
) { response in
|
||||||
|
#expect(response.status == .ok)
|
||||||
|
#expect(response.headers[.contentType] == file.contentType)
|
||||||
|
#expect(response.body == data(of: file))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(arguments: StaticFile.allCases)
|
@Test(arguments: StaticFile.allCases)
|
||||||
func `static files to be served`(
|
func `static files to be served`(
|
||||||
staticFile file: StaticFile
|
staticFile file: StaticFile
|
||||||
@@ -45,14 +67,47 @@ struct AppTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func `error page to be served when not found`() async throws {
|
||||||
|
let file: StaticFile = .errorHTML
|
||||||
|
let app = try await application(
|
||||||
|
reader: reader(
|
||||||
|
staticFilesPath: staticFilesPath
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
try await app.test(.router) { client in
|
||||||
|
try await client.execute(
|
||||||
|
uri: "/this-path-does-not-exist",
|
||||||
|
method: .get
|
||||||
|
) { response in
|
||||||
|
#expect(response.status == .notFound)
|
||||||
|
#expect(response.headers[.contentType] == file.contentType)
|
||||||
|
#expect(response.body == data(of: file))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
|
||||||
private extension AppTests {
|
private extension AppTests {
|
||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
|
func data(
|
||||||
|
of file: StaticFile
|
||||||
|
) -> ByteBuffer {
|
||||||
|
let url = URL(fileURLWithPath: file.path(relativeTo: staticFilesPath))
|
||||||
|
|
||||||
|
guard let data = try? Data(contentsOf: url) else {
|
||||||
|
return ByteBuffer()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ByteBuffer(bytes: data)
|
||||||
|
}
|
||||||
|
|
||||||
func reader(
|
func reader(
|
||||||
staticFilesPath: String
|
staticFilesPath: String
|
||||||
) -> ConfigReader {
|
) -> ConfigReader {
|
||||||
@@ -65,5 +120,5 @@ private extension AppTests {
|
|||||||
])
|
])
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user