Skip to content
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

Closed
bbigras opened this issue Oct 19, 2018 · 7 comments
Closed

feature request: StaticFiles::index_file without redirect #557

bbigras opened this issue Oct 19, 2018 · 7 comments
Labels
A-files project: actix-files C-improvement Category: an improvement to existing functionality

Comments

@bbigras
Copy link
Contributor

bbigras commented Oct 19, 2018

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.

@DoumanAsh DoumanAsh added the C-improvement Category: an improvement to existing functionality label Nov 23, 2018
@Boscop
Copy link

Boscop commented Nov 26, 2018

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.)

@dessalines
Copy link

What type is front end dir above, I can't get it working.

@dessalines
Copy link

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")?)
}

@DoumanAsh
Copy link
Contributor

JFYI This is planned for 0.8 in my PR on refactoring of Static Files. since it is breaking change.

@Boscop
Copy link

Boscop commented Nov 27, 2018

@dessalines frontend_dir is a PathBuf above. I use an env var to set it:

let frontend_dir = PathBuf::from(env::var("FRONTEND_DIR").expect("FRONTEND_DIR"));

Because on my laptop, I have it set to FRONTEND_DIR=target/deploy in my .env file (btw, for most env vars I usually use envy), and then I build my wasm frontend to that dir with cargo web deploy, and in production, I set it to a separate dir.

@fafhrd91
Copy link
Member

@DoumanAsh can we close this?

@fafhrd91 fafhrd91 added the A-files project: actix-files label Mar 23, 2019
@DoumanAsh
Copy link
Contributor

Yes, it is in 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-files project: actix-files C-improvement Category: an improvement to existing functionality
Projects
None yet
Development

No branches or pull requests

5 participants