-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Quafadas/frontendRouting
Might serve spa routes
- Loading branch information
Showing
9 changed files
with
515 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "scala", | ||
"request": "attach", | ||
"name": "Attach debugger", | ||
// name of the module that is being debugging | ||
"buildTarget": "project.test", | ||
// Host of the jvm to connect to | ||
"hostName": "localhost", | ||
// Port to connect to | ||
"port": 5005 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import org.http4s.Header | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Request | ||
import org.typelevel.ci.CIStringSyntax | ||
|
||
import cats.data.Kleisli | ||
import cats.effect.* | ||
import cats.effect.IO | ||
|
||
object NoCacheMiddlware: | ||
|
||
def apply(service: HttpRoutes[IO]): HttpRoutes[IO] = Kleisli { | ||
(req: Request[IO]) => | ||
service(req).map { | ||
resp => | ||
resp.putHeaders( | ||
Header.Raw(ci"Cache-Control", "no-cache") | ||
) | ||
} | ||
} | ||
|
||
end NoCacheMiddlware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
|
||
import org.http4s.Header | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Request | ||
import org.http4s.Response | ||
import org.http4s.Status | ||
import org.http4s.dsl.io.* | ||
import org.typelevel.ci.CIStringSyntax | ||
|
||
import fs2.* | ||
import fs2.io.file.Path | ||
|
||
import scribe.Scribe | ||
|
||
import cats.data.Kleisli | ||
import cats.data.OptionT | ||
import cats.effect.* | ||
import cats.effect.IO | ||
import cats.syntax.all.* | ||
|
||
def parseFromHeader(epochInstant: Instant, header: String): Long = | ||
java.time.Duration.between(epochInstant, ZonedDateTime.parse(header, formatter)).toSeconds() | ||
end parseFromHeader | ||
|
||
object StaticFileMiddleware: | ||
def apply(service: HttpRoutes[IO], file: Path)(logger: Scribe[IO]): HttpRoutes[IO] = Kleisli { | ||
(req: Request[IO]) => | ||
|
||
val epochInstant: Instant = Instant.EPOCH | ||
|
||
cachedFileResponse(epochInstant, file, req, service)(logger: Scribe[IO]) | ||
} | ||
end StaticFileMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
|
||
import org.http4s.Header | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Request | ||
import org.http4s.Response | ||
import org.http4s.Status | ||
import org.http4s.dsl.io.* | ||
import org.typelevel.ci.CIStringSyntax | ||
|
||
import fs2.* | ||
import fs2.io.file.Path | ||
|
||
import scribe.Scribe | ||
|
||
import cats.data.Kleisli | ||
import cats.data.OptionT | ||
import cats.effect.* | ||
import cats.effect.IO | ||
import cats.syntax.all.* | ||
|
||
inline def respondWithCacheLastModified(resp: Response[IO], lastModZdt: ZonedDateTime) = | ||
resp.putHeaders( | ||
Header.Raw(ci"Cache-Control", "no-cache"), | ||
Header.Raw(ci"ETag", lastModZdt.toInstant.getEpochSecond.toString()), | ||
Header.Raw( | ||
ci"Last-Modified", | ||
formatter.format(lastModZdt) | ||
), | ||
Header.Raw( | ||
ci"Expires", | ||
httpCacheFormat(ZonedDateTime.ofInstant(Instant.now().plusSeconds(10000000), ZoneId.of("GMT"))) | ||
) | ||
) | ||
end respondWithCacheLastModified | ||
|
||
inline def cachedFileResponse(epochInstant: Instant, fullPath: Path, req: Request[IO], service: HttpRoutes[IO])( | ||
logger: Scribe[IO] | ||
) = | ||
OptionT | ||
.liftF(fileLastModified(fullPath)) | ||
.flatMap { | ||
lastmod => | ||
req.headers.get(ci"If-Modified-Since") match | ||
case Some(header) => | ||
val browserLastModifiedAt = header.head.value | ||
service(req).semiflatMap { | ||
resp => | ||
val zdt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(lastmod), ZoneId.of("GMT")) | ||
val response = | ||
if parseFromHeader(epochInstant, browserLastModifiedAt) == lastmod then | ||
logger.debug("Time matches, returning 304") >> | ||
IO( | ||
respondWithCacheLastModified(Response[IO](Status.NotModified), zdt) | ||
) | ||
else | ||
logger.debug(lastmod.toString()) >> | ||
logger.debug("Last modified doesn't match, returning 200") >> | ||
IO( | ||
respondWithCacheLastModified(resp, zdt) | ||
) | ||
end if | ||
end response | ||
logger.debug(lastmod.toString()) >> | ||
logger.debug(parseFromHeader(epochInstant, browserLastModifiedAt).toString()) >> | ||
response | ||
} | ||
case _ => | ||
OptionT.liftF(logger.debug("No headers in query, service it")) >> | ||
service(req).map { | ||
resp => | ||
respondWithCacheLastModified( | ||
resp, | ||
ZonedDateTime.ofInstant(Instant.ofEpochSecond(lastmod), ZoneId.of("GMT")) | ||
) | ||
} | ||
|
||
end match | ||
} | ||
|
||
object StaticMiddleware: | ||
|
||
def apply(service: HttpRoutes[IO], staticDir: Path)(logger: Scribe[IO]): HttpRoutes[IO] = Kleisli { | ||
(req: Request[IO]) => | ||
val epochInstant: Instant = Instant.EPOCH | ||
val fullPath = staticDir / req.uri.path.toString.drop(1) | ||
|
||
cachedFileResponse(epochInstant, fullPath, req, service)(logger: Scribe[IO]) | ||
} | ||
end StaticMiddleware |
Oops, something went wrong.