Distinguishing directory-ish from file-ish paths for directory indexes


I’m trying to implement old-school directory indexing behavior for dynamically generated resources via tapir.

When I notice definition of a simple GET endpoint like /mypath/index.html, I want to augment it with an endpoint like /mypath/ that serves the HTML file.

If I see a URL like /mypath, without the trailing slash, I don’t want to serve the HTML directly (because that breaks relative URLs in the index). I want to redirect to to /mypath/ and then serve. (Browsers will take the base URL in this format to be /mypath rather than /, preserving relative URLs.)

I’ve tried several approaches to try to get an endpoint to distinguish /mypath/ from /mypath without too much success. Since the model parses paths to elements, it’s not obvious how to see the trailing slash. I’ve tried using

 .in(extractFromRequest( _.showShort ))

hoping I could check the generated String for the trailing slash, but what seems to happen is I automatically get the redirect opposite to the one I want, /mypath/ -> /mypath, and then the short request I see is always

GET /d24n-test

with no trailing slash.

Thanks for any thoughts or hints!

I think the sttp.tapir.noTrailingSlash input might be helpful. It’s used e.g. in the SwaggerUI to implement the exact functionality you describe.

That’s pretty exactly it! Thank you!