diff --git a/Sources/DocCMiddleware/Public/Middlewares/DocCMiddleware.swift b/Sources/DocCMiddleware/Public/Middlewares/DocCMiddleware.swift index 1abc144..f40d1d7 100644 --- a/Sources/DocCMiddleware/Public/Middlewares/DocCMiddleware.swift +++ b/Sources/DocCMiddleware/Public/Middlewares/DocCMiddleware.swift @@ -10,20 +10,21 @@ // // ===----------------------------------------------------------------------=== -import Hummingbird +import protocol Hummingbird.FileProvider +import struct Hummingbird.LocalFileSystem import struct Logging.Logger /// A middleware that proxies requests to `DocC` documentation containers within a hosting app. -public struct DocCMiddleware { +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 conforms to a protocol that defines file system interactions. + let fileProvider: FileSystemProvider /// A type that interacts with the logging system. let logger: Logger @@ -33,27 +34,44 @@ public struct DocCMiddleware { // MARK: Initializers - /// Initializes this middleware with the root path to the `DocC` documentation containers in the file system. + /// Initializes this middleware. /// - 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. - init( + public init( configuration: Configuration, - logger: Logger, - fileProvider: (any FileProvider)? = nil - ) { - self.configuration = configuration - self.fileProvider = if let fileProvider { - fileProvider - } else { - LocalFileSystem( + logger: Logger + ) where FileSystemProvider == LocalFileSystem { + self.init( + configuration: configuration, + fileProvider: LocalFileSystem( rootFolder: configuration.folderRoot, threadPool: configuration.threadPool, 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.configuration = configuration + self.fileProvider = fileProvider + self.prepareURIPath = .init(uriRoot: configuration.uriRoot) + self.redirectURI = .init(logger: logger) + self.serveURI = .init( + fileProvider: fileProvider, + logger: logger + ) } } diff --git a/Tests/DocCMiddleware/Tests/Public/Middlewares/DocCMiddlewareTests.swift b/Tests/DocCMiddleware/Tests/Public/Middlewares/DocCMiddlewareTests.swift index 6ee8295..cd5be76 100644 --- a/Tests/DocCMiddleware/Tests/Public/Middlewares/DocCMiddlewareTests.swift +++ b/Tests/DocCMiddleware/Tests/Public/Middlewares/DocCMiddlewareTests.swift @@ -72,22 +72,48 @@ private extension DocCMiddlewareTests { // MARK: Functions - /// Asserts the initialization of a `DocCMiddleware` type. + /// Asserts the public initializer. /// - 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( - configuration: DocCMiddleware.Configuration, - logger: Logger = .test(), - fileProvider: (any FileProvider)? = nil + configuration: DocCMiddleware.Configuration, + logger: Logger = .test() ) { // GIVEN // WHEN let middleware = DocCMiddleware( configuration: configuration, - logger: logger, - fileProvider: fileProvider + logger: logger + ) + + // 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( + configuration: DocCMiddleware.Configuration, + logger: Logger = .test(), + fileProvider: FileSystemProvider + ) { + // GIVEN + // WHEN + let middleware = DocCMiddleware( + configuration: configuration, + fileProvider: fileProvider, + logger: logger ) // THEN @@ -98,11 +124,7 @@ private extension DocCMiddlewareTests { #expect(middleware.logger.label == logger.label) #expect(middleware.logger.logLevel == logger.logLevel) - if let fileProvider { - #expect(type(of:middleware.fileProvider) == type(of: fileProvider)) - } else { - #expect(middleware.fileProvider is LocalFileSystem) - } + #expect(type(of:middleware.fileProvider) == FileSystemProvider.self) } }