Assuming I have a folder foo
with an index.html
file in it and the following minimal (but most likely not functional) server code for Akka HTTP below:
object Server extends App {
val route: Route =
pathPrefix("foo") {
getFromDirectory("foo")
}
Http().bindAndHandle(route, "0.0.0.0", 8080)
}
index.html
will correctly be served if I open http://localhost:8080/foo/index.html
in a browser, but not if I open http://localhost:8080/foo
or http://localhost:8080/foo/
.
If this is even possible, how can I set up my Akka HTTP routes to serve index.html
files within that location by default?
I know I could do the following:
val route: Route =
path("foo") {
getFromFile("foo/index.html")
} ~
pathPrefix("foo") {
getFromDirectory("foo")
}
But:
- This only makes
http://localhost:8080/foo
work, nothttp://localhost:8080/foo/
- This is very ad-hoc and does not apply globally: if I have a
foo/bar/index.html
file, the problem will be the same.