On static data and HTTP

Usually, if you fetch a file via HTTP, it ships with an ETag. An ETag specifies a file version. So the next time the browser requests that information, it sends the ETag along, and gets a 304 Not Modified back, so you don’t have to send the data over the wire again.

All of these solutions are most likely slower than the equivalent in nginx or a similar static file hoster, but they’re often fast enough.

Serving static files

Http4s provides a few helpers to handle ETags for you, they’re located in StaticFile.

import org.http4s._
// import org.http4s._

import org.http4s.dsl._
// import org.http4s.dsl._

import java.io.File
// import java.io.File

import fs2.Task
// import fs2.Task

import fs2.interop.cats._
// import fs2.interop.cats._

val service = HttpService {
  case request @ GET -> Root / "index.html" =>
    StaticFile.fromFile(new File("relative/path/to/index.html"), Some(request))
      .getOrElseF(NotFound()) // In case the file doesn't exist
}
// service: org.http4s.HttpService = Kleisli(org.http4s.package$HttpService$$$Lambda$32253/284928006@623718ac)

Serving from jars

For simple file serving, it’s possible to package resources with the jar and deliver them from there. Append to the List as needed.

def static(file: String, request: Request) =
  StaticFile.fromResource("/" + file, Some(request)).getOrElseF(NotFound())
// static: (file: String, request: org.http4s.Request)fs2.Task[org.http4s.Response]

val service = HttpService {
  case request @ GET -> Root / path if List(".js", ".css", ".map", ".html", ".webm").exists(path.endsWith) =>
    static(path, request)
}
// service: org.http4s.HttpService = Kleisli(org.http4s.package$HttpService$$$Lambda$32253/284928006@37ac6ab8)

Webjars

A special service exists to load files from WebJars. Add your WebJar to the class path, as you usually would:

libraryDependencies ++= Seq(
  "org.webjars" % "jquery" % "3.1.1-1"
)
// <console>:24: error: not found: value libraryDependencies
//        libraryDependencies ++= Seq(
//        ^
// <console>:25: error: value % is not a member of String
//          "org.webjars" % "jquery" % "3.1.1-1"
//                        ^

Then, mount the WebjarService like any other service:

import org.http4s.server.staticcontent.webjarService
// import org.http4s.server.staticcontent.webjarService

import org.http4s.server.staticcontent.WebjarService.{WebjarAsset, Config}
// import org.http4s.server.staticcontent.WebjarService.{WebjarAsset, Config}

// only allow js assets
def isJsAsset(asset: WebjarAsset): Boolean =
  asset.asset.endsWith(".js")
// isJsAsset: (asset: org.http4s.server.staticcontent.WebjarService.WebjarAsset)Boolean

val webjars: HttpService = webjarService(
  Config(
    filter = isJsAsset
  )
)
// webjars: org.http4s.HttpService = Kleisli(org.http4s.server.staticcontent.WebjarService$$$Lambda$32259/337390444@46db74cf)