New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: StaticFiles::index_file without redirect #557
Comments
I had the same issue, this is my solution: .resource("/", {
let frontend_dir_ = frontend_dir.clone();
move |r| r.f(move |_r| -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open(frontend_dir_.join("index.html"))?)
})
})
.handler("/", fs::StaticFiles::new(&frontend_dir).unwrap()) I think since this is a common use case (e.g. for single page web apps), it makes sense to have this in actix-web, e.g.: .handler("/",
fs::StaticFiles::new(&frontend_dir).unwrap().transparent_index_file("index.html")
) (Transparent serving instead of redirecting.) |
What type is front end dir above, I can't get it working. |
Okay I found the right way to do this: .resource("/favicon.ico", |r| r.f(favicon))
.resource("/", |r| r.f(index))
.handler(
"/static",
fs::StaticFiles::new("../ui/dist/")
.unwrap()
// .index_file("index.html"),
)
.finish() And the methods can look like: fn index(_req: &HttpRequest) -> Result<NamedFile, actix_web::error::Error> {
Ok(NamedFile::open("../ui/dist/index.html")?)
}
fn favicon(_req: &HttpRequest) -> Result<NamedFile, actix_web::error::Error> {
Ok(NamedFile::open("../ui/src/favicon.ico")?)
} |
JFYI This is planned for 0.8 in my PR on refactoring of Static Files. since it is breaking change. |
@dessalines let frontend_dir = PathBuf::from(env::var("FRONTEND_DIR").expect("FRONTEND_DIR")); Because on my laptop, I have it set to |
@DoumanAsh can we close this? |
Yes, it is in 1.0 |
I prefer to have my main URL "clean" (without /index.html) but something like index_file (without a redirect) would be simpler than using default_handler with NamedFile.
The text was updated successfully, but these errors were encountered: