Implemented the DocC archives support for the middleware #2

Merged
javier merged 29 commits from middleware/xcode-documentation into main 2025-09-26 23:54:08 +00:00
3 changed files with 99 additions and 66 deletions
Showing only changes of commit 398b852ac8 - Show all commits
@@ -12,7 +12,6 @@
import Hummingbird import Hummingbird
import class NIOPosix.NIOThreadPool
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.
@@ -20,42 +19,67 @@ public struct DocCMiddleware {
// MARK: Properties // MARK: Properties
/// A type that contains the parameters to configure the middleware.
let configuration: Configuration
/// A protocol that defines file system interactions. /// A protocol that defines file system interactions.
let fileProvider: any FileProvider let fileProvider: any FileProvider
/// A type that interacts with the logging system. /// A type that interacts with the logging system.
let logger: Logger let logger: Logger
/// A use case that checks whether a received URI could be processed or not.
private let checkURI: CheckURIUseCase = .init()
// MARK: Initializers // MARK: Initializers
/// Initializes this middleware with the root path to the `DocC` documentation containers in the file system. /// Initializes this middleware with the root path to the `DocC` documentation containers in the file system.
/// - Parameters: /// - 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. /// - 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( init(
pathToRoot rootFolder: String, configuration: Configuration,
logger: Logger, logger: Logger,
threadPool: NIOThreadPool = .singleton fileProvider: (any FileProvider)? = nil
) { ) {
self.fileProvider = LocalFileSystem( self.configuration = configuration
rootFolder: rootFolder, self.fileProvider = if let fileProvider {
threadPool: threadPool, fileProvider
logger: logger } else {
) LocalFileSystem(
self.logger = logger rootFolder: configuration.folderRoot,
} threadPool: configuration.threadPool,
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.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)
}
}
@@ -14,35 +14,53 @@ import Testing
import struct Hummingbird.LocalFileSystem import struct Hummingbird.LocalFileSystem
import struct Logging.Logger import struct Logging.Logger
import protocol Hummingbird.FileProvider import protocol Hummingbird.FileProvider
@testable import DocCMiddleware @testable import struct DocCMiddleware.DocCMiddleware
@Suite("DocC Middleware") @Suite("DocC Middleware", .tags(.middleware))
struct DocCMiddlewareTests { struct DocCMiddlewareTests {
// MARK: Initializers tests // MARK: Initializers tests
#if swift(>=6.2) #if swift(>=6.2)
@Test(.tags(.initializer)) @Test
func `initialize with path to root`() { func `initialize with URI and folder paths`() {
assertInit(pathToRoot: "/path/to/root/docc/documentation") assertInit(configuration: .init(
uriRoot: "/path/to/documentation",
folderRoot: "/location/docc/documentation"
))
} }
@Test(.tags(.initializer)) @Test
func `initialize with type that conforms to the FileProvider protocol`() { func `initialize with URI path and type that conforms to the FileProvider protocol`() {
// GIVEN assertInit(
assertInit(fileProvider: FileProviderStub()) configuration: .init(
uriRoot: "/path/to/documentation",
folderRoot: .empty
),
fileProvider: FileProviderStub()
)
} }
#else #else
@Test("initialize with path to root", .tags(.initializer)) @Test("initialize with URI and folder paths")
func initWithRootPath() { func initWithURIAndFolderPaths() {
assertInit(pathToRoot: "/path/to/root/docc/documentation") assertInit(configuration: .init(
uriRoot: "/path/to/documentation",
folderRoot: "/location/docc/documentation"
))
} }
@Test("initialize with type that conforms to the FileProvider protocol", .tags(.initializer)) @Test("initialize with type that conforms to the FileProvider protocol")
func initWithFileProviderType() { func initWithURIPathAndFileProviderType() {
assertInit(fileProvider: FileProviderStub()) assertInit(
configuration: .init(
uriRoot: "/path/to/documentation",
folderRoot: .empty
),
fileProvider: FileProviderStub()
)
} }
#endif #endif
@@ -54,46 +72,37 @@ private extension DocCMiddlewareTests {
// MARK: Functions // MARK: Functions
/// Asserts the initialization of a `DocCMiddleware` type with a root path in the file system. /// Asserts the initialization of a `DocCMiddleware` type.
/// - Parameters: /// - Parameters:
/// - pathToRoot: A path to the root `DocC` documentation containers. /// - 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(
pathToRoot: String, configuration: DocCMiddleware.Configuration,
logger: Logger = .test logger: Logger = .test,
fileProvider: (any FileProvider)? = nil
) { ) {
// GIVEN // GIVEN
// WHEN // WHEN
let middleware = DocCMiddleware( let middleware = DocCMiddleware(
pathToRoot: pathToRoot, configuration: configuration,
logger: logger logger: logger,
fileProvider: fileProvider
) )
// THEN // THEN
#expect(middleware.fileProvider is LocalFileSystem) #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.label == logger.label)
#expect(middleware.logger.logLevel == logger.logLevel) #expect(middleware.logger.logLevel == logger.logLevel)
}
/// Asserts the initialization of a `DocCMiddleware` type with a type that conforms 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.
func assertInit(
fileProvider: any FileProvider,
logger: Logger = .test
) {
// GIVEN
// WHEN
let middleware = DocCMiddleware(
fileProvider: fileProvider,
logger: logger
)
// THEN if let fileProvider {
#expect(type(of:middleware.fileProvider) == type(of: fileProvider)) #expect(type(of:middleware.fileProvider) == type(of: fileProvider))
#expect(middleware.logger.label == logger.label) } else {
#expect(middleware.logger.logLevel == logger.logLevel) #expect(middleware.fileProvider is LocalFileSystem)
}
} }
} }
@@ -18,8 +18,8 @@ extension Tag {
/// Tag that indicate a test case for an enumeration type. /// Tag that indicate a test case for an enumeration type.
@Tag static var enumeration: Self @Tag static var enumeration: Self
/// Tag that indicate a test case for a type initialization. /// Tag that indicate a test case for a middleware type.
@Tag static var initializer: Self @Tag static var middleware: Self
/// Tag that indicate a test case for a use case type. /// Tag that indicate a test case for a use case type.
@Tag static var useCase: Self @Tag static var useCase: Self