Implemented the "handle(_: context: next: )" function for the DocCMiddleware type in the library target.
This commit is contained in:
@@ -11,8 +11,12 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import protocol Hummingbird.FileProvider
|
||||
import protocol Hummingbird.RequestContext
|
||||
import protocol Hummingbird.RouterMiddleware
|
||||
|
||||
import struct Hummingbird.LocalFileSystem
|
||||
import struct Hummingbird.Request
|
||||
import struct Hummingbird.Response
|
||||
import struct Logging.Logger
|
||||
|
||||
/// A middleware that proxies requests to `DocC` documentation containers within a hosting app.
|
||||
@@ -32,6 +36,15 @@ public struct DocCMiddleware<FileSystemProvider: FileProvider> {
|
||||
/// A use case that checks whether a received URI could be processed or not.
|
||||
private let checkURI: CheckURIUseCase = .init()
|
||||
|
||||
/// A use case that extracts data from a given URI path, essential for routing the documentation contents.
|
||||
private let prepareURIPath: PrepareURIPathUseCase
|
||||
|
||||
/// A use case that produces a redirect response based on a given URI path.
|
||||
private let redirectURI: RedirectURIUseCase
|
||||
|
||||
/// A use case that serves a resource, defined by its URI path, from a physical location.
|
||||
private let serveURI: ServeURIUseCase<FileSystemProvider>
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Initializes this middleware.
|
||||
@@ -93,11 +106,74 @@ extension DocCMiddleware: RouterMiddleware {
|
||||
context: any Context,
|
||||
next: (Input, any Context) async throws -> Output
|
||||
) async throws -> Output {
|
||||
guard let uri = checkURI(input.uri) else {
|
||||
guard
|
||||
let uriPath = checkURI(input.uri),
|
||||
let uriData = prepareURIPath(uriPath)
|
||||
else {
|
||||
return try await next(input, context)
|
||||
}
|
||||
|
||||
if uriData.resourcePath == .Path.forwardSlash {
|
||||
// rule #1: Redirects URI root to `/`.
|
||||
// rule #2: Redirects URI resources with `/` to `/documentation`.
|
||||
return redirectURI(
|
||||
uriPath.hasSuffix(.Path.forwardSlash)
|
||||
? String(format: .Format.Path.documentation, uriPath)
|
||||
: String(format: .Format.Path.forwardSlash, uriPath),
|
||||
with: (input, context)
|
||||
)
|
||||
}
|
||||
|
||||
for assetFile in AssetFile.allCases {
|
||||
if uriData.resourcePath.contains(assetFile.path) {
|
||||
return try await serveURI(
|
||||
assetFile == .documentation
|
||||
// Rule #6: Redirects URI resources with `/data/documentation.json` to the file in the `data/documentation/`
|
||||
// folder that has the name of the module and ends with the `.json` extension in the *DocC* archive container.
|
||||
? String(format: .Format.Path.documentationJSON, uriData.archiveName)
|
||||
// Rule #7: Redirect URI resources for static files (`favicon.ico`, `favicon.svg`, and `theme-settings.json`)
|
||||
// to their respective files in the *DocC* archive container.
|
||||
: uriData.resourcePath,
|
||||
at: uriData.archivePath,
|
||||
with: (input, context)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
for assetFolder in AssetFolder.allCases {
|
||||
if uriData.resourcePath.contains(assetFolder.path) {
|
||||
// Rule #8: Redirect URI resources for asset files (`/css/`, `/data/`, `/downloads/`, `/images/`, `/img/`,
|
||||
// `/index/`, `/js/`, or `/videos/`) to their respective files in the *DocC* archive container.
|
||||
return try await serveURI(
|
||||
uriData.resourcePath,
|
||||
at: uriData.archivePath,
|
||||
with: (input, context)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
for documentationFolder in DocumentationFolder.allCases {
|
||||
if uriData.resourcePath.contains(documentationFolder.path) {
|
||||
if uriData.resourcePath.hasSuffix(.Path.forwardSlash) {
|
||||
// Rule #5: Redirect URI resources for `/documentation/` and `/tutorials/` folders to their respective `index.html` file.
|
||||
return try await serveURI(
|
||||
String(format: .Format.Path.index, documentationFolder.path, uriData.archiveName),
|
||||
at: uriData.archivePath,
|
||||
with: (input, context)
|
||||
)
|
||||
} else {
|
||||
// rule #3: Redirects URI resources with `/documentation` to `/documentation/`.
|
||||
// rule #4: Redirects URI resources with `/tutorials` to `/tutorials/`.
|
||||
return redirectURI(
|
||||
String(format: .Format.Path.forwardSlash, uriPath),
|
||||
with: (input, context)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return try await next(input, context)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user