Implemented (a first version of) the DocCMiddleware middleware in the library target.
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import Hummingbird
|
||||
import Logging
|
||||
import NIOPosix
|
||||
|
||||
struct DocCMiddleware<Context: RequestContext, AssetProvider: FileProvider>: RouterMiddleware {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let assetProvider: AssetProvider
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(
|
||||
_ rootFolder: String,
|
||||
threadPool: NIOThreadPool = .singleton,
|
||||
logger: Logger = .init(label: "DocCMiddleware")
|
||||
) where AssetProvider == LocalFileSystem {
|
||||
self.assetProvider = LocalFileSystem(
|
||||
rootFolder: rootFolder,
|
||||
threadPool: threadPool,
|
||||
logger: logger
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
init(assetProvider: AssetProvider) {
|
||||
self.assetProvider = assetProvider
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func handle(
|
||||
_ input: Request,
|
||||
context: Context,
|
||||
next: (Request, Context) async throws -> Response
|
||||
) async throws -> Response {
|
||||
guard
|
||||
let uriPath = input.uri.path.removingPercentEncoding,
|
||||
!uriPath.contains(.previousFolder),
|
||||
uriPath.hasPrefix(.forwardSlash)
|
||||
else {
|
||||
throw HTTPError(.badRequest)
|
||||
}
|
||||
|
||||
/*
|
||||
Send all requests starting with /documentation/ or /tutorials/ to the index.html file
|
||||
Send all requests starting with /css/, /data/, /downloads/, /images/, /img/, /index/, /js/, or /videos/ to their respective folders
|
||||
Send all requests to favicon.ico, favicon.svg, and theme-settings.json to their respective files
|
||||
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
|
||||
Redirect requests to / and /documentation to /documentation/
|
||||
Redirect requests to /tutorials to /tutorials/
|
||||
*/
|
||||
|
||||
print(uriPath)
|
||||
|
||||
guard uriPath.starts(with: /^\/docs\/\w+/) else {
|
||||
return try await next(input, context)
|
||||
}
|
||||
|
||||
let nameArchive = Path.archiveName(from: uriPath)
|
||||
let uriResource = Path.resourcePath(from: uriPath)
|
||||
|
||||
print(nameArchive)
|
||||
print(uriResource)
|
||||
|
||||
if uriResource == .forwardSlash {
|
||||
return .redirect(to: uriPath + "/documentation")
|
||||
}
|
||||
|
||||
for staticFile in StaticFile.allCases {
|
||||
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)
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for assetPrefix in AssetPrefix.allCases {
|
||||
if uriResource.contains(assetPrefix.path) {
|
||||
return try await serveFile(uriResource, at: "/docs", context: context)
|
||||
}
|
||||
}
|
||||
|
||||
for indexPrefix in IndexPrefix.allCases {
|
||||
if uriResource.contains(indexPrefix.path) {
|
||||
if uriResource.hasSuffix(.forwardSlash) {
|
||||
return try await serveFile(
|
||||
"/documentation/\(nameArchive)/index.html",
|
||||
at: "/docs",
|
||||
context: context
|
||||
)
|
||||
} else {
|
||||
return .redirect(to: uriPath + "/")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return .init(status: .ok)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension DocCMiddleware {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func serveFile(
|
||||
_ path: String,
|
||||
at folder: String? = nil,
|
||||
context: Context
|
||||
) async throws -> Response {
|
||||
let filePath = if let folder {
|
||||
folder + path
|
||||
} else {
|
||||
path
|
||||
}
|
||||
|
||||
print(filePath)
|
||||
|
||||
guard let fileIdentifier = assetProvider.getFileIdentifier(filePath) else {
|
||||
throw HTTPError(.notFound)
|
||||
}
|
||||
|
||||
let body = try await assetProvider.loadFile(id: fileIdentifier, context: context)
|
||||
|
||||
print(fileIdentifier)
|
||||
print(body)
|
||||
|
||||
return .init(status: .ok, headers: [:], body: body)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user