Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed May 31, 2024
1 parent b0f9efd commit 5419e0f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
10 changes: 9 additions & 1 deletion project/src/build.runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ def buildRunnerMill(
"-w",
s"$moduleName.fastLinkJS"
) ++ extraBuildArgs
).withWorkingDirectory(workDir).spawn[IO].useForever.map(_ => ()).background.void
).withWorkingDirectory(workDir)
.spawn[IO]
.use {
p =>
// p.stderr.through(fs2.io.stdout).compile.drain >>
p.stdout.through(text.utf8.decode).debug().compile.drain
}
.background
.void

for
_ <- logger.trace("Starting buildRunnerMill").toResource
Expand Down
9 changes: 5 additions & 4 deletions project/src/routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ def routes[F[_]: Files: MonadThrow](
clientRoutingPrefix match
case None => HttpRoutes.empty[IO]
case Some(spaRoute) =>
indexOpts match
val r = indexOpts match
case None =>
Root / spaRoute
StaticHtmlMiddleware(
HttpRoutes.of[IO] {
case GET -> root /: spaRoute /: path =>
// logger.trace(path) >>
case req @ GET -> root /: path =>
IO(
Response[IO]().withEntity(vanillaTemplate(false))
)
Expand Down Expand Up @@ -196,6 +195,8 @@ def routes[F[_]: Files: MonadThrow](
dir / "index.html"
)(logger)

Router(s"/$spaRoute" -> r)

val refreshRoutes = HttpRoutes.of[IO] {
case GET -> Root / "api" / "v1" / "sse" =>
val keepAlive = fs2.Stream.fixedRate[IO](10.seconds).as(KeepAlive())
Expand All @@ -208,9 +209,9 @@ def routes[F[_]: Files: MonadThrow](
val app = logMiddler(
refreshRoutes
.combineK(linkedAppWithCaching)
.combineK(proxyRoutes)
.combineK(clientSpaRoutes)
.combineK(staticAssetRoutes)
.combineK(proxyRoutes)
)

clientRoutingPrefix.fold(IO.unit)(s => logger.trace(s"client spa at : $s")).toResource >>
Expand Down
2 changes: 1 addition & 1 deletion project/test/src/RoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class RoutesSuite extends CatsEffectSuite:
}

val requestHtml = Request[IO](uri = uri"/")
val etag = "699892091"
// val etag = "699892091"

val checkRespHtml = client
.run(requestHtml)
Expand Down
54 changes: 54 additions & 0 deletions project/test/src/liveServer.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,60 @@ trait PlaywrightTest extends munit.FunSuite:
assertEquals(outFail.statusCode, 404)
}

files.test("proxy server and SPA client apps") {
testDir =>
val backendPort = 8090
val thisTestPort = basePort + 3
// use http4s to instantiate a simple server that responds to /api/hello with 200, use Http4sEmberServer
EmberServerBuilder
.default[IO]
.withHttpApp(
HttpRoutes
.of[IO] {
case GET -> Root / "api" / "hello" =>
Ok("hello world")
}
.orNotFound
)
.withPort(Port.fromInt(backendPort).get)
.build
.allocated
.unsafeToFuture()

LiveServer
.run(
List(
"--build-tool",
"scala-cli",
"--project-dir",
testDir.toString,
"--styles-dir",
styleDir(testDir).toString,
"--client-routes-prefix",
"/app",
"--port",
thisTestPort.toString,
"--proxy-target-port",
backendPort.toString,
"--proxy-prefix-path",
"/api"
)
)
.unsafeToFuture()

Thread.sleep(1000) // give the thing time to start.

val out = requests.get(s"http://localhost:$thisTestPort/api/hello", check = false)
assertEquals(out.statusCode, 200)
assertEquals(out.text(), "hello world")

val outFail = requests.get(s"http://localhost:$thisTestPort/api/nope", check = false)
assertEquals(outFail.statusCode, 404)

val canGetHtml = requests.get(s"http://localhost:$thisTestPort", check = false)
assertEquals(canGetHtml.statusCode, 200)
}

files.test("no styles") {
testDir =>
val thisTestPort = basePort + 4
Expand Down

0 comments on commit 5419e0f

Please sign in to comment.