diff --git a/build.sbt b/build.sbt index e5fc8bf..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 "http://localhost:9000" + 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/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] = 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..51f913b 100644 --- a/modules/frontend/vite.config.js +++ b/modules/frontend/vite.config.js @@ -8,4 +8,13 @@ export default defineConfig({ projectID: "frontend", }), ], + 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/, ""), + }, + }, + }, });