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
|
|
|
|
, runApp
|
2019-12-19 14:32:56 +03:00
|
|
|
, runAppLogFile
|
|
|
|
, runAppLogHandle
|
2020-01-11 03:39:31 +03:00
|
|
|
, runAppNoLog
|
2019-12-17 17:31:50 +03:00
|
|
|
, runPierApp
|
|
|
|
, HasConfigDir(..)
|
|
|
|
) 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
|
|
|
|
|
|
|
|
data App = App
|
|
|
|
{ _appLogFunc :: !LogFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''App
|
|
|
|
|
|
|
|
instance HasLogFunc App where
|
|
|
|
logFuncL = appLogFunc
|
|
|
|
|
2019-12-19 14:32:56 +03:00
|
|
|
runAppLogHandle :: Handle -> RIO App a -> IO a
|
|
|
|
runAppLogHandle logHandle inner = do
|
|
|
|
logOptions <- logOptionsHandle logHandle True
|
2019-12-17 17:31:50 +03:00
|
|
|
<&> setLogUseTime True
|
|
|
|
<&> setLogUseLoc False
|
|
|
|
|
|
|
|
withLogFunc logOptions $ \logFunc ->
|
|
|
|
go (App logFunc)
|
|
|
|
where
|
|
|
|
go app = runRIO app inner
|
|
|
|
|
2019-12-19 14:32:56 +03:00
|
|
|
runApp :: RIO App a -> IO a
|
|
|
|
runApp = runAppLogHandle stdout
|
|
|
|
|
|
|
|
runAppLogFile :: RIO App a -> IO a
|
|
|
|
runAppLogFile inner = withLogFileHandle (\h -> runAppLogHandle h inner)
|
|
|
|
|
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 =
|
|
|
|
withFile "/dev/null" AppendMode $ \handle ->
|
|
|
|
runAppLogHandle handle 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
|
|
|
|
, _pierAppPierConfig :: !PierConfig
|
|
|
|
, _pierAppNetworkConfig :: !NetworkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''PierApp
|
|
|
|
|
|
|
|
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-01-11 03:39:31 +03:00
|
|
|
runPierApp :: PierConfig -> NetworkConfig -> Bool -> RIO PierApp a -> IO a
|
|
|
|
runPierApp pierConfig networkConfig daemon inner =
|
|
|
|
if daemon
|
|
|
|
then exec stderr
|
|
|
|
else withLogFileHandle exec
|
2019-12-17 17:31:50 +03:00
|
|
|
where
|
2020-01-11 03:39:31 +03:00
|
|
|
exec logHandle = do
|
|
|
|
logOptions <- logOptionsHandle logHandle True
|
|
|
|
<&> setLogUseTime True
|
|
|
|
<&> setLogUseLoc False
|
|
|
|
|
|
|
|
withLogFunc logOptions $ \logFunc ->
|
|
|
|
go $ PierApp { _pierAppLogFunc = logFunc
|
|
|
|
, _pierAppPierConfig = pierConfig
|
|
|
|
, _pierAppNetworkConfig = networkConfig
|
|
|
|
}
|
2019-12-17 17:31:50 +03:00
|
|
|
go app = runRIO app inner
|