-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example directory showing sample client/server build and code.
- Loading branch information
Gary Coady
committed
Apr 2, 2018
1 parent
ac49c8b
commit 2811300
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|