https://github.com/ollls/qh2_grpc
https://github.com/ollls/qh2_grpc
grpc quartz-h2 scala
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ollls/qh2_grpc
- Owner: ollls
- Created: 2024-06-28T20:08:43.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T17:41:52.000Z (almost 2 years ago)
- Last Synced: 2025-03-24T05:44:49.371Z (about 1 year ago)
- Topics: grpc, quartz-h2, scala
- Language: Scala
- Homepage:
- Size: 80.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
PoC Quartz-H2 and sbt-fs2-grpc plugin.
We use generated models and Marshallers from sbt-fs2-grpc but to call the actual service methods we use scala 3.3 macro.
Tests with gcpcurl: UnaryToUnary, UnaryToStream, StreamToUnary, StreamToStream
```
grpcurl -v -insecure -proto orders.proto -d '{"name" : "John The Cube Jr", "number":101 }' localhost:8443 com.example.protos.Greeter/SayHello
grpcurl -v -insecure -proto orders.proto -d '{"name" : "John The Cube Jr", "number":101 }' localhost:8443 com.example.protos.Greeter/LotsOfReplies
grpcurl -v -insecure -proto orders.proto -d '{"name" : "MESSAGE1", "number":101 } {"name" : "MESSAGE2", "number":101 }' localhost:8443 com.example.protos.Greeter/LotsOfGreetings
grpcurl -v -insecure -proto orders.proto -d '{"name" : "John The Cube Jr", "number":101 } {"name" : "George The King", "number":101 }' localhost:8443 com.example.protos.Greeter/BidiHello
```
Universal grpc router for quartz with scala 3 macro.
To call service method directly with fs2 streaming with IO context we use
* ServerServiceDefinition
* TraitMethodFinder.getAllMethods[GreeterService] which was done with scala3 macro.
```scala
def run(args: List[String]) = {
val greeterService: Resource[IO, ServerServiceDefinition] =
GreeterFs2Grpc.bindServiceResource[IO](new GreeterService)
val exitCode = greeterService.use { sd =>
for {
_ <- IO(QuartzH2Server.setLoggingLevel(Level.DEBUG))
ctx <- QuartzH2Server.buildSSLContext(
"TLSv1.3",
"keystore.jks",
"password"
)
grpcIO <- IO(
Router[GreeterService](
service,
sd,
TraitMethodFinder.getAllMethods[GreeterService]
).getIO
)
exitCode <- new QuartzH2Server(
"localhost",
8443,
32000,
Some(ctx)
).start(grpcIO, sync = false)
} yield (exitCode)
}
exitCode
}
```
REST Style interraction with grpc clients also possible.
```scala
val R: HttpRouteIO = {
case req @ POST -> Root / "com.example.protos.Greeter" / "SayHello" =>
for {
request <- req.body
io <- service._sayHello(request, null)
} yield (Response
.Ok()
.trailers(
Headers(
"grpc-status" -> "0",
"grpc-message" -> "ok",
"content-type" -> "application/grpc"
)
)
.hdr("content-type" -> "application/grpc"))
.asStream(
Stream.emits(io.toByteArray)
)
}
```