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

This commit is contained in:
2025-02-26 00:22:14 +01:00
parent 882f791181
commit d19d34fafb
2 changed files with 30 additions and 9 deletions
@@ -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)
}
}