breve/src/Breve/Settings.hs

50 lines
1.4 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Breve.Settings where
import Control.Monad (when)
import System.Environment (lookupEnv)
import System.Environment.XDG.BaseDir
import System.Directory (doesFileExist)
import Data.Configurator
import Network.Wai.Handler.WarpTLS (tlsSettings, TLSSettings)
data AppSettings = AppSettings
{ bindPort :: Int
, bindUrl :: String
, urlTable :: FilePath
, tlsSetts :: TLSSettings
}
createEmptyIfMissing :: FilePath -> IO ()
createEmptyIfMissing file = do
exists <- doesFileExist file
when (not exists) (writeFile file "")
settings :: IO AppSettings
settings = do
urlsPath <- getUserDataFile "breve" ""
configPath <- getUserConfigFile "breve" ""
config <- load [Required configPath]
host <- lookupDefault "localhost" config "hostname"
port <- lookupDefault 3000 config "port"
cert <- lookupDefault "/usr/share/tls/breve.crt" config "cert"
key <- lookupDefault "/usr/share/tls/breve.key" config "key"
urls <- lookupDefault urlsPath config "urltable"
createEmptyIfMissing urls
let base = "https://" ++ host
url = if port == 443
then base
else base ++ ":" ++ show port
return AppSettings
{ bindPort = port
, bindUrl = url ++ "/"
, urlTable = urls
, tlsSetts = tlsSettings cert key
}