Normally React builds are just served as static files from a webserver like nginx but I want to serve the front end static files using Rust Rocket from a React build and I'm struggling to find a nice way of doing it, here are the routes I've set up
#[get("/")]
fn index() -> io::Result<NamedFile> {
NamedFile::open("build/index.html")
}
#[get("/<file..>", rank = 2)]
fn build_dir(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("build/").join(file)).ok()
}
#[get("/static/<file..>")]
fn static_dir(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("build/static/").join(file)).ok()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, build_dir])
.mount("/static", routes![static_dir])
}
This works but it doesn't serve things like favicons or the manifest.json file and I'd rather not add specific routes for each of these files, has anybody solved this problem in a better way ?
Please see the project code here