"0.1.0"
liftIO $ do
lookupEnv "KAOS_LOG_LEVEL" <&> \case
Just "debug" -> Debug
Just "info" -> Info
Just "warn" -> Warn
_ -> Info
data ServerConfig
= ServerConfig {
Config :: Effect where
GetConfig :: Config m ServerConfig
GetsConfig :: (ServerConfig -> a) -> Config m a
makeEffect ''Config
runConfig
:: ServerConfig
-> Eff (Config : es) a
-> Eff es a
runConfig config = interpret $ \_ -> \case
GetConfig -> pure config
GetsConfig f -> pure (f config)
loadConfig
:: (Files :> es, RNG :> es, Fail :> es, IOE :> es)
=> Eff es ServerConfig
loadConfig = do
cfg <- require ".state/config.json" =<< readConfigFile
logLevel <- getLogLevel
-- Attempt to load the key file, and generate a key if one doesn't exist yet.
serverKey <- loadServerKey
res <- runNonDet OnEmptyKeep $ do
port <- require "'port'"
(parse cfg "port" <|> pure 1312)
name <- require "'name'"
(parse cfg "name")
pure (ServerConfig { .. })
either (const (fail "boo")) return res
where
JSON.lookup key obj >>= mayb . JSON.fromJSON
mayb = \case { JSON.Error _ -> Nothing; JSON.Success a -> Just a }
\case
Just v -> pure v
Nothing -> fail ("Requires " <> msg <> " but it is missing")
loadServerKey = readServerKey >>= \case
Just key -> case decodePrivateKey (BS.toStrict key) of
Just k -> return k
Nothing -> fail "Bad private key!"
Nothing -> do
(_, key) <- genRSA
writeServerKey (BS.fromStrict $ encodePrivateKey key) $> key