aboutsummaryrefslogtreecommitdiff
path: root/hsm-dummy-receiver/Main.hs
blob: 78b343f005c00c76427debc82d8e716939e999cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

import Data.Function ((&))
import Data.Text (Text, pack)
import Effectful (Eff, (:>), runEff)
import Effectful.Log (Log, localDomain, logInfo_, runLog)
import Effectful.Reader.Static (runReader)
import Effectful.Resource (runResource)
import Hsm.Core.App (launch)
import Hsm.Core.Env (deriveFromYaml)
import Hsm.Core.Zmq.Client (receive, runClient)
import Streamly.Data.Fold qualified as S (drain)
import Streamly.Data.Stream qualified as S (Stream, fold, mapM)
import System.IO.Echo (withoutInputEcho)

data Env = Env
  { name :: Text
  , subEps :: [Text]
  , topics :: [Text]
  }

$(deriveFromYaml ''Env)

handle ::
     forall es. Log :> es
  => S.Stream (Eff es) Int
  -> Eff es ()
handle = S.fold S.drain . S.mapM handler
  where
    handler :: Int -> Eff es ()
    handler p =
      localDomain "receiver" $ logInfo_ $ "Received pulse #" <> pack (show p)

-- Dummy receiver service:
-- Proof of concept. Listens for "pulses" through ZMQ and logs each time one
-- is received.
main :: IO ()
main =
  launch @Env "dummy-receiver" withoutInputEcho $ \env logger level ->
    (receive & handle)
      & runClient @Env
      & runLog env.name logger level
      & runReader env
      & runResource
      & runEff