From 5419e0fb31eef6c0bda265327bf2c234426f20bf Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Fri, 31 May 2024 15:33:03 +0200 Subject: [PATCH] . --- project/src/build.runner.scala | 10 ++++- project/src/routes.scala | 9 +++-- project/test/src/RoutesSpec.scala | 2 +- project/test/src/liveServer.test.scala | 54 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/project/src/build.runner.scala b/project/src/build.runner.scala index f6be406..6de2f60 100644 --- a/project/src/build.runner.scala +++ b/project/src/build.runner.scala @@ -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 diff --git a/project/src/routes.scala b/project/src/routes.scala index b564a58..2d33d9c 100644 --- a/project/src/routes.scala +++ b/project/src/routes.scala @@ -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)) ) @@ -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()) @@ -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 >> diff --git a/project/test/src/RoutesSpec.scala b/project/test/src/RoutesSpec.scala index 9adaed1..a61afc4 100644 --- a/project/test/src/RoutesSpec.scala +++ b/project/test/src/RoutesSpec.scala @@ -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) diff --git a/project/test/src/liveServer.test.scala b/project/test/src/liveServer.test.scala index d7e6d26..c6a9ff5 100644 --- a/project/test/src/liveServer.test.scala +++ b/project/test/src/liveServer.test.scala @@ -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