shrub/pkg/hs/urbit-king/lib/Urbit/King/App.hs

119 lines
3.3 KiB
Haskell
Raw Normal View History

2020-01-23 07:16:09 +03:00
{-|
Code for setting up the RIO environment.
-}
module Urbit.King.App
( App
, runAppStderr
, runAppLogFile
, runAppNoLog
, runPierApp
, HasConfigDir(..)
2020-02-04 04:27:16 +03:00
, HasStderrLogFunc(..)
) where
import Urbit.King.Config
import Urbit.Prelude
import System.Directory (createDirectoryIfMissing, getHomeDirectory)
--------------------------------------------------------------------------------
class HasConfigDir a where
configDirL Lens' a FilePath
2020-02-04 04:27:16 +03:00
class HasStderrLogFunc a where
stderrLogFuncL :: Lens' a LogFunc
--------------------------------------------------------------------------------
data App = App
2020-02-04 04:27:16 +03:00
{ _appLogFunc :: !LogFunc
, _appStderrLogFunc :: !LogFunc
}
makeLenses ''App
instance HasLogFunc App where
logFuncL = appLogFunc
2020-02-04 04:27:16 +03:00
instance HasStderrLogFunc App where
stderrLogFuncL = appStderrLogFunc
runAppStderr :: RIO App a -> IO a
runAppStderr inner = do
2020-02-04 04:27:16 +03:00
logOptions <- logOptionsHandle stderr True
<&> setLogUseTime True
<&> setLogUseLoc False
withLogFunc logOptions $ \logFunc ->
2020-02-04 04:27:16 +03:00
runRIO (App logFunc logFunc) inner
runAppLogFile :: RIO App a -> IO a
2020-02-04 04:27:16 +03:00
runAppLogFile inner =
withLogFileHandle $ \h -> do
logOptions <- logOptionsHandle h True
<&> setLogUseTime True
<&> setLogUseLoc False
stderrLogOptions <- logOptionsHandle stderr True
<&> setLogUseTime False
<&> setLogUseLoc False
withLogFunc stderrLogOptions $ \stderrLogFunc ->
withLogFunc logOptions $ \logFunc ->
runRIO (App logFunc stderrLogFunc) inner
withLogFileHandle :: (Handle -> IO a) -> IO a
withLogFileHandle act = do
home <- getHomeDirectory
let logDir = home </> ".urbit"
createDirectoryIfMissing True logDir
withFile (logDir </> "king.log") AppendMode $ \handle -> do
hSetBuffering handle LineBuffering
act handle
runAppNoLog :: RIO App a -> IO a
runAppNoLog act =
2020-02-04 04:27:16 +03:00
withFile "/dev/null" AppendMode $ \handle -> do
logOptions <- logOptionsHandle handle True
withLogFunc logOptions $ \logFunc ->
runRIO (App logFunc logFunc) act
--------------------------------------------------------------------------------
2020-01-23 07:16:09 +03:00
-- | A PierApp is like an App, except that it also provides a PierConfig
data PierApp = PierApp
{ _pierAppLogFunc :: !LogFunc
2020-02-04 04:27:16 +03:00
, _pierAppStderrLogFunc :: !LogFunc
, _pierAppPierConfig :: !PierConfig
, _pierAppNetworkConfig :: !NetworkConfig
}
makeLenses ''PierApp
2020-02-04 04:27:16 +03:00
instance HasStderrLogFunc PierApp where
stderrLogFuncL = pierAppStderrLogFunc
instance HasLogFunc PierApp where
logFuncL = pierAppLogFunc
instance HasPierConfig PierApp where
pierConfigL = pierAppPierConfig
instance HasNetworkConfig PierApp where
networkConfigL = pierAppNetworkConfig
instance HasConfigDir PierApp where
configDirL = pierAppPierConfig . pcPierPath
runPierApp :: PierConfig -> NetworkConfig -> RIO PierApp a -> RIO App a
runPierApp pierConfig networkConfig action = do
app <- ask
let pierApp = PierApp { _pierAppLogFunc = app ^. logFuncL
, _pierAppStderrLogFunc = app ^. stderrLogFuncL
, _pierAppPierConfig = pierConfig
, _pierAppNetworkConfig = networkConfig
}
io (runRIO pierApp action)