Skip to content

Commit 682aea7

Browse files
author
sfomuseumbot
committed
add StripPrefix option
1 parent a39f256 commit 682aea7

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/SwifterProtomaps/SwifterProtomaps.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,43 @@ public struct ServeProtomapsOptions {
1212
public var AllowHeaders: String
1313
/// Logger is an option swift-logging instance for recording errors and warning.
1414
public var Logger: Logger?
15+
/// Optional string to strip from URL paths before processing
16+
public var StripPrefix: String
1517

1618
public init(root: URL) {
1719
Root = root
1820
AllowOrigins = ""
1921
AllowHeaders = ""
22+
StripPrefix = ""
2023
}
2124
}
2225

2326
/// ServeProtomapsTiles will serve HTTP range requests for zero or more Protomaps tile databases in a directory.
2427
@available(iOS 13.4, *)
2528
@available(macOS 10.15.4, *)
2629
public func ServeProtomapsTiles(_ opts: ServeProtomapsOptions) -> ((HttpRequest) -> HttpResponse) {
30+
2731
return { r in
2832

2933
var rsp_headers = [String: String]()
3034

31-
guard let rel_path = r.params.first else {
35+
guard let req_path = r.params.first else {
3236
return .raw(404, "Not found", rsp_headers, {_ in })
3337
}
3438

35-
let uri = opts.Root.appendingPathComponent(rel_path.value)
39+
var rel_path = req_path.value
40+
41+
if opts.StripPrefix != "" {
42+
rel_path = rel_path.replacingOccurrences(of: opts.StripPrefix, with: "")
43+
}
44+
45+
let uri = opts.Root.appendingPathComponent(rel_path)
3646
let path = uri.absoluteString
3747

3848
// https://developer.apple.com/documentation/foundation/filehandle
3949

4050
guard let file = FileHandle(forReadingAtPath: path) else {
51+
opts.Logger?.error("Not found: \(path)")
4152
return .raw(404, "Not found", rsp_headers, {_ in })
4253
}
4354

0 commit comments

Comments
 (0)