expert expert - 2 months ago 14
Scala Question

Loaded html requests static content from parent directory

How can I solve issue of missing static content when serving html from Spray (or Akka-Http) ? Base url of my service is

/api
(even though it should be irrelevant in this case).

Here is my route

get {
pathPrefix("swagger") {
pathEndOrSingleSlash {
getFromResource("swagger-ui/index.html")
} ~
getFromResourceDirectory("swagger-ui")
}
}


Loaded html can find css and js file when I open it as

/api/swagger/


but when I open

/api/swagger
(without trailing slash)

loaded html attempts to get content from

/api/css/reset.css
instead of
/api/swagger/css/reset.css


How should I rewrite my route to cover both cases ?

Answer

I ended up adding redirect. If anyone knows more elegant solution please post.

pathPrefix("swagger") {
  CachingDirectives.cachingProhibited {
    pathEnd {
      redirect("/api/swagger/", StatusCodes.TemporaryRedirect)
    } ~
      pathSingleSlash {
        getFromResource("swagger-ui/index.html")
      } ~
      getFromResourceDirectory("swagger-ui")
  }
}
Comments