Skip to content

Instantly share code, notes, and snippets.

@elyphas
Created October 16, 2024 19:47
Show Gist options
  • Save elyphas/07c17fd24a7b5c89c3d162c2488b667e to your computer and use it in GitHub Desktop.
Save elyphas/07c17fd24a7b5c89c3d162c2488b667e to your computer and use it in GitHub Desktop.
package server_routes
import cats.effect.IO
import org.http4s.StaticFile
import org.http4s.HttpRoutes
import org.http4s.Method.GET
import org.http4s.dsl.io._
object StaticFilesRoutes {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
val route = HttpRoutes.of[IO] {
case request @ GET -> Root =>
StaticFile
.fromResource[IO]("index.html", Some(request)).getOrElseF(NotFound())
case request @ GET -> Root / "assets" / "javascript" / path if staticFileAllowed(path) =>
StaticFile
.fromResource("public/javascript/" + path, Some(request))
.getOrElseF(NotFound())
case request@GET -> Root / "assets" / path if staticFileAllowed(path) =>
StaticFile
.fromResource("public/" + path, Some(request))
.getOrElseF(NotFound())
case request@GET -> Root / "assets" / "stylesheets" / path if staticFileAllowed(path) =>
StaticFile
.fromResource("public/stylesheets/" + path, Some(request))
.getOrElseF(NotFound())
}
private def staticFileAllowed(path: String) = List(".gif", ".js", ".css", ".map", ".html", ".webm").exists(path.endsWith)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment