A Reverse Geocoding gRPC Service Written in Scala
I made a reverse geocoder gRPC server as a demo of how one might structure a backend service in Scala. I structured the application to have a purely functional core, with an imperative shell.
However there’s a twist to the plot. I’m mixing classical OOP with pure FP. I wanted to see what the code looked like if I used a dependency injection framework (Airframe) to wire up the side effects at the outer edges.
The main method is where we build the object graph:
object Main extends App with LazyLogging {
override def main(args: Array[String]): Unit = {
val config = loadConfigOrThrow[Config]
// Wire up dependencies
newDesign
.bind[Config].toInstance(config)
.bind[Clock].toInstance(clock)
.bind[Healthttpd].toInstance(Healthttpd(config.statusPort))
.bind[LinesFileReader].toInstance(fileReader)
// Load places from disk immediately upon startup
.bind[KDTreeMap[Location, Place]].toEagerSingletonProvider(loadPlacesBlocking)
// Startup
.withProductionMode
.noLifeCycleLogging
.withSession(_.build[Application].run())
// Side effects are injected at the edge:
lazy val fileReader: LinesFileReader = () => {
logger.info(s"Loading places from ${config.placesFilePath}")
val reader = new BufferedReader(
new InputStreamReader(new FileInputStream(config.placesFilePath), "UTF-8")
)
Observable.fromLinesReader(reader)
}
lazy val loadPlacesBlocking: PlacesLoader => KDTreeMap[Location, Place] = { loader =>
Await.result(loader.load().runAsync, 1 minute)
}
lazy val clock: Clock = {
Observable
.interval(1 second)
.map(_ => Instant.now())
}
}
}
Side effects are:
- Reading from the file system
- The clock
fileReader
gives us a stream of lines from the file, and the clock is a stream of Instant
s. Both are modelled using the Monix Observable type.
The Application
trait is still very much imperative. We set up application status, served via Healthttpd, then start the gRPC server.
trait Application extends LazyLogging {
private val config = bind[Config]
private val healthttpd = bind[Healthttpd]
private val reverseGeocoderService = bind[ReverseGeocoderService]
def run(): Unit = {
healthttpd.startAndIndicateNotReady()
logger.info("Starting gRPC server")
val grpcServer = NettyServerBuilder
.forPort(config.grpcPort)
.addService(ReverseGeocoderGrpcMonix.bindService(reverseGeocoderService, monix.execution.Scheduler.global))
.build()
.start()
sys.ShutdownHookThread {
grpcServer.shutdown()
healthttpd.stop()
}
healthttpd.indicateReady()
grpcServer.awaitTermination()
}
}
The core of the application, concerned with serving requests, is pure, and easily tested. I’m using Task as an IO monad.
class ReverseGeocodeLocationRpc(places: KDTreeMap[Location, Place], clock: Clock) {
def handle(request: ReverseGeocodeLocationRequest): Task[ReverseGeocodeLocationResponse] = {
findNearest(request.latitude, request.longitude)(places)
.map(Task.now(_))
.map(addSunTimes(_, clock).map(toResponse))
.getOrElse(emptyTaskResponse)
}
private def findNearest(latitude: Latitude, longitude: Longitude)(places: KDTreeMap[Location, Place]): Option[Place] = {
places
.findNearest((latitude, longitude), 1)
.headOption
.map(_._2)
}
private case class Sun(rise: Option[Timestamp], set: Option[Timestamp])
private def addSunTimes(place: Task[Place], clock: Clock): Task[Place] = {
Task.zip2(place, clock.firstL).map {
case (p, t) =>
val zonedDateTime = t.atZone(ZoneId.of(p.timezone))
val sun = calculateSun(p.latitude, p.longitude, p.elevationMeters, zonedDateTime)
p.copy(sunriseToday = sun.rise, sunsetToday = sun.set)
}
}
private def calculateSun(latitude: Latitude,
longitude: Longitude,
altitudeMeters: Int,
zonedDateTime: ZonedDateTime): Sun = {
val solarTime = SolarTime.ofLocation(latitude, longitude, altitudeMeters, StdSolarCalculator.TIME4J)
val calendarDate = PlainDate.from(zonedDateTime.toLocalDate)
def toTimestamp(moment: Moment) = Timestamp(moment.getPosixTime, moment.getNanosecond())
val rise = solarTime.sunrise().apply(calendarDate).asScala.map(toTimestamp)
val set = solarTime.sunset().apply(calendarDate).asScala.map(toTimestamp)
Sun(rise, set)
}
private def toResponse(place: Place): ReverseGeocodeLocationResponse = {
ReverseGeocodeLocationResponse(Some(place))
}
private val emptyTaskResponse = Task.now(ReverseGeocodeLocationResponse.defaultInstance)
}
This was a pragmatic approach to putting togetger a Scala backend application. I picked a toy service to experiment with DI in the context of FP. I think that the result wasn’t too gnarly.
The source code is available on GitHub: reverse-geocoder.