From 6dad51c027a022e1e05110662faf9248ac8fa7bd Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 23 Apr 2025 00:16:31 +0200 Subject: [PATCH] Implemented the "redirect(to: input: context: )" helper function for the DocCMiddleware middleware in the library target. --- .../Internal/Middlewares/DocCMiddleware.swift | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/Library/Sources/Internal/Middlewares/DocCMiddleware.swift b/Library/Sources/Internal/Middlewares/DocCMiddleware.swift index 5db9b7b..512a50b 100644 --- a/Library/Sources/Internal/Middlewares/DocCMiddleware.swift +++ b/Library/Sources/Internal/Middlewares/DocCMiddleware.swift @@ -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(