Write the Server main entry¶
In authservice/src/main/scala/chatroom/AuthServer.scala’s main() method:
- Use ServerBuilder to build a new server that listens on port 9091
- Add an instance of AuthServiceImpl as a service: builder.addService(…)
// TODO Use ServerBuilder to create a new Server instance. Start it, and await termination.
val algorithm = Algorithm.HMAC256("secret")
val authServiceImpl = new AuthServiceImpl(repository, "auth-issuer", algorithm)
val server = ServerBuilder.forPort(9091)
.addService(AuthenticationServiceGrpc.bindService(authServiceImpl, ExecutionContext.global))
.build
- Add a shutdown hook to shutdown the server in case the process receives shutdown signal. This will try to shutdown the server gracefully, if shutdown hook is called.
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run(): Unit = {
server.shutdownNow
}
})
- Start the server:
server.start()
logger.info("Server started on port 9091")
- Server will be running in a background thread. Call server.awaitTermination() to block the main thread:
server.awaitTermination()
Run the Server¶
To run the authentication server:
$ sbt authservice/run ... [info] Running chatroom.AuthServer ... INFO: Server started on port 9091
Not bad for the first gRPC service!
Note that the username / passwords are read from the file userdatabase.txt in the root of the project directory.