Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gRPC server reflection #109

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions grpc/src/main/scala/eu/ostrzyciel/jelly/grpc/RdfStreamServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ object RdfStreamServer:

/**
* Simple implementation of a Pekko gRPC server for streaming Jelly RDF data.
*
* The server implements gRPC server reflection, which allows clients to discover the service. It also supports
* gzip compression only when enabled in the configuration.
*
* This implementation is a good starting point. For your production application, you will most likely need to
* create your own server implementation, using this as a template. For example, here we create a new
* Pekko HTTP server instance, while you may want to integrate this with your existing HTTP server.
*
* @param options options for this server
* @param streamService the service implementing the methods of the API
* @param system actor system
Expand All @@ -52,16 +60,17 @@ final class RdfStreamServer(options: RdfStreamServer.Options, streamService: Rdf
*/
def run(): Future[ServerBinding] =
val service: HttpRequest => Future[HttpResponse] =
RdfStreamServiceHandler(streamService)
// Enable server reflection to allow clients to discover the service
// See: https://grpc.io/docs/guides/reflection/
RdfStreamServiceHandler.withServerReflection(streamService)

val handler: HttpRequest => Future[HttpResponse] = if options.enableGzip then
service
else
{ request =>
// Eternal thanks to: https://github.com/akka/akka-grpc/issues/1265
val withoutEncoding = request.withHeaders(request.headers.filterNot(_.lowercaseName == "grpc-accept-encoding"))
service(withoutEncoding)
}
else { request =>
// Eternal thanks to: https://github.com/akka/akka-grpc/issues/1265
val withoutEncoding = request.withHeaders(request.headers.filterNot(_.lowercaseName == "grpc-accept-encoding"))
service(withoutEncoding)
}

val bound: Future[ServerBinding] = Http()
.newServerAt(options.host, options.port)
Expand Down