Implemented the "redirect(to: input: context: )" helper function for the DocCMiddleware middleware in the library target.
This commit is contained in:
parent
e81516cc5d
commit
6dad51c027
@ -90,11 +90,17 @@ struct DocCMiddleware<
|
|||||||
|
|
||||||
// rule #5: Redirects URI resources with `/` to `/documentation`.
|
// rule #5: Redirects URI resources with `/` to `/documentation`.
|
||||||
if uriResource == .forwardSlash {
|
if uriResource == .forwardSlash {
|
||||||
return if uriPath.hasSuffix(.forwardSlash) {
|
let pathRedirect = if uriPath.hasSuffix(.forwardSlash) {
|
||||||
.redirect(to: String(format: .Format.Path.documentation, uriPath))
|
String(format: .Format.Path.documentation, uriPath)
|
||||||
} else {
|
} 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 {
|
for staticFile in StaticFile.allCases {
|
||||||
@ -147,7 +153,11 @@ struct DocCMiddleware<
|
|||||||
} else {
|
} else {
|
||||||
// rule #5: Redirects URI resources with `/documentation` to `/documentation/`.
|
// rule #5: Redirects URI resources with `/documentation` to `/documentation/`.
|
||||||
// rule #6: Redirects URI resources with `/tutorials` to `/tutorials/`.
|
// 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
|
// 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.
|
/// Serves a resource file from a provider as a HTTP response.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - path: A relative path to a resource file.
|
/// - path: A relative path to a resource file.
|
||||||
/// - folder: A folder accessible to the provider where to find resource files.
|
/// - folder: A folder accessible to the provider where to find resource files.
|
||||||
|
/// - input: An input request.
|
||||||
/// - context: A request context.
|
/// - context: A request context.
|
||||||
/// - Returns: A HTTP response containing the content of a given resource file inside its body.
|
/// - Returns: A HTTP response containing the content of a given resource file inside its body.
|
||||||
/// - Throws:An error...
|
/// - Throws:An error...
|
||||||
@ -204,7 +242,10 @@ private extension DocCMiddleware {
|
|||||||
throw HTTPError(.notFound)
|
throw HTTPError(.notFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = try await assetProvider.loadFile(id: fileIdentifier, context: context)
|
let body = try await assetProvider.loadFile(
|
||||||
|
id: fileIdentifier,
|
||||||
|
context: context
|
||||||
|
)
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user