Context
I have been serving files via Vapor (v4) successfully with:
func configure(_ app: Application) throws {
// ...
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
// ...
}
Problem
- This attempts to map any/every URL that would match a file within publicDirectory.
- How would one limit the "reach" of the
FileMiddlewareto only requests whose URL have a specific prefix? (say/files/)
As in, if I have /Public/foobar.txt, one would request it via GET /files/foobar.txt while GET /foobar.txt would NOT match anything.
Considered approaches
- My understanding is that
Middlewareis applied server-wide (affects every "route") and not a subset ... so this wouldn't work. - Moving
/Public/foobar.txtto/Public/files/foobar.txtis NOT a solution as I'm trying to limit URLs that can potentially get mapped to the file system... - Redirecting
GET /files/foobar.txttoGET /foobar.txtis also not acceptable. Again, I need to limit the potential URLs which are mapped to the file system.