diff --git a/Library/Sources/Internal/Extensions/Path+Functions.swift b/Library/Sources/Internal/Extensions/Path+Functions.swift index 2ad1ed1..51fe991 100644 --- a/Library/Sources/Internal/Extensions/Path+Functions.swift +++ b/Library/Sources/Internal/Extensions/Path+Functions.swift @@ -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" } diff --git a/Library/Sources/Internal/Middlewares/DocCMiddleware.swift b/Library/Sources/Internal/Middlewares/DocCMiddleware.swift index fa46670..c362775 100644 --- a/Library/Sources/Internal/Middlewares/DocCMiddleware.swift +++ b/Library/Sources/Internal/Middlewares/DocCMiddleware.swift @@ -53,10 +53,11 @@ struct DocCMiddleware: 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: 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: 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: Rou } } - return .init(status: .ok) + throw HTTPError(.notFound) } }