Replies: 3 comments
-
Hmm, I'm not sure I understand why this is difficult to do. Is the following insufficient? #[get("/<path..>")]
fn file_to_html(mut path: PathBuf) -> Option<NamedFile> {
path.set_extension("html");
NamedFile::open(Path::new("static").join(path)).await.ok()
} |
Beta Was this translation helpful? Give feedback.
-
I guess I did a bad job explaining what I mean. I'm not concerned with a single file. I want to expose all files under a particular directory without requiring the extension. So if the directory ./apps/ has files named foo.html and some/more/directories/bar.html then from the browser /apps/foo, /apps/foo.html, /apps/some/more/directories/bar and /apps/some/more/directories/bar.html would automatically find them -- and any others that get added in the future with no code changes or recompiling. This is called content negotiation in web servers like Apache HTTPD (though it supports a variety of extensions and also considers Accept headers in the request, which I'm not proposing to support here). What you've suggested would require the server Rust code to know and be kept up to date every time a .html file gets added to the directory. While this would work, technically, it would create maintenance burden and couple the code to the directory structure in an undesirable way. Fair enough if you consider this feature not worth implementing in Rocket. As I say, I've replaced FileServer with my own thing that works differently in a few ways that I like and that I can extend as needed. I just wanted to offer it as an option. |
Beta Was this translation helpful? Give feedback.
-
That's exactly what the example code I posted above does. To adapt it to this exact scenario, just replace
Not at all. I think you might be misinterpreting what the code does. Give it a try! |
Beta Was this translation helpful? Give feedback.
-
I'm experimenting with Rocket and I've found something I'd like to do that isn't easy with the current implementation: I'd like to emulate the content negotiation feature of popular web servers so that a link to /path/thing will fetch /path/thing.html if it exists. I find this useful because it's easy to replace an HTML file with a directory as things get more sophisticated without having to change any HTML href values.
I've implemented this in my own code with a replacement for the Rocket FileServer struct, but the following patch would add it to Rocket as an optional feature.
extfile.txt
Beta Was this translation helpful? Give feedback.
All reactions