Abaks: Drivers

Last time we introduced Interface Adapters with servant to serve a HTTP (REST) API.

But we've been left with some Sem r-based definition we have to transform to fit in servant types:

Let's start by setting the layout:

main :: IO ()
main = do
  let settings = setPort 8080 $ setHost "0.0.0.0" defaultSettings
  eventSourcingSettings <- EventSourcing.mkFileSettings "abaks-aggregates.json"
  runSettings settings $
    serve (Proxy @API) $
      hoistServer (Proxy @API) (hoistHandler eventSourcingSettings) server

The key here is hoistServer which takes a function to convert Sem r a to servant's Handler a.

And, surprise surprise, hoistServer is a classical interpreter:

hoistHandler ::
  EventSourcing.FileSettings ->
  Sem (Append ApiEffects '[Final IO]) a ->
  Handler a
hoistHandler eventSourcingSettings =
  Handler
    . ExceptT
    . runFinal
    . runError
    . Random.runRandom
    . EventSourcing.runFile eventSourcingSettings