breve/src/Breve/Settings.hs

77 lines
2.5 KiB
Haskell
Raw Normal View History

2019-11-06 15:09:02 +01:00
{-|
This module defines the Breve configuration
parser and application settings.
-}
module Breve.Settings
( AppSettings(..)
, createEmptyIfMissing
, settings
) where
import Paths_breve (getDataFileName)
2019-11-06 15:09:02 +01:00
import Control.Monad (when)
import System.Environment (lookupEnv)
import System.Directory (doesFileExist, getXdgDirectory, XdgDirectory(..))
import Data.Text (Text, pack)
2015-05-09 22:22:36 +02:00
import Data.Configurator
2015-08-11 04:02:10 +02:00
import Data.Monoid
2015-08-11 15:41:59 +02:00
2019-11-06 15:09:02 +01:00
import Network.Wai.Handler.WarpTLS (TLSSettings (..), tlsSettingsChain)
import Network.TLS (Version (..))
import Network.TLS.Extra (ciphersuite_strong)
2015-05-09 22:22:36 +02:00
2019-11-06 15:09:02 +01:00
-- | Breve settings
2015-05-09 22:22:36 +02:00
data AppSettings = AppSettings
2019-11-06 15:09:02 +01:00
{ bindHost :: Text -- ^ the host to bind to
, bindPort :: Int -- ^ the port to bind to
, bindUrl :: Text -- ^ the url used to reach breve
, urlTable :: FilePath -- ^ path where to save the url table
, staticDir :: FilePath -- ^ path of the static assets
2019-11-06 15:09:02 +01:00
, tlsSettings :: TLSSettings -- ^ warp TLS settings
2015-05-09 22:22:36 +02:00
}
2019-11-06 15:09:02 +01:00
-- | Initialises a file if it doesn't exist
2015-05-09 22:22:36 +02:00
createEmptyIfMissing :: FilePath -> IO ()
createEmptyIfMissing file = do
exists <- doesFileExist file
when (not exists) (writeFile file "")
2019-11-06 15:09:02 +01:00
-- | Configuration file parser
settings :: Maybe FilePath -> IO AppSettings
settings path = do
configPath <- case path of
Just path -> return path
Nothing -> getXdgDirectory XdgConfig "breve"
urlsPath <- getXdgDirectory XdgData "breve"
2015-05-09 22:22:36 +02:00
2017-02-21 16:22:57 +01:00
config <- load [Required configPath]
host <- lookupDefault "localhost" config "hostname"
portnum <- lookupDefault 3000 config "port"
urls <- lookupDefault urlsPath config "urltable"
cert <- lookupDefault "/usr/share/tls/breve.crt" config "tls.cert"
key <- lookupDefault "/usr/share/tls/breve.key" config "tls.key"
chain <- lookupDefault [] config "tls.chain"
2015-05-09 22:22:36 +02:00
2017-02-21 16:22:57 +01:00
let
port = if portnum == 443 then "" else ":" <> pack (show portnum)
url = "https://" <> host <> port <> "/"
baseURL <- lookupDefault url config "baseurl"
static <- getDataFileName "static/"
2015-05-09 22:22:36 +02:00
2017-02-21 16:22:57 +01:00
createEmptyIfMissing urls
2015-05-09 22:22:36 +02:00
return AppSettings
{ bindHost = host
, bindPort = portnum
, bindUrl = baseURL
, urlTable = urls
, staticDir = static
2017-02-21 16:22:57 +01:00
, tlsSettings = (tlsSettingsChain cert chain key)
{ tlsAllowedVersions = [TLS12, TLS11]
, tlsCiphers = ciphersuite_strong
}
2015-05-09 22:22:36 +02:00
}