wrapped everything in the EnvM Monad, the logging might commence

This commit is contained in:
Dennis Frieberg 2021-04-05 22:11:07 +02:00
parent 27a51c2121
commit ac303abcc0
7 changed files with 78 additions and 51 deletions

View file

@ -8,11 +8,13 @@ import Toml (TomlCodec, (.=))
import qualified Toml
import System.IO
import qualified Data.Text as T
import Control.Concurrent.MVar
import Control.Concurrent
import UnliftIO.MVar
import UnliftIO.Concurrent
import WaiApp
import Data.Maybe(fromJust)
import Data.Coerce
import Environment
import Colog.Message
-- This Type is here to replace Toml.Codec.Monoid.First as the First Monoid
@ -29,20 +31,20 @@ setConfigM = Just . coerce
configM :: (Toml.Key -> TomlCodec a) -> Toml.Key -> TomlCodec (ConfigM a)
configM codec = Toml.diwrap . Toml.dioptional . codec
forkWithConfigs :: (a -> IO ()) -> TomlCodec [a] -> FilePath -> IO [MVar ()]
forkWithConfigs :: (a -> EnvM ()) -> TomlCodec [a] -> FilePath -> EnvM [MVar ()]
forkWithConfigs f = withConfigs (forkBackend . f)
withConfigs :: (a -> IO (MVar ())) -> TomlCodec [a] -> FilePath -> IO [MVar ()]
withConfigs :: (a -> EnvM b) -> TomlCodec [a] -> FilePath -> EnvM [b]
withConfigs f codec configFile = do
parseResult <- Toml.decodeFileEither codec configFile
either
(error . show)
(\x -> (logError . T.pack . show) x >> return [])
(mapM f)
parseResult
-- Note for later:
-- TODO this may need logging if f dies with an exception
forkBackend :: IO () -> IO (MVar ())
forkBackend :: EnvM () -> EnvM (MVar ())
forkBackend f = do
mVar <- newEmptyMVar
forkFinally f (const $ putMVar mVar ())