{-# LANGUAGE NoImplicitPrelude #-}
module RIO.Prelude.Simple
( SimpleApp
, mkSimpleApp
, runSimpleApp
) where
import RIO.Prelude.Reexports
import RIO.Prelude.Logger
import RIO.Prelude.Lens
import RIO.Prelude.RIO
import RIO.Process
import System.Environment (lookupEnv)
data SimpleApp = SimpleApp
{ saLogFunc :: !LogFunc
, saProcessContext :: !ProcessContext
}
instance HasLogFunc SimpleApp where
logFuncL = lens saLogFunc (\x y -> x { saLogFunc = y })
instance HasProcessContext SimpleApp where
processContextL = lens saProcessContext (\x y -> x { saProcessContext = y })
mkSimpleApp :: MonadIO m => LogFunc -> Maybe ProcessContext -> m SimpleApp
mkSimpleApp logFunc mProcessContext = do
processContext <- maybe mkDefaultProcessContext pure mProcessContext
pure $ SimpleApp {saLogFunc = logFunc, saProcessContext = processContext}
runSimpleApp :: MonadIO m => RIO SimpleApp a -> m a
runSimpleApp m = liftIO $ do
verbose <- isJust <$> lookupEnv "RIO_VERBOSE"
lo <- logOptionsHandle stderr verbose
withLogFunc lo $ \lf -> do
simpleApp <- mkSimpleApp lf Nothing
runRIO simpleApp m