0
votes

with akka http, I use getFromDirectory() to serve files from a directory. How can I say that if a requested file is not found in a dir, look at it to another directory?

Something like this:

lazy val userRoutes: Route = 
  pathPrefix("files") {
    getFromDirectory("./static")
    .orElse(getFromDirectory(".generated1"))  // this orElse function does not exist. how to achive something like this?
    .orElse(getFromDirectory(".generated2"))  // this orElse function does }
1

1 Answers

0
votes

You can have something like this

  val route =
      pathPrefix("prefix") {
        List("/tmp", "/home/john", "/home/john/Downloads/").map(getFromDirectory).reduceLeft(_ ~ _)
      }

Here, it compose the routes for all folders and , since it uses reject when the file is not found, it will fallback to the next route

You can find this usage of reduceLeft in the implementation of getFromBrowseableDirectories