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

Support configurable paths prefixes for Connect routes #40

Merged
merged 1 commit into from
Dec 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cats.effect.{Async, Resource}
import cats.implicits.*
import io.grpc.{ManagedChannelBuilder, ServerBuilder, ServerServiceDefinition}
import org.http4s.dsl.Http4sDsl
import org.http4s.{HttpApp, HttpRoutes, Method}
import org.http4s.{HttpApp, HttpRoutes, Method, Uri}
import org.ivovk.connect_rpc_scala.grpc.*
import org.ivovk.connect_rpc_scala.http.*
import org.ivovk.connect_rpc_scala.http.QueryParams.*
Expand Down Expand Up @@ -35,6 +35,7 @@ case class ConnectRouteBuilder[F[_] : Async] private(
jsonPrinterConfigurator: Endo[Printer] = identity,
serverConfigurator: Endo[ServerBuilder[_]] = identity,
channelConfigurator: Endo[ManagedChannelBuilder[_]] = identity,
pathPrefix: Uri.Path = Uri.Path.Root,
executor: Executor = ExecutionContext.global,
waitForShutdown: Duration = 5.seconds,
treatTrailersAsHeaders: Boolean = true,
Expand All @@ -51,6 +52,9 @@ case class ConnectRouteBuilder[F[_] : Async] private(
def withChannelConfigurator(method: Endo[ManagedChannelBuilder[_]]): ConnectRouteBuilder[F] =
copy(channelConfigurator = method)

def withPathPrefix(path: Uri.Path): ConnectRouteBuilder[F] =
copy(pathPrefix = path)

def withExecutor(executor: Executor): ConnectRouteBuilder[F] =
copy(executor = executor)

Expand Down Expand Up @@ -106,12 +110,12 @@ case class ConnectRouteBuilder[F[_] : Async] private(
)

HttpRoutes.of[F] {
case [email protected] -> Root / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) =>
case [email protected] -> pathPrefix / serviceName / methodName :? EncodingQP(contentType) +& MessageQP(message) =>
val grpcMethod = MethodName(serviceName, methodName)
val entity = RequestEntity[F](message, req.headers)

handler.handle(Method.GET, contentType.some, entity, grpcMethod)
case [email protected] -> Root / serviceName / methodName =>
case [email protected] -> pathPrefix / serviceName / methodName =>
val grpcMethod = MethodName(serviceName, methodName)
val contentType = req.contentType.map(_.mediaType)
val entity = RequestEntity[F](req)
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/scala/org/ivovk/connect_rpc_scala/HttpTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,30 @@ class HttpTest extends AnyFunSuite, Matchers {
.unsafeRunSync()
}

test("support path prefixes") {
val service = TestService.bindService(TestServiceImpl, ExecutionContext.global)

ConnectRouteBuilder.forService[IO](service)
.withPathPrefix(Root / "connect")
.build
.flatMap { app =>
val client = Client.fromHttpApp(app)

client.run(
Request[IO](Method.POST, uri"/connect/org.ivovk.connect_rpc_scala.test.TestService/Add")
.withEntity(""" { "a": 1, "b": 2} """)
)
}
.use { response =>
for {
body <- response.as[String]
status <- response.status.pure[IO]
} yield {
assert(body == """{"sum":3}""")
assert(status == Status.Ok)
}
}
.unsafeRunSync()
}

}
Loading