https://kotlinlang.org logo
#http4k
Title
# http4k
v

Vojtěch Knyttl

03/29/2021, 8:54 PM
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

dave

03/30/2021, 1:28 PM
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

Vojtěch Knyttl

03/30/2021, 1:29 PM
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

dave

03/30/2021, 7:30 PM
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

Vojtěch Knyttl

03/31/2021, 6:17 AM
Thanks, this seems to have helped! This might be helpful if added in http4k examples.
96 Views