Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix base URL #13

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions modules/backend/src/main/scala/smithy4s_codegen/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -55,7 +54,6 @@ object Routes {
SimpleRestJsonBuilder
.routes(new SmithyCodeGenerationServiceImpl(generator, validator))
.resource
.map(CORS(_))
}

private val docs: HttpRoutes[IO] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]] =
Expand All @@ -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 {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
Expand All @@ -45,7 +44,7 @@ object Home {
.map(r =>
CodeEditor.Smithy4sConversionResult.Success(r.generated)
)
.recover { case ex: Throwable =>
.recover { ex =>
Some(CodeEditor.Smithy4sConversionResult.UnknownFailure(ex))
}
}
Expand Down
9 changes: 9 additions & 0 deletions modules/frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/, ""),
},
},
},
});