Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

GRPC Server

Matt Anger edited this page Mar 21, 2022 · 2 revisions

A barebones example of how to use AG Server can be found in the example folder of our repo here.

Basics

A basic main function for grpc servers will look something like this:

fun main() {
    val injector = App.createInjector(
       YourModule(),
       GrpcModule(),
    )
    val server = App.getServer(injector)
    server.runBlocking()
}

So very similar to the HTTP Server example in our other wiki page, we just add the GrpcModule to include our GRPC components. Out of the box a few things are provided for you:

  • Prometheus metrics interceptor
  • Default grpc health service
  • Proto reflection service, to make it easier to use things like BloomRPC to test your server

To inject your GRPC Services you code for YourModule should look something like this:

class YourModule : AbstractModule() {
    @ProvidesIntoSet
    fun getMyHandler(): ServerServiceDefinition {
        return MyGrpcService().bindService()
    }
}

Similar to the HttpHandler definitions we provide them into a set, and the server will automatically find and add them.

Interceptors

The AG Server also supports GRPC Interceptors. Similar to the HTTP Decorators you provide lists into a set. The AG Server will order them appropriately so the first interceptor will be the outermost of the list you provide.

class YourModule : AbstractModule() {
    @ProvidesIntoSet
    fun getInterceptors(): List< ServerInterceptor> {
        return listOf(
          MyInterceptor(),
        )
    }
}

For interceptors that only apply to select services, you should do that at their injection time. For example:

class YourModule : AbstractModule() {
    @Inject
    @ProvidesIntoSet
    fun getMyHandler(
      myInterceptor: ServerInterceptor,
      myInterceptor2: ServerInterceptor,
    ): ServerServiceDefinition {
        return ServerInterceptors.intercept(MyGrpcService().bindService(), listOf(myInterceptor, myInterceptor2))
    }
}

Clone this wiki locally