-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add AssetMiddleware for react-router routing priority
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
"xfp-middleware", | ||
"error", | ||
"date", | ||
"asset-middleware", | ||
"file" | ||
], | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
import HTTP | ||
import libc | ||
|
||
/// Servers files from the supplied public directory | ||
/// on not found errors. | ||
public final class AssetMiddleware: Middleware { | ||
|
||
private var assetDirs: [String] | ||
private var publicDir: String | ||
|
||
private let loader = DataFile() | ||
private let chunkSize: Int | ||
|
||
public init(assetDirs: [String], publicDir: String, chunkSize: Int? = nil) { | ||
// Remove last "/" from the publicDir if present, so we can directly append uri path from the request. | ||
self.assetDirs = assetDirs | ||
self.publicDir = publicDir.finished(with: "/") | ||
self.chunkSize = chunkSize ?? 32_768 // 2^15 | ||
} | ||
|
||
public func respond(to request: Request, chainingTo next: Responder) throws -> Response { | ||
var path = request.uri.path | ||
guard !path.contains("../") else { | ||
throw HTTP.Status.forbidden | ||
} | ||
if path.hasPrefix("/") { | ||
path = String(path.dropFirst()) | ||
} | ||
if assetDirs.index(where: { path.starts(with: $0) }) != nil { | ||
let filePath = publicDir + path | ||
let ifNoneMatch = request.headers["If-None-Match"] | ||
return try Response(filePath: filePath, ifNoneMatch: ifNoneMatch, chunkSize: chunkSize) | ||
} | ||
return try next.respond(to: request) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters