2025-03-09 14:41:02 +01:00
|
|
|
import Hummingbird
|
|
|
|
import Logging
|
|
|
|
|
2025-03-26 00:31:16 +01:00
|
|
|
/// A type that sets and builds an application that would be ready to run in the executable target.
|
2025-03-09 14:41:02 +01:00
|
|
|
public struct AppBuilder {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
2025-03-26 00:31:16 +01:00
|
|
|
private let name: String
|
|
|
|
private let archivesPath: String
|
2025-03-09 14:41:02 +01:00
|
|
|
private let environment: Environment
|
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
/// Initialises this type.
|
|
|
|
/// - Parameters:
|
2025-03-26 00:31:16 +01:00
|
|
|
/// - name: A name for the app to build.
|
|
|
|
/// - archivesPath: A relative path to the location of the *DocC* archive containers.
|
2025-03-09 14:41:02 +01:00
|
|
|
public init(
|
2025-03-26 00:31:16 +01:00
|
|
|
name: String,
|
|
|
|
archivesPath: String
|
2025-03-09 14:41:02 +01:00
|
|
|
) {
|
2025-03-26 00:31:16 +01:00
|
|
|
self.name = name
|
|
|
|
self.archivesPath = archivesPath
|
2025-03-09 14:41:02 +01:00
|
|
|
self.environment = Environment()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
/// Sets and builds a ready-to-use application.
|
|
|
|
///
|
|
|
|
/// The application this function builds have the following features:
|
2025-03-26 00:31:16 +01:00
|
|
|
/// * proxy requests to any available *DocC* archive container in the ``AppArguments/archivesPath`` location;
|
2025-03-09 14:41:02 +01:00
|
|
|
/// * log any request to any available *DocC* archive container in the application console.
|
|
|
|
///
|
|
|
|
/// - Parameter arguments: A set in input parameters used while building the application.
|
|
|
|
/// - Returns: A configured, functional application that is ready to use.
|
|
|
|
public func callAsFunction(_ arguments: some AppArguments) async throws -> some ApplicationProtocol {
|
|
|
|
let logger = logger(level: arguments.logLevel)
|
|
|
|
let router = router(logger: logger)
|
|
|
|
|
|
|
|
return Application(
|
|
|
|
router: router,
|
|
|
|
configuration: .init(
|
2025-03-26 00:31:16 +01:00
|
|
|
address: .hostname(
|
|
|
|
arguments.hostname,
|
|
|
|
port: arguments.port
|
|
|
|
),
|
|
|
|
serverName: name
|
2025-03-09 14:41:02 +01:00
|
|
|
),
|
|
|
|
logger: logger
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension AppBuilder {
|
|
|
|
|
|
|
|
// MARK: Type aliases
|
|
|
|
|
|
|
|
typealias AppRequestContext = BasicRequestContext
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func logger(level: Logger.Level?) -> Logger {
|
2025-03-26 00:31:16 +01:00
|
|
|
var logger = Logger(label: name)
|
2025-03-09 14:41:02 +01:00
|
|
|
|
|
|
|
logger.logLevel = level
|
|
|
|
?? environment.logLevel.flatMap { Logger.Level(rawValue: $0) ?? .info }
|
|
|
|
?? .info
|
|
|
|
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func router(logger: Logger) -> Router<AppRequestContext> {
|
|
|
|
let router = Router()
|
|
|
|
|
|
|
|
router.addMiddleware {
|
|
|
|
LogRequestsMiddleware(logger.logLevel)
|
2025-04-22 10:37:49 +02:00
|
|
|
DocCMiddleware(archivesPath, logger: logger)
|
2025-03-09 14:41:02 +01:00
|
|
|
}
|
2025-03-10 01:58:33 +01:00
|
|
|
|
2025-04-17 01:39:27 +02:00
|
|
|
ArchiveController(
|
|
|
|
archivesPath,
|
|
|
|
logger: logger
|
|
|
|
)
|
|
|
|
.register(to: router)
|
2025-03-09 14:41:02 +01:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|