Spray.io 路径指令服务 index.html

Spray.io Path Directives serve index.html

我想设置一个 Spray 路由来提供目录外的 Web 内容。

例如,以下 URL 应解析为同一文件。

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

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

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

应该从位于 ./web/path/to/thing/index 的文件系统提供 index.html 文件。html

如果明确指定了 "index.html",则以下路由有效,否则无效。

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

我如何在 Spray 路由中表示它?

假设您已经匹配了 "thing" 和 pathPrefix,我认为您想要 pathEndOrSingleSlash,描述了 here。我认为 Spray-routing 不会匹配隐式 "index.html",但无论如何,如果需要,您可以轻松编写指令:

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

更新:

好的,根据您的评论,我认为您只想采用指定的任何路径并提供该目录中的文件。像这样(未经测试)?

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