Integrated the DocCMiddleware.Configuration type to the DocCMiddleware type in the library target.

This commit is contained in:
2025-09-23 16:21:25 +02:00
parent d87c828608
commit 398b852ac8
3 changed files with 99 additions and 66 deletions
@@ -12,7 +12,6 @@
import Hummingbird
import class NIOPosix.NIOThreadPool
import struct Logging.Logger
/// A middleware that proxies requests to `DocC` documentation containers within a hosting app.
@@ -20,42 +19,67 @@ public struct DocCMiddleware {
// MARK: Properties
/// A type that contains the parameters to configure the middleware.
let configuration: Configuration
/// A protocol that defines file system interactions.
let fileProvider: any FileProvider
/// A type that interacts with the logging system.
let logger: Logger
/// A use case that checks whether a received URI could be processed or not.
private let checkURI: CheckURIUseCase = .init()
// MARK: Initializers
/// Initializes this middleware with the root path to the `DocC` documentation containers in the file system.
/// - Parameters:
/// - pathToRoot: A path to the root folder in which the `DocC` documentation container are located.
/// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system.
/// - threadPool: A representation of the thread pool that should be used in case some blocking work needs to be performed for which no non-blocking API exists.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
init(
pathToRoot rootFolder: String,
configuration: Configuration,
logger: Logger,
threadPool: NIOThreadPool = .singleton
fileProvider: (any FileProvider)? = nil
) {
self.fileProvider = LocalFileSystem(
rootFolder: rootFolder,
threadPool: threadPool,
logger: logger
)
self.logger = logger
}
/// Initializes this middleware with a type conforming to the `FileProvider` protocol.
/// - Parameters:
/// - fileProvider: A type that conforms to the protocol that defines file system interactions.
/// - logger: A type that interacts with the logging system.
init(
fileProvider: any FileProvider,
logger: Logger
) {
self.fileProvider = fileProvider
self.configuration = configuration
self.fileProvider = if let fileProvider {
fileProvider
} else {
LocalFileSystem(
rootFolder: configuration.folderRoot,
threadPool: configuration.threadPool,
logger: logger
)
}
self.logger = logger
}
}
// 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 {
guard let uri = checkURI(input.uri) else {
return try await next(input, context)
}
return try await next(input, context)
}
}