Implemented the "redirect(to: input: context: )" helper function for the DocCMiddleware middleware in the library target.

This commit is contained in:
Javier Cicchelli 2025-04-23 00:16:31 +02:00
parent e81516cc5d
commit 6dad51c027

View File

@ -90,11 +90,17 @@ struct DocCMiddleware<
// rule #5: Redirects URI resources with `/` to `/documentation`.
if uriResource == .forwardSlash {
return if uriPath.hasSuffix(.forwardSlash) {
.redirect(to: String(format: .Format.Path.documentation, uriPath))
let pathRedirect = if uriPath.hasSuffix(.forwardSlash) {
String(format: .Format.Path.documentation, uriPath)
} else {
.redirect(to: String(format: .Format.Path.forwardSlash, uriPath))
String(format: .Format.Path.forwardSlash, uriPath)
}
return redirect(
to: pathRedirect,
input: input,
context: context
)
}
for staticFile in StaticFile.allCases {
@ -147,7 +153,11 @@ struct DocCMiddleware<
} else {
// rule #5: Redirects URI resources with `/documentation` to `/documentation/`.
// rule #6: Redirects URI resources with `/tutorials` to `/tutorials/`.
return .redirect(to: String(format: .Format.Path.forwardSlash, uriPath))
return redirect(
to: String(format: .Format.Path.forwardSlash, uriPath),
input: input,
context: context
)
}
}
}
@ -175,10 +185,38 @@ private extension DocCMiddleware {
// MARK: Functions
/// Redirects a request to a new relative path.
/// - Parameters:
/// - path: A relative path to use in the redirection.
/// - input: An input request.
/// - context: A request context.
/// - Returns: A HTTP response containing the redirection to another
func redirect(
to path: String,
input: Request,
context: Context
) -> Response {
defer {
logger.debug(
"The path URI has been redirected to: \(path)",
metadata: .metadata(
context: context,
request: input,
statusCode: .permanentRedirect,
redirect: path
),
source: .source
)
}
return .redirect(to: path)
}
/// Serves a resource file from a provider as a HTTP response.
/// - Parameters:
/// - path: A relative path to a resource file.
/// - folder: A folder accessible to the provider where to find resource files.
/// - input: An input request.
/// - context: A request context.
/// - Returns: A HTTP response containing the content of a given resource file inside its body.
/// - Throws:An error...
@ -204,7 +242,10 @@ private extension DocCMiddleware {
throw HTTPError(.notFound)
}
let body = try await assetProvider.loadFile(id: fileIdentifier, context: context)
let body = try await assetProvider.loadFile(
id: fileIdentifier,
context: context
)
defer {
logger.debug(