Deploy WebJars

Gradle setup

implementation group: "org.http4k", name: "http4k-core", version: "4.13.0.0"

// for the example...
implementation group: "org.webjars", name: "swagger-ui", version: "3.43.0"

WebJars is a library to ship pre-packaged Web assets on your classpath by just adding the dependency. The assets are rehoused under the META-INF directory and end up with URLs such as:

http://localhost:8080/webjars/swagger-ui/3.43.0/index.html

http4k integrates this functionality into the core library and ships with the webJars() router plugin to activate. As the plugin is just an HttpHandler, the simplest example is just to launch WebJars directly as a Server:

webJars().asServer(SunHttp(8080)).start()

... or a more standard use-case is to mix it into your application routing as in the example below:

Code

package guide.howto.deploy_webjars

import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.routing.webJars
import org.http4k.server.SunHttp
import org.http4k.server.asServer

fun main() {
    // mix the WebJars routing into your app...
    val app = routes(
        "/myGreatRoute" bind GET to { req: Request -> Response(OK) },
        webJars()
    )

    app.asServer(SunHttp(8080)).start()

    // then browse to: http://localhost:8080/webjars/swagger-ui/3.43.0/index.html
}