Upio
Upio

Reputation: 1374

Spray.io Path Directives serve index.html

I want to set up a Spray route to serve web content out of a directory.

For example, the below URLs should resolve to the same file.

http://mywebsite.com/path/to/thing

http://mywebsite.com/path/to/thing/

http://mywebsite.com/path/to/thing/index.html

Should serve the index.html file from the filesystem at ./web/path/to/thing/index.html

The following route works if "index.html" is explicitly specified but not otherwise.

pathPrefix("") {
  getFromDirectory("./web/")
}

How do i represent this in Spray routing?

Upvotes: 1

Views: 637

Answers (1)

AmigoNico
AmigoNico

Reputation: 6852

Assuming you have already matched up to "thing" with pathPrefix, I think you want pathEndOrSingleSlash, described here. I didn't think that Spray-routing would match an implicit "index.html", but in any case you can compose directives easily if you need to:

(pathEndOrSingleSlash | path("index.html")) { ... }

UPDATE:

OK, from your comment I am thinking that you just want to take whatever path was specified and serve up the file from that directory. Something like this (untested)?

path(Segment) { rawPath =>
  getFromFile("web/" + if (rawPath endsWith "/") rawPath + "index.html" else rawPath)
}

Upvotes: 3

Related Questions