Skip to content

Commit 384298c

Browse files
authored
Merge pull request #37 from Rembane/read-conf-from-env
Read configuration from environment variables.
2 parents 5aae95e + 046d4ce commit 384298c

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

app/Config.hs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ module Config ( prettyVersion
2323
import System.IO.Error (IOError)
2424
import Control.Applicative
2525
import qualified Data.Configurator as C
26+
import qualified Data.Configurator.Parser as C
2627
import qualified Data.Configurator.Types as C
2728
import Data.Monoid
29+
import Data.Scientific (floatingOrInteger)
2830
import Data.Text (intercalate, lines)
2931
import Data.Text.Encoding (encodeUtf8)
3032
import Data.Version (versionBranch)
3133
import Options.Applicative hiding (str)
3234
import Paths_postgres_websockets (version)
35+
import System.IO (hPrint)
3336
import Text.Heredoc
34-
import Text.PrettyPrint.ANSI.Leijen hiding ((<>))
37+
import Text.PrettyPrint.ANSI.Leijen hiding ((<>), (<$>))
3538
import qualified Text.PrettyPrint.ANSI.Leijen as L
3639
import Protolude hiding (intercalate, (<>))
3740

@@ -58,28 +61,40 @@ readOptions = do
5861
cfgPath <- customExecParser parserPrefs opts
5962
-- Now read the actual config file
6063
conf <- catch
61-
(C.load [C.Required cfgPath])
64+
(C.readConfig =<< C.load [C.Required cfgPath])
6265
configNotfoundHint
6366

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
8191

8292
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+
8398
opts = info (helper <*> pathParser) $
8499
fullDesc
85100
<> progDesc (

postgres-websockets.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ executable postgres-websockets
6363
, protolude >= 0.2
6464
, base64-bytestring
6565
, bytestring
66-
, configurator
66+
, configurator-ng >= 0.0.0.1
6767
, optparse-applicative
68+
, scientific >= 0.3.5.0
6869
, text
6970
, time
7071
, wai

stack.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
resolver: lts-9.12
22
extra-deps:
3-
- protolude-0.2
3+
- protolude-0.2
4+
- configurator-ng-0.0.0.1
5+
- critbit-0.2.0.0
46
ghc-options:
57
postgres-websockets: -O2 -Wall -fwarn-identities -fno-warn-redundant-constraints

0 commit comments

Comments
 (0)