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,27 +1,17 @@
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 {
def appRoutes[F[_]: Effect]( private[this] val suffixes = List(
blocker: Blocker
)(implicit C: ContextShift[F]): HttpRoutes[F] =
webjarService(
WebjarConfig(
filter = assetFilter,
blocker = blocker,
cacheStrategy = NoopCacheStrategy[F]
)
)
def assetFilter(asset: WebjarAsset): Boolean =
List(
".js", ".js",
".css", ".css",
".html", ".html",
@ -36,6 +26,25 @@ object WebjarRoutes {
".ttf", ".ttf",
".yml", ".yml",
".xml" ".xml"
).exists(e => asset.asset.endsWith(e)) )
def appRoutes[F[_]: Effect](
blocker: Blocker
)(implicit CS: ContextShift[F]): HttpRoutes[F] =
Kleisli {
case req if req.method == Method.GET =>
val p = req.pathInfo
if (p.contains("..") || !suffixes.exists(p.endsWith(_)))
OptionT.pure(Response.notFound[F])
else
StaticFile.fromResource(
s"/META-INF/resources/webjars$p",
blocker,
Some(req),
true
)
case _ =>
OptionT.none
}
} }