Skip to content
Snippets Groups Projects
Verified Commit 731efa00 authored by jan.hamal.dvorak's avatar jan.hamal.dvorak
Browse files

Extend configuration file format

parent cbb13e47
No related branches found
No related tags found
Loading
...@@ -30,14 +30,14 @@ module Hikaru.Config ...@@ -30,14 +30,14 @@ module Hikaru.Config
, generateSecret , generateSecret
) )
where where
import Relude hiding (lines) import Relude hiding (drop, lines, isPrefixOf, length)
import qualified Data.Map as Map import qualified Data.Map as Map
import Crypto.Random.Entropy import Crypto.Random.Entropy
import Data.ByteArray.Encoding import Data.ByteArray.Encoding
import Data.List (lines, span)
import Data.String.Conversions import Data.String.Conversions
import Data.Text hiding (map)
import System.Environment import System.Environment
...@@ -68,11 +68,24 @@ where ...@@ -68,11 +68,24 @@ where
-- let cfg = env <> def -- let cfg = env <> def
-- @ -- @
-- --
-- Configuration file format is approximately this:
--
-- @
-- # How many seconds before forms need to be reloaded?
-- CSRF_VALIDITY = 3600
--
-- # Secret key to protect against CSRF. Don't tell anyone!
-- CSRF_SECRET = Ain9eec8aighoiri
-- @
--
configFromFile :: (MonadIO m) => FilePath -> m Config configFromFile :: (MonadIO m) => FilePath -> m Config
configFromFile path = Map.fromList <$> map parse <$> lines <$> readFile path configFromFile path = Map.fromList <$> parseFile <$> readFileText path
where where
parse = conv . span (/= '=') parseFile = mapMaybe parseLine . lines
conv (k, v) = (cs k, cs $ drop 1 v) parseLine = fmap tidy . fmap parseKV . reject . strip
parseKV = fmap (drop 1) . span (/= '=')
reject = guarded (not . isPrefixOf "#") <=< guarded ("" /=)
tidy (k, v) = (strip k, strip v)
-- | -- |
...@@ -84,7 +97,6 @@ where ...@@ -84,7 +97,6 @@ where
configDefault :: (MonadIO m) => m Config configDefault :: (MonadIO m) => m Config
configDefault = do configDefault = do
secret <- generateSecret 16 secret <- generateSecret 16
return $ Map.fromList [ ("CSRF_SECRET", secret) return $ Map.fromList [ ("CSRF_SECRET", secret)
, ("CSRF_VALIDITY", "86400") , ("CSRF_VALIDITY", "86400")
] ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment