-
Notifications
You must be signed in to change notification settings - Fork 15
/
GrpcServer.scala
38 lines (33 loc) · 1.08 KB
/
GrpcServer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package io.iohk.atala.iris.apiserver.grpc
import io.grpc.{ServerBuilder, ServerServiceDefinition}
import io.grpc.protobuf.services.ProtoReflectionService
import io.iohk.atala.iris.proto.service.IrisServiceGrpc
import zio.*
object GrpcServer {
def start(port: Int, services: Seq[ServerServiceDefinition]): Task[Unit] = {
val managedServer = ZIO.acquireRelease(
for {
_ <- ZIO.logInfo(s"starting grpc server on port $port")
server <- ZIO.attempt {
val builder = ServerBuilder.forPort(port)
services.foreach(s => builder.addService(s))
builder.addService(ProtoReflectionService.newInstance())
builder.build().start()
}
_ <- ZIO.logInfo(s"grpc server listening on port $port")
} yield server
)(server =>
for {
_ <- ZIO.logInfo("stopping grpc server")
_ <- ZIO.attempt(server.shutdown()).orDie
_ <- ZIO.logInfo("grpc server stopped successfully")
} yield ()
)
ZIO.scoped {
for {
_ <- managedServer
_ <- ZIO.never
} yield ()
}
}
}