diff --git a/build.sbt b/build.sbt index fb4dfb4..612ade4 100644 --- a/build.sbt +++ b/build.sbt @@ -27,7 +27,10 @@ ThisBuild / mergifySuccessConditions += MergifyCondition.Custom("#approved-revie lazy val log4catsVersion = "2.7.0" -lazy val root = tlCrossRootProject.aggregate(`http4s-consul-middleware`) +lazy val root = tlCrossRootProject.aggregate( + `http4s-consul-middleware`, + `smithy4s-consul-middleware`, +) lazy val `http4s-consul-middleware` = crossProject(JSPlatform, JVMPlatform) .crossType(CrossType.Full) @@ -79,3 +82,20 @@ lazy val `http4s-consul-middleware` = crossProject(JSPlatform, JVMPlatform) "io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.6.0" % Test, ) ) + +lazy val `smithy4s-consul-middleware` = crossProject(JSPlatform, JVMPlatform) + .crossType(CrossType.Pure) + .in(file("smithy4s")) + .settings( + description := "smithy4s middleware to rewrite URLs back to the consul://{service} format expected by http4s-consul-middleware", + tpolecatScalacOptions += ScalacOptions.release("8"), + tlVersionIntroduced := Map("3" -> "0.3.2", "2.12" -> "0.3.2", "2.13" -> "0.3.2"), + libraryDependencies ++= { + val http4sVersion = "0.23.27" + + Seq( + "org.http4s" %%% "http4s-client" % http4sVersion, + "com.disneystreaming.smithy4s" %% "smithy4s-core" % "0.18.23", + ) + }, + ) diff --git a/smithy4s/src/main/scala/com/dwolla/consul/smithy4s/ReconsulifyMiddleware.scala b/smithy4s/src/main/scala/com/dwolla/consul/smithy4s/ReconsulifyMiddleware.scala new file mode 100644 index 0000000..317c791 --- /dev/null +++ b/smithy4s/src/main/scala/com/dwolla/consul/smithy4s/ReconsulifyMiddleware.scala @@ -0,0 +1,18 @@ +package com.dwolla.consul.smithy4s + +import cats.effect.MonadCancelThrow +import org.http4s.client.Client +import org.http4s.syntax.all._ +import smithy4s.{Endpoint, Service} + +class ReconsulifyMiddleware[F[_] : MonadCancelThrow] extends Endpoint.Middleware[Client[F]] { + override def prepare[Alg[_[_, _, _, _, _]]](service: Service[Alg]) + (endpoint: service.Endpoint[_, _, _, _, _]): Client[F] => Client[F] = + underlying => Client { req => + underlying.run(req.withUri(req.uri.copy(scheme = Option(scheme"consul")))) + } +} + +object ReconsulifyMiddleware { + def apply[F[_] : MonadCancelThrow]: Endpoint.Middleware[Client[F]] = new ReconsulifyMiddleware[F] +}