Skip to content

Commit

Permalink
Switch to configurator-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
begriffs committed Mar 13, 2017
1 parent 5c7feec commit 8b7ee77
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion postgrest.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ library
, bytestring
, case-insensitive
, cassava
, configurator
, configurator-ng == 0.0.0.1
, containers
, contravariant
, either
Expand Down
54 changes: 23 additions & 31 deletions src/PostgREST/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BS
import qualified Data.CaseInsensitive as CI
import qualified Data.Configurator as C
import qualified Data.Configurator.Types as C
import qualified Data.Configurator.Parser as C
import Data.List (lookup)
import Data.Monoid
import Data.Text (strip, intercalate, lines)
Expand Down Expand Up @@ -95,28 +95,30 @@ readOptions = do
cfgPath <- customExecParser parserPrefs opts
-- Now read the actual config file
conf <- catch
(C.load [C.Required cfgPath])
(C.readConfig =<< C.load [C.Required cfgPath])
configNotfoundHint

handle missingKeyHint $ do
-- db ----------------
cDbUri <- C.require conf "db-uri"
cDbSchema <- C.require conf "db-schema"
cDbAnon <- C.require conf "db-anon-role"
cPool <- C.lookupDefault 10 conf "db-pool"
-- server ------------
cHost <- C.lookupDefault "*4" conf "server-host"
cPort <- C.lookupDefault 3000 conf "server-port"
cProxy <- C.lookup conf "server-proxy-uri"
-- jwt ---------------
cJwtSec <- C.lookup conf "jwt-secret"
cJwtB64 <- C.lookupDefault False conf "secret-is-base64"
-- safety ------------
cMaxRows <- C.lookup conf "max-rows"
cReqCheck <- C.lookup conf "pre-request"

return $ AppConfig cDbUri cDbAnon cProxy cDbSchema cHost cPort
(encodeUtf8 <$> cJwtSec) cJwtB64 cPool cMaxRows cReqCheck False
let (mAppConf, errs) = flip C.runParserA conf $
AppConfig <$>
C.key "db-uri"
<*> C.key "db-anon-role"
<*> C.key "server-proxy-uri"
<*> C.key "db-schema"
<*> (fromMaybe "*4" <$> C.key "server-host")
<*> (fromMaybe 3000 <$> C.key "server-port")
<*> (fmap encodeUtf8 <$> C.key "jwt-secret")
<*> (fromMaybe False <$> C.key "secret-is-base64")
<*> (fromMaybe 10 <$> C.key "db-pool")
<*> C.key "max-rows"
<*> C.key "pre-request"
<*> pure False

case mAppConf of
Nothing -> do
forM_ errs $ hPutStrLn stderr . show
exitFailure
Just appConf ->
return appConf

where
opts = info (helper <*> pathParser) $
Expand All @@ -139,15 +141,6 @@ readOptions = do
"Cannot open config file:\n\t" <> show e
exitFailure

missingKeyHint :: C.KeyError -> IO a
missingKeyHint (C.KeyError n) = do
hPutStrLn stderr $
"Required config parameter \"" <> n <> "\" is missing or of wrong type.\n" <>
"Documentation for configuration options available at\n" <>
"\thttp://postgrest.com/en/v0.4/admin.html#configuration\n\n" <>
"Try the --example-config option to see how to configure PostgREST."
exitFailure

exampleCfg :: Doc
exampleCfg = vsep . map (text . toS) . lines $
[str|db-uri = "postgres://user:pass@localhost:5432/dbname"
Expand All @@ -173,7 +166,6 @@ readOptions = do
|# pre-request = "stored_proc_name"
|]


pathParser :: Parser FilePath
pathParser =
strArgument $
Expand Down
2 changes: 2 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ resolver: lts-8.0
extra-deps:
- Ranged-sets-0.3.0
- hasql-pool-0.4.1
- configurator-ng-0.0.0.1
- critbit-0.2.0.0
ghc-options:
postgrest: -O2 -Werror -Wall -fwarn-identities -fno-warn-redundant-constraints
nix:
Expand Down

0 comments on commit 8b7ee77

Please sign in to comment.