2025-09-18 23:08:52 +02:00
|
|
|
// ===----------------------------------------------------------------------===
|
|
|
|
|
//
|
|
|
|
|
// This source file is part of the Hummingbird DocC Middleware open source project
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2025 Röck+Cöde VoF. and the Hummingbird DocC Middleware project authors
|
|
|
|
|
// Licensed under the EUPL 1.2 or later.
|
|
|
|
|
//
|
|
|
|
|
// See LICENSE for license information
|
|
|
|
|
// See CONTRIBUTORS for the list of Hummingbird DocC Middleware project authors
|
|
|
|
|
//
|
|
|
|
|
// ===----------------------------------------------------------------------===
|
|
|
|
|
|
2025-09-25 02:41:52 +02:00
|
|
|
import protocol Hummingbird.FileProvider
|
2025-09-26 00:27:56 +02:00
|
|
|
import protocol Hummingbird.RequestContext
|
|
|
|
|
import protocol Hummingbird.RouterMiddleware
|
2025-09-18 23:08:52 +02:00
|
|
|
|
2025-09-25 02:41:52 +02:00
|
|
|
import struct Hummingbird.LocalFileSystem
|
2025-09-26 00:27:56 +02:00
|
|
|
import struct Hummingbird.Request
|
|
|
|
|
import struct Hummingbird.Response
|
2025-09-18 23:08:52 +02:00
|
|
|
import struct Logging.Logger
|
|
|
|
|
|
|
|
|
|
/// A middleware that proxies requests to `DocC` documentation containers within a hosting app.
|
2025-09-25 02:41:52 +02:00
|
|
|
public struct DocCMiddleware<FileSystemProvider: FileProvider> {
|
2025-09-18 23:08:52 +02:00
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
2025-09-23 16:21:25 +02:00
|
|
|
/// A type that contains the parameters to configure the middleware.
|
|
|
|
|
let configuration: Configuration
|
|
|
|
|
|
2025-09-25 02:41:52 +02:00
|
|
|
/// A type that conforms to a protocol that defines file system interactions.
|
|
|
|
|
let fileProvider: FileSystemProvider
|
2025-09-18 23:08:52 +02:00
|
|
|
|
|
|
|
|
/// A type that interacts with the logging system.
|
|
|
|
|
let logger: Logger
|
|
|
|
|
|
2025-09-23 16:21:25 +02:00
|
|
|
/// A use case that checks whether a received URI could be processed or not.
|
|
|
|
|
private let checkURI: CheckURIUseCase = .init()
|
|
|
|
|
|
2025-09-26 00:27:56 +02:00
|
|
|
/// 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>
|
|
|
|
|
|
2025-09-18 23:08:52 +02:00
|
|
|
// MARK: Initializers
|
|
|
|
|
|
2025-09-25 02:41:52 +02:00
|
|
|
/// Initializes this middleware.
|
2025-09-18 23:08:52 +02:00
|
|
|
/// - Parameters:
|
2025-09-23 16:21:25 +02:00
|
|
|
/// - configuration: A type that contains the parameters to configure the middleware.
|
2025-09-18 23:08:52 +02:00
|
|
|
/// - logger: A type that interacts with the logging system.
|
2025-09-25 02:41:52 +02:00
|
|
|
public init(
|
2025-09-23 16:21:25 +02:00
|
|
|
configuration: Configuration,
|
2025-09-25 02:41:52 +02:00
|
|
|
logger: Logger
|
|
|
|
|
) where FileSystemProvider == LocalFileSystem {
|
|
|
|
|
self.init(
|
|
|
|
|
configuration: configuration,
|
|
|
|
|
fileProvider: LocalFileSystem(
|
2025-09-23 16:21:25 +02:00
|
|
|
rootFolder: configuration.folderRoot,
|
|
|
|
|
threadPool: configuration.threadPool,
|
|
|
|
|
logger: logger
|
2025-09-25 02:41:52 +02:00
|
|
|
),
|
|
|
|
|
logger: logger,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Initializes this middleware with a concrete file provider type.
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - configuration: A type that contains the parameters to configure the middleware.
|
|
|
|
|
/// - fileProvider: A type that conforms to the protocol that defines file system interactions.
|
|
|
|
|
/// - logger: A type that interacts with the logging system.
|
|
|
|
|
init(
|
|
|
|
|
configuration: Configuration,
|
|
|
|
|
fileProvider: FileSystemProvider,
|
|
|
|
|
logger: Logger,
|
|
|
|
|
) {
|
2025-09-18 23:08:52 +02:00
|
|
|
self.logger = logger
|
2025-09-25 02:41:52 +02:00
|
|
|
self.configuration = configuration
|
|
|
|
|
self.fileProvider = fileProvider
|
|
|
|
|
self.prepareURIPath = .init(uriRoot: configuration.uriRoot)
|
|
|
|
|
self.redirectURI = .init(logger: logger)
|
|
|
|
|
self.serveURI = .init(
|
|
|
|
|
fileProvider: fileProvider,
|
|
|
|
|
logger: logger
|
|
|
|
|
)
|
2025-09-18 23:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 16:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - RouterMiddleware
|
|
|
|
|
|
|
|
|
|
extension DocCMiddleware: RouterMiddleware {
|
|
|
|
|
|
|
|
|
|
// MARK: Type aliases
|
|
|
|
|
|
|
|
|
|
public typealias Context = RequestContext
|
|
|
|
|
public typealias Input = Request
|
|
|
|
|
public typealias Output = Response
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
|
|
public func handle(
|
|
|
|
|
_ input: Input,
|
|
|
|
|
context: any Context,
|
|
|
|
|
next: (Input, any Context) async throws -> Output
|
|
|
|
|
) async throws -> Output {
|
2025-09-26 00:27:56 +02:00
|
|
|
guard
|
|
|
|
|
let uriPath = checkURI(input.uri),
|
|
|
|
|
let uriData = prepareURIPath(uriPath)
|
|
|
|
|
else {
|
2025-09-23 16:21:25 +02:00
|
|
|
return try await next(input, context)
|
|
|
|
|
}
|
2025-09-26 00:27:56 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-23 16:21:25 +02:00
|
|
|
|
|
|
|
|
return try await next(input, context)
|
2025-09-18 23:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-26 00:27:56 +02:00
|
|
|
|