Improved the initializers for the DocCMiddleware type in the library target.

This commit is contained in:
2025-09-25 02:41:52 +02:00
parent 36f14cfb51
commit edeaf219a0
2 changed files with 69 additions and 29 deletions
@@ -10,20 +10,21 @@
// //
// ===----------------------------------------------------------------------=== // ===----------------------------------------------------------------------===
import Hummingbird import protocol Hummingbird.FileProvider
import struct Hummingbird.LocalFileSystem
import struct Logging.Logger import struct Logging.Logger
/// A middleware that proxies requests to `DocC` documentation containers within a hosting app. /// A middleware that proxies requests to `DocC` documentation containers within a hosting app.
public struct DocCMiddleware { public struct DocCMiddleware<FileSystemProvider: FileProvider> {
// MARK: Properties // MARK: Properties
/// A type that contains the parameters to configure the middleware. /// A type that contains the parameters to configure the middleware.
let configuration: Configuration let configuration: Configuration
/// A protocol that defines file system interactions. /// A type that conforms to a protocol that defines file system interactions.
let fileProvider: any FileProvider let fileProvider: FileSystemProvider
/// A type that interacts with the logging system. /// A type that interacts with the logging system.
let logger: Logger let logger: Logger
@@ -33,27 +34,44 @@ public struct DocCMiddleware {
// MARK: Initializers // MARK: Initializers
/// Initializes this middleware with the root path to the `DocC` documentation containers in the file system. /// Initializes this middleware.
/// - Parameters: /// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware. /// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system. /// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any. public init(
init(
configuration: Configuration, configuration: Configuration,
logger: Logger, logger: Logger
fileProvider: (any FileProvider)? = nil ) where FileSystemProvider == LocalFileSystem {
) { self.init(
self.configuration = configuration configuration: configuration,
self.fileProvider = if let fileProvider { fileProvider: LocalFileSystem(
fileProvider
} else {
LocalFileSystem(
rootFolder: configuration.folderRoot, rootFolder: configuration.folderRoot,
threadPool: configuration.threadPool, threadPool: configuration.threadPool,
logger: logger logger: logger
) ),
} 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,
) {
self.logger = logger self.logger = logger
self.configuration = configuration
self.fileProvider = fileProvider
self.prepareURIPath = .init(uriRoot: configuration.uriRoot)
self.redirectURI = .init(logger: logger)
self.serveURI = .init(
fileProvider: fileProvider,
logger: logger
)
} }
} }
@@ -72,22 +72,48 @@ private extension DocCMiddlewareTests {
// MARK: Functions // MARK: Functions
/// Asserts the initialization of a `DocCMiddleware` type. /// Asserts the public initializer.
/// - Parameters: /// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware. /// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system. /// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
func assertInit( func assertInit(
configuration: DocCMiddleware.Configuration, configuration: DocCMiddleware<LocalFileSystem>.Configuration,
logger: Logger = .test(), logger: Logger = .test()
fileProvider: (any FileProvider)? = nil
) { ) {
// GIVEN // GIVEN
// WHEN // WHEN
let middleware = DocCMiddleware( let middleware = DocCMiddleware(
configuration: configuration, configuration: configuration,
logger: logger, logger: logger
fileProvider: fileProvider )
// THEN
#expect(middleware.configuration.folderRoot == configuration.folderRoot)
#expect(middleware.configuration.uriRoot == configuration.uriRoot)
#expect(middleware.configuration.threadPool === configuration.threadPool)
#expect(middleware.logger.label == logger.label)
#expect(middleware.logger.logLevel == logger.logLevel)
#expect(type(of:middleware.fileProvider) == LocalFileSystem.self)
}
/// Asserts the internal initializer with a concrete file provider type.
/// - Parameters:
/// - configuration: A type that contains the parameters to configure the middleware.
/// - logger: A type that interacts with the logging system.
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
func assertInit<FileSystemProvider: FileProvider>(
configuration: DocCMiddleware<FileSystemProvider>.Configuration,
logger: Logger = .test(),
fileProvider: FileSystemProvider
) {
// GIVEN
// WHEN
let middleware = DocCMiddleware(
configuration: configuration,
fileProvider: fileProvider,
logger: logger
) )
// THEN // THEN
@@ -98,11 +124,7 @@ private extension DocCMiddlewareTests {
#expect(middleware.logger.label == logger.label) #expect(middleware.logger.label == logger.label)
#expect(middleware.logger.logLevel == logger.logLevel) #expect(middleware.logger.logLevel == logger.logLevel)
if let fileProvider { #expect(type(of:middleware.fileProvider) == FileSystemProvider.self)
#expect(type(of:middleware.fileProvider) == type(of: fileProvider))
} else {
#expect(middleware.fileProvider is LocalFileSystem)
}
} }
} }