Updated the DocCMiddleware middleware in the library target to support hosting from docc archives.
This commit is contained in:
parent
882f791181
commit
d19d34fafb
@ -2,11 +2,19 @@ enum Path {
|
|||||||
|
|
||||||
// MARK: Functions
|
// 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 {
|
static func archiveName(from path: String) -> String {
|
||||||
let pathComponents = path.split(separator: .forwardSlash)
|
let pathComponents = path.split(separator: .forwardSlash)
|
||||||
|
|
||||||
return pathComponents.count > 1
|
return pathComponents.count > 1
|
||||||
? String(pathComponents[1]).lowercased()
|
? .init(pathComponents[1]).lowercased()
|
||||||
: .empty
|
: .empty
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,5 +45,5 @@ private extension Character {
|
|||||||
// MARK: - String+Formats
|
// MARK: - String+Formats
|
||||||
|
|
||||||
private extension String.Format {
|
private extension String.Format {
|
||||||
static let archiveDocC = "%@.doccarchive"
|
static let archiveDocC = "/%@.doccarchive"
|
||||||
}
|
}
|
||||||
|
@ -53,10 +53,11 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
|
|||||||
|
|
||||||
print(uriPath)
|
print(uriPath)
|
||||||
|
|
||||||
guard uriPath.starts(with: /^\/docs\/\w+/) else {
|
guard uriPath.starts(with: /^\/archives\/\w+/) else {
|
||||||
return try await next(input, context)
|
return try await next(input, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pathArchive = Path.archivePath(from: uriPath)
|
||||||
let nameArchive = Path.archiveName(from: uriPath)
|
let nameArchive = Path.archiveName(from: uriPath)
|
||||||
let uriResource = Path.resourcePath(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 uriResource.contains(staticFile.path) {
|
||||||
if staticFile == .documentation {
|
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
|
// 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 {
|
} else {
|
||||||
// Send all requests to favicon.ico, favicon.svg, and theme-settings.json to their respective files
|
// 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 {
|
for assetPrefix in AssetPrefix.allCases {
|
||||||
if uriResource.contains(assetPrefix.path) {
|
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.contains(indexPrefix.path) {
|
||||||
if uriResource.hasSuffix(.forwardSlash) {
|
if uriResource.hasSuffix(.forwardSlash) {
|
||||||
return try await serveFile(
|
return try await serveFile(
|
||||||
"/documentation/\(nameArchive)/index.html",
|
"\(indexPrefix.path)/\(nameArchive)/index.html",
|
||||||
at: "/docs",
|
at: pathArchive,
|
||||||
context: context
|
context: context
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@ -99,7 +112,7 @@ struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: Rou
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return .init(status: .ok)
|
throw HTTPError(.notFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user