-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.purs
52 lines (43 loc) · 1.5 KB
/
App.purs
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
48
49
50
51
52
module Example.App where
import Prelude
import Control.Monad.Reader (ReaderT, ask, asks, runReaderT)
import Control.Monad.Reader.Class (class MonadAsk)
import Data.Either (Either(..))
import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console (log)
import Node.Process (getEnv)
import Type.Equality (class TypeEquals, from)
import Type.Proxy (Proxy(..))
import TypedEnv (fromEnv) as TypedEnv
import TypedEnv (printEnvError)
type Config =
{ "ALERT_EMAIL" :: String
, "ALERT_SUBJECT" :: String
}
newtype AppM a = AppM (ReaderT Config Effect a)
runAppM :: Config -> AppM ~> Effect
runAppM env (AppM m) = runReaderT m env
derive newtype instance functorAppM :: Functor AppM
derive newtype instance applyAppM :: Apply AppM
derive newtype instance applicativeAppM :: Applicative AppM
derive newtype instance bindAppM :: Bind AppM
derive newtype instance monadAppM :: Monad AppM
derive newtype instance monadEffectAppM :: MonadEffect AppM
instance monadAskAppM :: TypeEquals e Config => MonadAsk e AppM where
ask = AppM $ asks from
main :: Effect Unit
main = do
eitherConfig <- TypedEnv.fromEnv (Proxy :: _ Config) <$> getEnv
case eitherConfig of
Left error ->
log $ "ERROR: " <> printEnvError error
Right config ->
runAppM config sendAlert
sendAlert :: AppM Unit
sendAlert = do
{ "ALERT_EMAIL": email, "ALERT_SUBJECT": subject } <- ask
liftEffect $ log
( "Sending alert with subject \"" <> subject <> "\" to \"" <> email <>
"\"...done."
)