2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Code for setting up the RIO environment.
|
|
|
|
-}
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.King.App
|
2019-12-17 17:31:50 +03:00
|
|
|
( App
|
2020-05-13 21:29:50 +03:00
|
|
|
, runAppStderr
|
2019-12-19 14:32:56 +03:00
|
|
|
, runAppLogFile
|
2020-01-11 03:39:31 +03:00
|
|
|
, runAppNoLog
|
2019-12-17 17:31:50 +03:00
|
|
|
, runPierApp
|
|
|
|
, HasConfigDir(..)
|
2020-02-04 04:27:16 +03:00
|
|
|
, HasStderrLogFunc(..)
|
2019-12-17 17:31:50 +03:00
|
|
|
) where
|
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.King.Config
|
|
|
|
import Urbit.Prelude
|
2019-12-17 17:31:50 +03:00
|
|
|
|
2019-12-19 14:32:56 +03:00
|
|
|
import System.Directory (createDirectoryIfMissing, getHomeDirectory)
|
|
|
|
|
2019-12-17 17:31:50 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class HasConfigDir a where
|
|
|
|
configDirL ∷ Lens' a FilePath
|
|
|
|
|
2020-02-04 04:27:16 +03:00
|
|
|
class HasStderrLogFunc a where
|
|
|
|
stderrLogFuncL :: Lens' a LogFunc
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-12-17 17:31:50 +03:00
|
|
|
data App = App
|
2020-02-04 04:27:16 +03:00
|
|
|
{ _appLogFunc :: !LogFunc
|
|
|
|
, _appStderrLogFunc :: !LogFunc
|
2019-12-17 17:31:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''App
|
|
|
|
|
|
|
|
instance HasLogFunc App where
|
|
|
|
logFuncL = appLogFunc
|
|
|
|
|
2020-02-04 04:27:16 +03:00
|
|
|
instance HasStderrLogFunc App where
|
|
|
|
stderrLogFuncL = appStderrLogFunc
|
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
runAppStderr :: RIO App a -> IO a
|
|
|
|
runAppStderr inner = do
|
2020-02-04 04:27:16 +03:00
|
|
|
logOptions <- logOptionsHandle stderr True
|
2019-12-17 17:31:50 +03:00
|
|
|
<&> setLogUseTime True
|
|
|
|
<&> setLogUseLoc False
|
|
|
|
|
|
|
|
withLogFunc logOptions $ \logFunc ->
|
2020-02-04 04:27:16 +03:00
|
|
|
runRIO (App logFunc logFunc) inner
|
2019-12-19 14:32:56 +03:00
|
|
|
|
|
|
|
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
|
2019-12-19 14:32:56 +03:00
|
|
|
|
2020-01-11 03:39:31 +03:00
|
|
|
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
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
-- | A PierApp is like an App, except that it also provides a PierConfig
|
2019-12-17 17:31:50 +03:00
|
|
|
data PierApp = PierApp
|
|
|
|
{ _pierAppLogFunc :: !LogFunc
|
2020-02-04 04:27:16 +03:00
|
|
|
, _pierAppStderrLogFunc :: !LogFunc
|
2019-12-17 17:31:50 +03:00
|
|
|
, _pierAppPierConfig :: !PierConfig
|
|
|
|
, _pierAppNetworkConfig :: !NetworkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''PierApp
|
|
|
|
|
2020-02-04 04:27:16 +03:00
|
|
|
instance HasStderrLogFunc PierApp where
|
|
|
|
stderrLogFuncL = pierAppStderrLogFunc
|
|
|
|
|
2019-12-17 17:31:50 +03:00
|
|
|
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
|
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
runPierApp :: PierConfig -> NetworkConfig -> RIO PierApp a -> RIO App a
|
|
|
|
runPierApp pierConfig networkConfig action = do
|
|
|
|
app <- ask
|
2020-01-11 03:39:31 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
let pierApp = PierApp { _pierAppLogFunc = app ^. logFuncL
|
|
|
|
, _pierAppStderrLogFunc = app ^. stderrLogFuncL
|
|
|
|
, _pierAppPierConfig = pierConfig
|
|
|
|
, _pierAppNetworkConfig = networkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
io (runRIO pierApp action)
|