Integrated the AppOptions options to the"init(name: archivesPath: )" initialisation function for the AppBuilder type in the library target.

This commit is contained in:
Javier Cicchelli 2025-03-26 00:31:16 +01:00
parent f9fc29c8da
commit b13ba625f4
3 changed files with 26 additions and 20 deletions

View File

@ -11,7 +11,10 @@ struct App: AsyncParsableCommand {
// MARK: Functions // MARK: Functions
mutating func run() async throws { mutating func run() async throws {
let appBuilder = AppBuilder(appName: "Doxy") let appBuilder = AppBuilder(
name: options.name,
archivesPath: options.archivesPath
)
let app = try await appBuilder(options) let app = try await appBuilder(options)

View File

@ -1,27 +1,27 @@
import Hummingbird import Hummingbird
import Logging import Logging
/// A type that sets and builds an application that should be ready to run. /// A type that sets and builds an application that would be ready to run in the executable target.
public struct AppBuilder { public struct AppBuilder {
// MARK: Properties // MARK: Properties
private let appName: String private let name: String
private let folderArchives: String private let archivesPath: String
private let environment: Environment private let environment: Environment
// MARK: Initialisers // MARK: Initialisers
/// Initialises this type. /// Initialises this type.
/// - Parameters: /// - Parameters:
/// - appName: A name for the app to build. /// - name: A name for the app to build.
/// - folderArchives: A relative path to the location of the *DocC* archive containers. /// - archivesPath: A relative path to the location of the *DocC* archive containers.
public init( public init(
appName: String, name: String,
folderArchives: String = "Resources/Archives" archivesPath: String
) { ) {
self.appName = appName self.name = name
self.folderArchives = folderArchives self.archivesPath = archivesPath
self.environment = Environment() self.environment = Environment()
} }
@ -30,7 +30,7 @@ public struct AppBuilder {
/// Sets and builds a ready-to-use application. /// Sets and builds a ready-to-use application.
/// ///
/// The application this function builds have the following features: /// The application this function builds have the following features:
/// * proxy requests to any available *DocC* archive container in the `folderArchives` location; /// * proxy requests to any available *DocC* archive container in the ``AppArguments/archivesPath`` location;
/// * log any request to any available *DocC* archive container in the application console. /// * 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. /// - Parameter arguments: A set in input parameters used while building the application.
@ -42,8 +42,11 @@ public struct AppBuilder {
return Application( return Application(
router: router, router: router,
configuration: .init( configuration: .init(
address: .hostname(arguments.hostname, port: arguments.port), address: .hostname(
serverName: appName arguments.hostname,
port: arguments.port
),
serverName: name
), ),
logger: logger logger: logger
) )
@ -62,7 +65,7 @@ private extension AppBuilder {
// MARK: Functions // MARK: Functions
func logger(level: Logger.Level?) -> Logger { func logger(level: Logger.Level?) -> Logger {
var logger = Logger(label: appName) var logger = Logger(label: name)
logger.logLevel = level logger.logLevel = level
?? environment.logLevel.flatMap { Logger.Level(rawValue: $0) ?? .info } ?? environment.logLevel.flatMap { Logger.Level(rawValue: $0) ?? .info }
@ -76,10 +79,10 @@ private extension AppBuilder {
router.addMiddleware { router.addMiddleware {
LogRequestsMiddleware(logger.logLevel) LogRequestsMiddleware(logger.logLevel)
DocCMiddleware(folderArchives) DocCMiddleware(archivesPath)
} }
ArchiveController(folderArchives).register(to: router) ArchiveController(archivesPath).register(to: router)
return router return router
} }

View File

@ -8,10 +8,6 @@ struct AppBuilderTests {
// MARK: Properties // MARK: Properties
private let appBuilder = AppBuilder(
appName: "DoxyTest",
folderArchives: "Resources/Archives/Test"
)
private let arguments = TestArguments() private let arguments = TestArguments()
// MARK: Route tests // MARK: Route tests
@ -21,6 +17,10 @@ struct AppBuilderTests {
uri: String, uri: String,
expects status: HTTPResponse.Status expects status: HTTPResponse.Status
) async throws { ) async throws {
let appBuilder = AppBuilder(
name: arguments.name,
archivesPath: arguments.archivesPath
)
let app = try await appBuilder(arguments) let app = try await appBuilder(arguments)
try await app.test(.router) { client in try await app.test(.router) { client in