@@ -23,15 +23,18 @@ module Config ( prettyVersion
23
23
import System.IO.Error (IOError )
24
24
import Control.Applicative
25
25
import qualified Data.Configurator as C
26
+ import qualified Data.Configurator.Parser as C
26
27
import qualified Data.Configurator.Types as C
27
28
import Data.Monoid
29
+ import Data.Scientific (floatingOrInteger )
28
30
import Data.Text (intercalate , lines )
29
31
import Data.Text.Encoding (encodeUtf8 )
30
32
import Data.Version (versionBranch )
31
33
import Options.Applicative hiding (str )
32
34
import Paths_postgres_websockets (version )
35
+ import System.IO (hPrint )
33
36
import Text.Heredoc
34
- import Text.PrettyPrint.ANSI.Leijen hiding ((<>) )
37
+ import Text.PrettyPrint.ANSI.Leijen hiding ((<>) , (<$>) )
35
38
import qualified Text.PrettyPrint.ANSI.Leijen as L
36
39
import Protolude hiding (intercalate , (<>) )
37
40
@@ -58,28 +61,40 @@ readOptions = do
58
61
cfgPath <- customExecParser parserPrefs opts
59
62
-- Now read the actual config file
60
63
conf <- catch
61
- (C. load [C. Required cfgPath])
64
+ (C. readConfig =<< C. load [C. Required cfgPath])
62
65
configNotfoundHint
63
66
64
- handle missingKeyHint $ do
65
- -- db ----------------
66
- cDbUri <- C. require conf " db-uri"
67
- cPool <- C. lookupDefault 10 conf " db-pool"
68
- -- server ------------
69
- cPath <- C. require conf " server-root"
70
- cHost <- C. lookupDefault " *4" conf " server-host"
71
- cPort <- C. lookupDefault 3000 conf " server-port"
72
- cAuditC <- C. lookup conf " audit-channel"
73
- cChannel <- case cAuditC of
74
- Just c -> C. lookupDefault c conf " listen-channel"
75
- Nothing -> C. require conf " listen-channel"
76
- -- jwt ---------------
77
- cJwtSec <- C. require conf " jwt-secret"
78
- cJwtB64 <- C. lookupDefault False conf " secret-is-base64"
79
-
80
- return $ AppConfig cDbUri cPath cHost cPort cChannel (encodeUtf8 cJwtSec) cJwtB64 cPool
67
+ let (mAppConf, errs) = flip C. runParserM conf $ do
68
+ -- db ----------------
69
+ cDbUri <- C. key " db-uri"
70
+ cPool <- fromMaybe 10 . join . fmap coerceInt <$> C. key " db-pool"
71
+ -- server ------------
72
+ cPath <- C. key " server-root"
73
+ cHost <- fromMaybe " *4" . mfilter (/= " " ) <$> C. key " server-host"
74
+ cPort <- fromMaybe 3000 . join . fmap coerceInt <$> C. key " server-port"
75
+ cAuditC <- C. key " audit-channel"
76
+ cChannel <- case cAuditC of
77
+ Just c -> fromMaybe c . mfilter (/= " " ) <$> C. key " listen-channel"
78
+ Nothing -> C. key " listen-channel"
79
+ -- jwt ---------------
80
+ cJwtSec <- C. key " jwt-secret"
81
+ cJwtB64 <- fromMaybe False <$> C. key " secret-is-base64"
82
+
83
+ return $ AppConfig cDbUri cPath cHost cPort cChannel (encodeUtf8 cJwtSec) cJwtB64 cPool
84
+
85
+ case mAppConf of
86
+ Nothing -> do
87
+ forM_ errs $ hPrint stderr
88
+ exitFailure
89
+ Just appConf ->
90
+ return appConf
81
91
82
92
where
93
+ coerceInt :: (Read i , Integral i ) => C. Value -> Maybe i
94
+ coerceInt (C. Number x) = rightToMaybe $ floatingOrInteger x
95
+ coerceInt (C. String x) = readMaybe $ toS x
96
+ coerceInt _ = Nothing
97
+
83
98
opts = info (helper <*> pathParser) $
84
99
fullDesc
85
100
<> progDesc (
0 commit comments