diff --git a/example/build.sbt b/example/build.sbt new file mode 100644 index 00000000..3c2501f1 --- /dev/null +++ b/example/build.sbt @@ -0,0 +1,29 @@ +lazy val protobuf = + project + .in(file(".")) + .settings( + PB.targets in Compile := List( + scalapb.gen() -> (sourceManaged in Compile).value, + fs2CodeGenerator -> (sourceManaged in Compile).value + ) + ) + +lazy val client = + project + .in(file("client")) + .settings( + libraryDependencies ++= List( + "io.grpc" % "grpc-netty" % "1.11.0" + ) + ) + .dependsOn(protobuf) + +lazy val server = + project + .in(file("server")) + .settings( + libraryDependencies ++= List( + "io.grpc" % "grpc-netty" % "1.11.0" + ) + ) + .dependsOn(protobuf) diff --git a/example/client/src/main/scala/Main.scala b/example/client/src/main/scala/Main.scala new file mode 100644 index 00000000..5ec40991 --- /dev/null +++ b/example/client/src/main/scala/Main.scala @@ -0,0 +1,34 @@ +import cats.effect.IO +import com.example.protos.hello._ +import fs2._ +import io.grpc._ + +import scala.concurrent.ExecutionContext.Implicits.global + +object Main extends StreamApp[IO] { + val managedChannelStream: Stream[IO, ManagedChannel] = + Stream.bracket( + IO( + ManagedChannelBuilder + .forAddress("127.0.0.1", 9999) + .usePlaintext() + .build()))(Stream.emit[ManagedChannel], + (channel: ManagedChannel) => IO(channel.shutdown())) + + def runProgram(helloStub: GreeterFs2Grpc[IO]): IO[Unit] = { + for { + response <- helloStub.sayHello(HelloRequest("John Doe"), new Metadata()) + _ <- IO(println(response.message)) + } yield () + } + + override def stream( + args: List[String], + requestShutdown: IO[Unit]): fs2.Stream[IO, StreamApp.ExitCode] = { + for { + managedChannel <- managedChannelStream + helloStub = GreeterFs2Grpc.stub[IO](managedChannel) + _ <- Stream.eval(runProgram(helloStub)) + } yield StreamApp.ExitCode.Success + } +} diff --git a/example/project/build.properties b/example/project/build.properties new file mode 100644 index 00000000..05313438 --- /dev/null +++ b/example/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.1.2 diff --git a/example/project/plugins.sbt b/example/project/plugins.sbt new file mode 100644 index 00000000..516ef409 --- /dev/null +++ b/example/project/plugins.sbt @@ -0,0 +1,2 @@ +addSbtPlugin("org.lyranthe.fs2-grpc" % "sbt-java-gen" % "0.1.0-SNAPSHOT") + diff --git a/example/server/src/main/scala/Main.scala b/example/server/src/main/scala/Main.scala new file mode 100644 index 00000000..fa5a36e0 --- /dev/null +++ b/example/server/src/main/scala/Main.scala @@ -0,0 +1,36 @@ +import cats.effect.IO +import com.example.protos.hello._ +import fs2._ +import io.grpc._ + +import scala.concurrent.ExecutionContext.Implicits.global + +class ExampleImplementation extends GreeterFs2Grpc[IO] { + override def sayHello(request: HelloRequest, + clientHeaders: Metadata): IO[HelloReply] = { + IO(HelloReply("Request name is: " + request.name)) + } + + override def sayHelloStream( + request: Stream[IO, HelloRequest], + clientHeaders: Metadata): Stream[IO, HelloReply] = { + request.evalMap(req => sayHello(req, clientHeaders)) + } +} + +object Main { + val helloService: ServerServiceDefinition = + GreeterFs2Grpc.bindService(new ExampleImplementation) + val server: Server = + ServerBuilder.forPort(9999).addService(helloService).build() + + val serverStream: Stream[IO, Server] = + Stream.bracket(IO(server.start()))( + Stream.emit[Server], + (server: Server) => IO(server.shutdown())) + + def main(args: Array[String]): Unit = { + server.start() + server.awaitTermination() + } +} diff --git a/example/src/main/protobuf/hello.proto b/example/src/main/protobuf/hello.proto new file mode 100644 index 00000000..e358c252 --- /dev/null +++ b/example/src/main/protobuf/hello.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +package com.example.protos; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply) {} + + rpc SayHelloStream (stream HelloRequest) returns (stream HelloReply) {} +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings +message HelloReply { + string message = 1; +} +