@@ -13,6 +13,9 @@ import org.http4s.server.staticcontent.fileService
1313import fs2 .io .file .Files
1414
1515import cats .effect .kernel .Async
16+ import cats .syntax .all .*
17+
18+ import scribe .Scribe
1619
1720def appRoute [F [_]: Files ](stringPath : String )(using f : Async [F ]): HttpRoutes [F ] = HttpRoutes .of[F ] {
1821
@@ -33,25 +36,29 @@ def appRoute[F[_]: Files](stringPath: String)(using f: Async[F]): HttpRoutes[F]
3336
3437}
3538
36- def appRouteInMemory [F [_]](lookup : String => Option [Array [Byte ]])(using f : Async [F ]): HttpRoutes [F ] =
39+ def appRouteInMemory [F [_]](lookup : String => Option [Array [Byte ]])(using
40+ f : Async [F ],
41+ logger : Scribe [F ]
42+ ): HttpRoutes [F ] =
3743 def contentTypeFor (ext : String ): `Content-Type` =
3844 MediaType .forExtension(ext).fold(`Content-Type`(MediaType .application.`octet-stream`))(m => `Content-Type`(m))
3945
46+ def serve (req : org.http4s.Request [F ], ext : String ): F [Response [F ]] =
47+ val key = req.uri.path.renderString.stripPrefix(" /" )
48+ lookup(key) match
49+ case Some (bytes) =>
50+ logger.debug(
51+ s " [appRouteInMemory] HIT ext= $ext key=' $key' size= ${bytes.length} bytes "
52+ ) >> f.pure(Response [F ](Status .Ok ).withEntity(bytes).withContentType(contentTypeFor(ext)))
53+ case None =>
54+ logger.debug(
55+ s " [appRouteInMemory] MISS ext= $ext key=' $key' "
56+ ) >> f.pure(Response [F ](Status .NotFound ))
57+
4058 HttpRoutes .of[F ] {
41- case req @ GET -> Root / fName ~ " js" =>
42- lookup(req.uri.path.renderString.stripPrefix(" /" )) match
43- case Some (bytes) => f.pure(Response [F ](Status .Ok ).withEntity(bytes).withContentType(contentTypeFor(" js" )))
44- case None => f.pure(Response [F ](Status .NotFound ))
45-
46- case req @ GET -> Root / fName ~ " wasm" =>
47- lookup(req.uri.path.renderString.stripPrefix(" /" )) match
48- case Some (bytes) => f.pure(Response [F ](Status .Ok ).withEntity(bytes).withContentType(contentTypeFor(" wasm" )))
49- case None => f.pure(Response [F ](Status .NotFound ))
50-
51- case req @ GET -> Root / fName ~ " map" =>
52- lookup(req.uri.path.renderString.stripPrefix(" /" )) match
53- case Some (bytes) => f.pure(Response [F ](Status .Ok ).withEntity(bytes).withContentType(contentTypeFor(" map" )))
54- case None => f.pure(Response [F ](Status .NotFound ))
59+ case req @ GET -> Root / fName ~ " js" => serve(req, " js" )
60+ case req @ GET -> Root / fName ~ " wasm" => serve(req, " wasm" )
61+ case req @ GET -> Root / fName ~ " map" => serve(req, " map" )
5562 }
5663end appRouteInMemory
5764
0 commit comments