2019-12-17 17:31:50 +03:00
|
|
|
module King.App
|
|
|
|
( App
|
|
|
|
, runApp
|
2019-12-19 14:32:56 +03:00
|
|
|
, runAppLogFile
|
|
|
|
, runAppLogHandle
|
2019-12-17 17:31:50 +03:00
|
|
|
, runPierApp
|
|
|
|
, HasConfigDir(..)
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Config
|
|
|
|
import UrbitPrelude
|
|
|
|
|
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)
|
|
|
|
where
|
|
|
|
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
|
|
|
|
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- A PierApp is like an App, except that it also provides a PierConfig
|
|
|
|
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
|
|
|
|
|
|
|
|
runPierApp :: PierConfig -> NetworkConfig -> RIO PierApp a -> IO a
|
|
|
|
runPierApp pierConfig networkConfig inner = do
|
2019-12-19 14:32:56 +03:00
|
|
|
logOptions <- logOptionsHandle stdout True
|
2019-12-17 17:31:50 +03:00
|
|
|
<&> setLogUseTime True
|
|
|
|
<&> setLogUseLoc False
|
|
|
|
|
|
|
|
withLogFunc logOptions $ \logFunc ->
|
|
|
|
go $ PierApp { _pierAppLogFunc = logFunc
|
|
|
|
, _pierAppPierConfig = pierConfig
|
|
|
|
, _pierAppNetworkConfig = networkConfig
|
|
|
|
}
|
|
|
|
where
|
|
|
|
go app = runRIO app inner
|