breve/src/Breve/Settings.hs

54 lines
1.5 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 Data.Monoid
import Data.Text (Text, pack)
import Network.Wai.Handler.WarpTLS (tlsSettings, TLSSettings)
data AppSettings = AppSettings
{ bindHost :: Text
, bindPort :: Int
, bindUrl :: Text
, 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 <> ":" <> pack (show port)
return AppSettings
{ bindHost = host
, bindPort = port
, bindUrl = url <> "/"
, urlTable = urls
, tlsSetts = tlsSettings cert key
}