Serve static files/assets preferring the gzip version

This commit is contained in:
Eike Kettner 2020-09-03 01:28:14 +02:00
parent 3b8500245f
commit 7a0f71604d

View File

@ -1,41 +1,50 @@
package docspell.restserver.webapp package docspell.restserver.webapp
import cats.data.Kleisli
import cats.data.OptionT
import cats.effect._ import cats.effect._
import org.http4s.HttpRoutes import org.http4s.HttpRoutes
import org.http4s.server.staticcontent.NoopCacheStrategy import org.http4s.Method
import org.http4s.server.staticcontent.WebjarService.{Config => WebjarConfig, WebjarAsset} import org.http4s.Response
import org.http4s.server.staticcontent.webjarService import org.http4s.StaticFile
object WebjarRoutes { object WebjarRoutes {
private[this] val suffixes = List(
".js",
".css",
".html",
".json",
".jpg",
".png",
".eot",
".woff",
".woff2",
".svg",
".otf",
".ttf",
".yml",
".xml"
)
def appRoutes[F[_]: Effect]( def appRoutes[F[_]: Effect](
blocker: Blocker blocker: Blocker
)(implicit C: ContextShift[F]): HttpRoutes[F] = )(implicit CS: ContextShift[F]): HttpRoutes[F] =
webjarService( Kleisli {
WebjarConfig( case req if req.method == Method.GET =>
filter = assetFilter, val p = req.pathInfo
blocker = blocker, if (p.contains("..") || !suffixes.exists(p.endsWith(_)))
cacheStrategy = NoopCacheStrategy[F] OptionT.pure(Response.notFound[F])
) else
) StaticFile.fromResource(
s"/META-INF/resources/webjars$p",
def assetFilter(asset: WebjarAsset): Boolean = blocker,
List( Some(req),
".js", true
".css", )
".html", case _ =>
".json", OptionT.none
".jpg", }
".png",
".eot",
".woff",
".woff2",
".svg",
".otf",
".ttf",
".yml",
".xml"
).exists(e => asset.asset.endsWith(e))
} }