Added logging to the ArchiveController controller in the library target.

This commit is contained in:
Javier Cicchelli 2025-04-17 01:39:27 +02:00
parent 42758f29c1
commit fd7dd9bd01
2 changed files with 85 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import Hummingbird
import Logging
/// A controller type that provides information about the *DocC* archives containers.
struct ArchiveController<Context: RequestContext> {
@ -7,6 +8,7 @@ struct ArchiveController<Context: RequestContext> {
private let archivesPath: String
private let fileService: any FileServicing
private let logger: Logger
// MARK: Initialisers
@ -14,12 +16,15 @@ struct ArchiveController<Context: RequestContext> {
/// - Parameters:
/// - archivesPath: A path in the local file system where the *DocC* archive contained are located.
/// - fileService: A service that interfaces with the local file system.
/// - logger: A service that interfaces with the logging system.
init(
_ archivesPath: String,
fileService: any FileServicing = FileService()
fileService: any FileServicing = FileService(),
logger: Logger
) {
self.archivesPath = archivesPath
self.fileService = fileService
self.logger = logger
}
// MARK: Functions
@ -49,19 +54,89 @@ private extension ArchiveController {
.map { $0.dropLast(String.suffixArchive.count) }
.map(String.init)
.sorted { $0 < $1 }
return .init(nameArchives)
let archiveList: ArchiveList = .init(nameArchives)
defer {
logger.debug(
"The codable response returned: \(String(describing: archiveList))",
metadata: .metadata(
context: context,
request: request,
statusCode: .ok
),
source: .source
)
}
return archiveList
} catch .folderNotFound {
defer {
logger.error(
"The resource has not been found.",
metadata: .metadata(
context: context,
request: request,
statusCode: .notFound
),
source: .source
)
}
throw .init(.notFound)
} catch .folderPathEmpty, .folderNotDirectory {
throw .init(.unprocessableContent)
defer {
logger.error(
"The folder for the resource has not been located.",
metadata: .metadata(
context: context,
request: request,
statusCode: .notAcceptable
),
source: .source
)
}
throw .init(.notAcceptable)
} catch {
defer {
logger.error(
"The request has issues.",
metadata: .metadata(
context: context,
request: request,
statusCode: .badRequest
),
source: .source
)
}
throw .init(.badRequest)
}
}
}
// MARK: - Logger,Metadata+Functions
private extension Logger.Metadata {
// MARK: Functions
static func metadata<Context: RequestContext>(
context: Context,
request: Request,
statusCode: HTTPResponse.Status
) -> Logger.Metadata {
return [
"hb.request.id": "\(context.id)",
"hb.request.method": "\(request.method.rawValue)",
"hb.request.path": "\(request.uri.path)",
"hb.request.status": "\(statusCode.code)"
]
}
}
// MARK: - RouterPath+Constants
private extension RouterPath {
@ -71,5 +146,6 @@ private extension RouterPath {
// MARK: - String+Constants
private extension String {
static let source = "ArchiveController"
static let suffixArchive: String = ".doccarchive"
}

View File

@ -82,7 +82,11 @@ private extension AppBuilder {
DocCMiddleware(archivesPath)
}
ArchiveController(archivesPath).register(to: router)
ArchiveController(
archivesPath,
logger: logger
)
.register(to: router)
return router
}