Skip to content

Commit

Permalink
Add example directory showing sample client/server build and code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Coady committed Apr 2, 2018
1 parent ac49c8b commit 2811300
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
29 changes: 29 additions & 0 deletions example/build.sbt
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions example/client/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions example/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.1.2
2 changes: 2 additions & 0 deletions example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addSbtPlugin("org.lyranthe.fs2-grpc" % "sbt-java-gen" % "0.1.0-SNAPSHOT")

36 changes: 36 additions & 0 deletions example/server/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -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()
}
}
22 changes: 22 additions & 0 deletions example/src/main/protobuf/hello.proto
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 2811300

Please sign in to comment.