Updated the DocCMiddleware middleware in the library target to support hosting from docc archives.

This commit is contained in:
Javier Cicchelli 2025-02-26 00:22:14 +01:00
parent 882f791181
commit d19d34fafb
2 changed files with 30 additions and 9 deletions

View File

@ -2,11 +2,19 @@ enum Path {
// MARK: Functions
static func archivePath(from path: String) -> String {
let pathComponents = path.split(separator: .forwardSlash)
return pathComponents.count > 1
? .init(format: .Format.archiveDocC, String(pathComponents[1]))
: .empty
}
static func archiveName(from path: String) -> String {
let pathComponents = path.split(separator: .forwardSlash)
return pathComponents.count > 1
? String(pathComponents[1]).lowercased()
? .init(pathComponents[1]).lowercased()
: .empty
}
@ -37,5 +45,5 @@ private extension Character {
// MARK: - String+Formats
private extension String.Format {
static let archiveDocC = "%@.doccarchive"
static let archiveDocC = "/%@.doccarchive"
}

View File

@ -53,10 +53,11 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
print(uriPath)
guard uriPath.starts(with: /^\/docs\/\w+/) else {
guard uriPath.starts(with: /^\/archives\/\w+/) else {
return try await next(input, context)
}
let pathArchive = Path.archivePath(from: uriPath)
let nameArchive = Path.archiveName(from: uriPath)
let uriResource = Path.resourcePath(from: uriPath)
@ -71,17 +72,29 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
if uriResource.contains(staticFile.path) {
if staticFile == .documentation {
// Send all requests to /data/documentation.json to the file in the data/documentation/ folder that has the name of the module and ends with .json
return try await serveFile("/data/documentation/\(nameArchive).json", at: "/docs", context: context)
return try await serveFile(
"/data/documentation/\(nameArchive).json",
at: pathArchive,
context: context
)
} else {
// Send all requests to favicon.ico, favicon.svg, and theme-settings.json to their respective files
return try await serveFile(uriResource, at: "/docs", context: context)
return try await serveFile(
uriResource,
at: pathArchive,
context: context
)
}
}
}
for assetPrefix in AssetPrefix.allCases {
if uriResource.contains(assetPrefix.path) {
return try await serveFile(uriResource, at: "/docs", context: context)
return try await serveFile(
uriResource,
at: pathArchive,
context: context
)
}
}
@ -89,8 +102,8 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
if uriResource.contains(indexPrefix.path) {
if uriResource.hasSuffix(.forwardSlash) {
return try await serveFile(
"/documentation/\(nameArchive)/index.html",
at: "/docs",
"\(indexPrefix.path)/\(nameArchive)/index.html",
at: pathArchive,
context: context
)
} else {
@ -99,7 +112,7 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
}
}
return .init(status: .ok)
throw HTTPError(.notFound)
}
}