From eb00d6c1351946f19d839501e514240d7a9b2e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 7 Nov 2023 21:36:57 +0100 Subject: [PATCH 1/4] Fix base URL --- build.sbt | 2 +- .../src/main/scala/smithy4s_codegen/Main.scala | 9 ++++++++- .../main/scala/smithy4s_codegen/ApiBuilder.scala | 13 ++++++++++++- .../src/main/scala/smithy4s_codegen/Main.scala | 1 - .../smithy4s_codegen/components/pages/Home.scala | 5 ++--- modules/frontend/vite.config.js | 8 ++++++++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/build.sbt b/build.sbt index e5fc8bf..7e83423 100644 --- a/build.sbt +++ b/build.sbt @@ -45,7 +45,7 @@ lazy val frontend = (project in file("modules/frontend")) "org.http4s" %%% "http4s-client" % "0.23.16" ), baseUri := { - if (insideCI.value) "" else "http://localhost:9000" + if (insideCI.value) "" else "/api" }, buildInfoKeys := Seq[BuildInfoKey](baseUri), buildInfoPackage := "smithy4s_codegen", diff --git a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala index 65bd30b..4190147 100644 --- a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala +++ b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala @@ -18,6 +18,7 @@ import java.io.File import java.nio.file.Files import java.nio.file.Paths import scala.concurrent.duration._ +import org.http4s.server.middleware.Logger class SmithyCodeGenerationServiceImpl(generator: Smithy4s, validator: Validate) extends SmithyCodeGenerationService[IO] { @@ -99,7 +100,13 @@ object Main extends IOApp.Simple { .default[IO] .withPort(thePort) .withHost(theHost) - .withHttpApp(routes.orNotFound) + .withHttpApp( + Logger.httpApp( + logHeaders = true, + logBody = true, + logAction = Some(IO.println(_: String)) + )(routes.orNotFound) + ) .withShutdownTimeout(5.seconds) .build _ <- Resource.eval(IO.println(s"Server started on: $theHost:$thePort")) diff --git a/modules/frontend/src/main/scala/smithy4s_codegen/ApiBuilder.scala b/modules/frontend/src/main/scala/smithy4s_codegen/ApiBuilder.scala index 125450e..2c783a2 100644 --- a/modules/frontend/src/main/scala/smithy4s_codegen/ApiBuilder.scala +++ b/modules/frontend/src/main/scala/smithy4s_codegen/ApiBuilder.scala @@ -11,6 +11,8 @@ import org.http4s.Uri import smithy4s_codegen.BuildInfo.baseUri import smithy4s.kinds.PolyFunction import cats.effect.std.Dispatcher +import org.http4s.client.Client +import util.chaining._ object ApiBuilder { def build: Resource[IO, SmithyCodeGenerationService[EventStream]] = @@ -19,7 +21,10 @@ object ApiBuilder { ec <- IO.executionContext.toResource client <- SimpleRestJsonBuilder(SmithyCodeGenerationService) - .client(FetchClientBuilder[IO].create) + .client( + FetchClientBuilder[IO].create + .pipe(resetBaseUri) + ) .uri(Uri.unsafeFromString(baseUri)) .resource } yield { @@ -30,4 +35,10 @@ object ApiBuilder { } } + + private def resetBaseUri(c: Client[IO]): Client[IO] = Client[IO] { req => + val amendedUri = req.uri.copy(scheme = None, authority = None) + val amendedRequest = req.withUri(amendedUri) + c.run(amendedRequest) + } } diff --git a/modules/frontend/src/main/scala/smithy4s_codegen/Main.scala b/modules/frontend/src/main/scala/smithy4s_codegen/Main.scala index 6a28968..77060fd 100644 --- a/modules/frontend/src/main/scala/smithy4s_codegen/Main.scala +++ b/modules/frontend/src/main/scala/smithy4s_codegen/Main.scala @@ -10,7 +10,6 @@ import org.http4s.client.Client import org.http4s.dom.FetchClientBuilder import org.scalajs.dom import smithy4s.http4s.SimpleRestJsonBuilder -import smithy4s_codegen.BuildInfo.baseUri import smithy4s_codegen.api.SmithyCodeGenerationService import smithy4s_codegen.components.pages.Home diff --git a/modules/frontend/src/main/scala/smithy4s_codegen/components/pages/Home.scala b/modules/frontend/src/main/scala/smithy4s_codegen/components/pages/Home.scala index c37cf22..e001611 100644 --- a/modules/frontend/src/main/scala/smithy4s_codegen/components/pages/Home.scala +++ b/modules/frontend/src/main/scala/smithy4s_codegen/components/pages/Home.scala @@ -2,7 +2,6 @@ package smithy4s_codegen.components.pages import com.raquo.laminar.api.L._ import org.scalajs.dom.ext.Ajax.InputData -import smithy4s_codegen.BuildInfo.baseUri import smithy4s_codegen.api.Smithy4sConvertInput import smithy4s_codegen.api.Smithy4sConvertOutput import smithy4s_codegen.api.SmithyCodeGenerationService @@ -29,7 +28,7 @@ object Home { .recover { case InvalidSmithyContent(errors) => Some(CodeEditor.ValidationResult.Failed(errors)) - case ex: Throwable => + case ex => Some(CodeEditor.ValidationResult.UnknownFailure(ex)) } } @@ -45,7 +44,7 @@ object Home { .map(r => CodeEditor.Smithy4sConversionResult.Success(r.generated) ) - .recover { case ex: Throwable => + .recover { ex => Some(CodeEditor.Smithy4sConversionResult.UnknownFailure(ex)) } } diff --git a/modules/frontend/vite.config.js b/modules/frontend/vite.config.js index 7bbfcf6..2edc86b 100644 --- a/modules/frontend/vite.config.js +++ b/modules/frontend/vite.config.js @@ -8,4 +8,12 @@ export default defineConfig({ projectID: "frontend", }), ], + server: { + proxy: { + "/api": { + target: "http://localhost:9000", + rewrite: (path) => path.replace(/^\/api/, ""), + }, + }, + }, }); From dc5b6bebaad3fde1508d8f200dc44fe038160735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 7 Nov 2023 21:38:34 +0100 Subject: [PATCH 2/4] revert logger --- .../backend/src/main/scala/smithy4s_codegen/Main.scala | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala index 4190147..65bd30b 100644 --- a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala +++ b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala @@ -18,7 +18,6 @@ import java.io.File import java.nio.file.Files import java.nio.file.Paths import scala.concurrent.duration._ -import org.http4s.server.middleware.Logger class SmithyCodeGenerationServiceImpl(generator: Smithy4s, validator: Validate) extends SmithyCodeGenerationService[IO] { @@ -100,13 +99,7 @@ object Main extends IOApp.Simple { .default[IO] .withPort(thePort) .withHost(theHost) - .withHttpApp( - Logger.httpApp( - logHeaders = true, - logBody = true, - logAction = Some(IO.println(_: String)) - )(routes.orNotFound) - ) + .withHttpApp(routes.orNotFound) .withShutdownTimeout(5.seconds) .build _ <- Resource.eval(IO.println(s"Server started on: $theHost:$thePort")) From aa37162663230e985ec0407f289484c52aa7314e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 7 Nov 2023 21:40:32 +0100 Subject: [PATCH 3/4] Remove CORS --- modules/backend/src/main/scala/smithy4s_codegen/Main.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala index 65bd30b..0f3a264 100644 --- a/modules/backend/src/main/scala/smithy4s_codegen/Main.scala +++ b/modules/backend/src/main/scala/smithy4s_codegen/Main.scala @@ -46,7 +46,6 @@ class SmithyCodeGenerationServiceImpl(generator: Smithy4s, validator: Validate) } object Routes { - import org.http4s.server.middleware.CORS def exampleRoute(localJars: List[File]): Resource[IO, HttpRoutes[IO]] = Resource .eval(ModelLoader(localJars)) @@ -55,7 +54,6 @@ object Routes { SimpleRestJsonBuilder .routes(new SmithyCodeGenerationServiceImpl(generator, validator)) .resource - .map(CORS(_)) } private val docs: HttpRoutes[IO] = From de1e4e4de49836739ce6b87f87a87ec9ff05f871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 7 Nov 2023 21:41:15 +0100 Subject: [PATCH 4/4] Document proxy setup --- build.sbt | 5 ++++- modules/frontend/vite.config.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 7e83423..cd8aebd 100644 --- a/build.sbt +++ b/build.sbt @@ -45,7 +45,10 @@ lazy val frontend = (project in file("modules/frontend")) "org.http4s" %%% "http4s-client" % "0.23.16" ), baseUri := { - if (insideCI.value) "" else "/api" + if (insideCI.value) "" + else + // Vite will proxy this to the backend. See vite.config.js + "/api" }, buildInfoKeys := Seq[BuildInfoKey](baseUri), buildInfoPackage := "smithy4s_codegen", diff --git a/modules/frontend/vite.config.js b/modules/frontend/vite.config.js index 2edc86b..51f913b 100644 --- a/modules/frontend/vite.config.js +++ b/modules/frontend/vite.config.js @@ -10,6 +10,7 @@ export default defineConfig({ ], server: { proxy: { + // For requests to /api/**, drop the prefix and proxy the rest to the backend. "/api": { target: "http://localhost:9000", rewrite: (path) => path.replace(/^\/api/, ""),