Search code examples
scalagrpclagomakka-grpc

How to create a lagom server with grpc service implementation only?


I am trying to create a grpc service using Lagom framework following this documentation. Here the hello service also exposes rest API besides grpc service.

Now while creating lagom server in the ApplicationLoader we assign grpc service impl as additionalRouter like so:

abstract class HelloApplication(context: LagomApplicationContext)
  extends LagomApplication(context)
    with AhcWSComponents {

  // Bind the service that this server provides
  override lazy val lagomServer =
    serverFor[HelloService](wire[HelloServiceImpl])
    .additionalRouter(wire[HelloGrpcServiceImpl])

}

Its all fine for the purpose of demo but we may not need to always create a REST endpoint besides gRPC endpoint. In that case I won't need either HelloService or HelloServiceImpl. The problem is how would you create lagom server with only HelloGrpcServiceImpl? I can't see to find a way either any documentation or the APIs itself to be able to achieve this!

Please suggest.


Solution

  • Based on the answer in the link I provided as a comment to my question, the solution would look something like this:

    trait PrimeGeneratorService extends Service {
      override def descriptor: Descriptor = named("prime-generator")
    }
    
    class PrimeGeneratorServiceImpl() extends PrimeGeneratorService
    
    abstract class PrimeGeneratorApplication(context: LagomApplicationContext)
      extends LagomApplication(context) with AhcWSComponents {
    
      override lazy val lagomServer: LagomServer = {
        serverFor[PrimeGeneratorService](wire[PrimeGeneratorServiceImpl])
          .additionalRouter(wire[PrimeGeneratorGrpcServiceImpl])
      }
    
    }
    
    class PrimeGeneratorLoader extends LagomApplicationLoader {
      override def load(context: LagomApplicationContext): LagomApplication =
        new PrimeGeneratorApplication(context) {
          override def serviceLocator: ServiceLocator = NoServiceLocator
        }
    
      override def loadDevMode(context: LagomApplicationContext): LagomApplication =
        new PrimeGeneratorApplication(context) with LagomDevModeComponents
    
      override def describeService = Some(readDescriptor[PrimeGeneratorService])
    }
    
    

    Here we have to create dummy service trait and implementation, namely, PrimeGeneratorService and PrimeGeneratorServiceImpl, respectively.

    Don't forget to add following in the loader class:

    override def describeService = Some(readDescriptor[PrimeGeneratorService])