@@ -19,39 +19,42 @@ import cats.syntax.all.*
1919
2020object ETagMiddleware :
2121
22+ private def respondWithEtag (mr : Ref [IO , Map [String , String ]], req : Request [IO ], resp : Response [IO ])(
23+ logger : Scribe [IO ]
24+ ) =
25+ mr.get
26+ .flatMap {
27+ map =>
28+ // logger.trace(s"Responding with ETag at path: ${req.uri.path}") >>
29+ map.get(req.uri.path.toString.drop(1 )) match
30+ case Some (hash) =>
31+ logger.debug(s " Found ETag: $hash in map for ${req.uri.path}" ) >>
32+ IO (
33+ resp.putHeaders(
34+ Header .Raw (ci " ETag " , hash),
35+ Header .Raw (ci " Cache-control " , " Must-Revalidate" ),
36+ Header .Raw (ci " Cache-control " , " No-cache" ),
37+ Header .Raw (ci " Cache-control " , " max-age=0" ),
38+ Header .Raw (ci " Cache-control " , " public" )
39+ )
40+ )
41+ case None =>
42+ logger.debug(" No hash found in map at path :" + req.uri.toString) >>
43+ IO (
44+ resp.putHeaders(
45+ Header .Raw (ci " Cache-control " , " Must-Revalidate" ),
46+ Header .Raw (ci " Cache-control " , " No-cache" ),
47+ Header .Raw (ci " Cache-control " , " max-age=0" ),
48+ Header .Raw (ci " Cache-control " , " public" )
49+ )
50+ )
51+ end match
52+ }
53+ end respondWithEtag
54+
2255 def apply (service : HttpRoutes [IO ], mr : Ref [IO , Map [String , String ]])(logger : Scribe [IO ]): HttpRoutes [IO ] = Kleisli {
2356 (req : Request [IO ]) =>
2457
25- def respondWithEtag (resp : Response [IO ]) =
26- mr.get
27- .flatMap {
28- map =>
29- map.get(req.uri.path.toString.drop(1 )) match
30- case Some (hash) =>
31- logger.debug(req.uri.toString) >>
32- IO (
33- resp.putHeaders(
34- Header .Raw (ci " ETag " , hash),
35- Header .Raw (ci " Cache-control " , " Must-Revalidate" ),
36- Header .Raw (ci " Cache-control " , " No-cache" ),
37- Header .Raw (ci " Cache-control " , " max-age=0" ),
38- Header .Raw (ci " Cache-control " , " public" )
39- )
40- )
41- case None =>
42- logger.debug(req.uri.toString) >>
43- IO (
44- resp.putHeaders(
45- Header .Raw (ci " Cache-control " , " Must-Revalidate" ),
46- Header .Raw (ci " Cache-control " , " No-cache" ),
47- Header .Raw (ci " Cache-control " , " max-age=0" ),
48- Header .Raw (ci " Cache-control " , " public" )
49- )
50- )
51- end match
52- }
53- end respondWithEtag
54-
5558 req.headers.get(ci " If-None-Match " ) match
5659 case Some (header) =>
5760 val etag = header.head.value
@@ -65,23 +68,24 @@ object ETagMiddleware:
6568 map.get(req.uri.path.toString.drop(1 )) match
6669 case Some (foundEt) =>
6770 if etag == foundEt then
68- logger.debug(" ETag matches , returning 304" ) >>
71+ logger.debug(s " ETag $etag found in cache at path ${req.uri.path} , returning 304 " ) >>
6972 IO (Response [IO ](Status .NotModified ))
7073 else
71- logger.debug(etag) >>
72- logger.debug(" ETag doesn't match, returning 200" ) >>
73- respondWithEtag(resp)
74+ logger.debug(s " $etag not found in cache at path ${req.uri.path} returning 200 " ) >>
75+ respondWithEtag(mr, req, resp)(logger)
7476 end if
7577 case None =>
76- respondWithEtag(resp)
78+ logger.debug(s " No path found in cache at path ${req.uri.path}" ) >>
79+ respondWithEtag(mr, req, resp)(logger)
7780 }
7881 }
7982 case _ =>
80- OptionT .liftF(logger.debug(" No ETag header in query, service it" )) >>
83+ OptionT .liftF(logger.debug(" No If-None-Match ETag header in request" )) >>
84+ OptionT .liftF(logger.debug(s " Headers are : ${req.headers.headers.mkString(" , " )} at ${req.uri.path}" )) >>
8185 service(req).semiflatMap {
8286 resp =>
83- logger.trace (resp.toString) >>
84- respondWithEtag(resp)
87+ logger.debug (resp.toString) >>
88+ respondWithEtag(mr, req, resp)(logger )
8589 }
8690 end match
8791 }
0 commit comments