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

Allow routing both "/" and "/index.html" to the same handler #115

Open
joshuawarner32 opened this issue Jun 10, 2016 · 3 comments
Open

Allow routing both "/" and "/index.html" to the same handler #115

joshuawarner32 opened this issue Jun 10, 2016 · 3 comments
Labels

Comments

@joshuawarner32
Copy link

Currently, to have a router forward requests for "/" and "/index.html" (and "/js/example.js", etc) to the same handler, I currently have to do something like:

router.get("/", Static::new(Path::new("public")));
router.get("/*", Static::new(Path::new("public"))); // notice the "*"

It would be convenient if I could have both of those redirect to the same handler. For example, this syntax would make sense to me:

router.get("*", Static::new(Path::new("public")));

If there's an option everyone can settle on, I'm happy to take a crack at implementing this.

Thoughts?

@Hoverbear
Copy link

Hm, could you not add something ad the end of your router that globs anything not matched ending with / to redirect to /index.html?

Express.js allows for foo/bar/baz/?index.html as a route that would match either, for example. That could be an option.

@joshuawarner32
Copy link
Author

Here's a little more detail on what I'm trying to do:

  • I have an API endpoint at /api that I have various handlers registered for.
  • I want everything else, including /, /index.html, /js/*, etc - to be handled by a single Static server.

To expand on the code segment:

let mut router = Router::new();
router.post("/api/files", handle_upload);
router.get("/*", Static::new(Path::new("public")));
router.get("/", Static::new(Path::new("public")));

... which works fine, but is a little redundantly redundant.

I'd love to be able to replace those last two lines with a single pattern and a single instantiation of Static.

Also, Static handles serving /index.html when asked for / just fine - that's not the problem.

Does that help?

Lastly, I understand that this could easily be a bikeshedding issue, so feel free to close this if it's just not the direction you want to go.

@untitaker
Copy link
Member

I think you want to use the mount crate for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants