Is there a way to route to a single static file? e...
# http4k
v
Is there a way to route to a single static file? eg:
Copy code
routes(
    "/docs/swagger-ui/swagger-ui.css" bind static(Classpath("/swagger-ui/swagger-ui.css")), // read from src/main/resources
    "/docs" bind static(Classpath("META-INF/resources/webjars/swagger-ui/3.25.2"))
basically I want to override the single
swagger-ui.css
file and leave the rest of swagger tools as default. with the example above, the file is ignored and only content from the swagger-ui webjars is served.
d
your problem here is that it's looking for
/swagger-ui/swagger-ui.css/docs/swagger-ui/swagger-ui.css
.
you probably just need to do
Classpath()
v
I will try again, but I think I tried and it still served the contents of the jar
thannks!
ok - only thing I managed to get to work is this:
Copy code
"/docs" bind static(Classpath("/docs")),
but once I add the file, it immediately gets to 404
but that is in contradiction with what you have written above
Copy code
"/docs" bind static(Classpath()),
does not work
d
yeah - it doesn't seem to work. something like this does:
Copy code
fun main() {
    val app = routes(
        override("/org/index.html", "/"),
        static(Classpath("/bar"))
    )

    println("1" + app(Request(GET, "/org/index.html")).bodyString())
    println("2" + app(Request(GET, "/index.html")).bodyString())
}

private fun override(resource: String, root: String) = resource bind { r: Request ->
    Response(OK).body(Classpath(root).load(resource.drop(1))!!.openStream())
}
🙌 1
v
Thanks, this seems to have helped! This might be helpful if added in http4k examples.
105 Views