Read db-uri configuration from a separate file. (#1215)
* Read dburi configuration from a separate file. * Add changelog entry.
This commit is contained in:
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
- #1205, Add support for parsing JSON Web Key Sets -@russelldavies
|
- #1205, Add support for parsing JSON Web Key Sets -@russelldavies
|
||||||
|
- #1203, Add support for reading db-uri from a separate file - @zhoufeng1989
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
+17
-2
@@ -25,7 +25,7 @@ import Data.IORef (IORef, atomicWriteIORef,
|
|||||||
import Data.String (IsString (..))
|
import Data.String (IsString (..))
|
||||||
import Data.Text (pack, replace, stripPrefix, strip)
|
import Data.Text (pack, replace, stripPrefix, strip)
|
||||||
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
|
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
|
||||||
import Data.Text.IO (hPutStrLn)
|
import Data.Text.IO (hPutStrLn, readFile)
|
||||||
import Data.Time.Clock (getCurrentTime)
|
import Data.Time.Clock (getCurrentTime)
|
||||||
import qualified Hasql.Pool as P
|
import qualified Hasql.Pool as P
|
||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Session as H
|
||||||
@@ -139,7 +139,7 @@ main = do
|
|||||||
--
|
--
|
||||||
-- readOptions builds the 'AppConfig' from the config file specified on the
|
-- readOptions builds the 'AppConfig' from the config file specified on the
|
||||||
-- command line
|
-- command line
|
||||||
conf <- loadSecretFile =<< readOptions
|
conf <- loadDbUriFile =<< loadSecretFile =<< readOptions
|
||||||
let host = configHost conf
|
let host = configHost conf
|
||||||
port = configPort conf
|
port = configPort conf
|
||||||
proxy = configProxyUri conf
|
proxy = configProxyUri conf
|
||||||
@@ -278,3 +278,18 @@ loadSecretFile conf = extractAndTransform mSecret
|
|||||||
-- replace: Replace every occurrence of one substring with another
|
-- replace: Replace every occurrence of one substring with another
|
||||||
replaceUrlChars =
|
replaceUrlChars =
|
||||||
replace "_" "/" . replace "-" "+" . replace "." "="
|
replace "_" "/" . replace "-" "+" . replace "." "="
|
||||||
|
|
||||||
|
{-
|
||||||
|
Load database uri from a separate file if `db-uri` is a filepath.
|
||||||
|
-}
|
||||||
|
loadDbUriFile :: AppConfig -> IO AppConfig
|
||||||
|
loadDbUriFile conf = extractDbUri mDbUri
|
||||||
|
where
|
||||||
|
mDbUri = configDatabase conf
|
||||||
|
extractDbUri :: Text -> IO AppConfig
|
||||||
|
extractDbUri dbUri =
|
||||||
|
fmap setDbUri $
|
||||||
|
case stripPrefix "@" dbUri of
|
||||||
|
Nothing -> return dbUri
|
||||||
|
Just filename -> strip <$> readFile (toS filename)
|
||||||
|
setDbUri dbUri = conf {configDatabase = dbUri}
|
||||||
|
|||||||
@@ -82,6 +82,24 @@ readSecretFromFile(){
|
|||||||
pgrStop
|
pgrStop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readDbUriFromFile(){
|
||||||
|
pgrConfig="dburi-from-file.config"
|
||||||
|
pgrStartRead "./configs/$pgrConfig" "./dburis/$1"
|
||||||
|
while pgrStarted && test "$( rootStatus )" -ne 200
|
||||||
|
do
|
||||||
|
# wait for the server to start
|
||||||
|
sleep 0.1 \
|
||||||
|
|| sleep 1 # fallback: subsecond sleep is not standard and may fail
|
||||||
|
done
|
||||||
|
if pgrStarted
|
||||||
|
then
|
||||||
|
ok "connection with $2 dburi read from a file"
|
||||||
|
else
|
||||||
|
ko "failed to read $2 dburi from a file"
|
||||||
|
fi
|
||||||
|
pgrStop
|
||||||
|
}
|
||||||
|
|
||||||
reqWithRoleClaimKey(){
|
reqWithRoleClaimKey(){
|
||||||
export ROLE_CLAIM_KEY=$1
|
export ROLE_CLAIM_KEY=$1
|
||||||
pgrStart "./configs/role-claim-key.config"
|
pgrStart "./configs/role-claim-key.config"
|
||||||
@@ -187,6 +205,9 @@ readSecretFromFile ascii.b64 'Base64 (ASCII)'
|
|||||||
readSecretFromFile utf8.b64 'Base64 (UTF-8)'
|
readSecretFromFile utf8.b64 'Base64 (UTF-8)'
|
||||||
readSecretFromFile binary.b64 'Base64 (binary)'
|
readSecretFromFile binary.b64 'Base64 (binary)'
|
||||||
|
|
||||||
|
readDbUriFromFile uri.noeol "(no EOL)"
|
||||||
|
readDbUriFromFile uri.txt "(EOL)"
|
||||||
|
|
||||||
reqWithRoleClaimKey '.postgrest.a_role' '{"postgrest":{"a_role":"postgrest_test_author"}}' 200
|
reqWithRoleClaimKey '.postgrest.a_role' '{"postgrest":{"a_role":"postgrest_test_author"}}' 200
|
||||||
reqWithRoleClaimKey '.customObject.manyRoles[1]' '{"customObject":{"manyRoles": ["other", "postgrest_test_author"]}}' 200
|
reqWithRoleClaimKey '.customObject.manyRoles[1]' '{"customObject":{"manyRoles": ["other", "postgrest_test_author"]}}' 200
|
||||||
reqWithRoleClaimKey '."https://www.example.com/roles"[0].value' '{"https://www.example.com/roles":[{"value":"postgrest_test_author"}]}' 200
|
reqWithRoleClaimKey '."https://www.example.com/roles"[0].value' '{"https://www.example.com/roles":[{"value":"postgrest_test_author"}]}' 200
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
db-uri = "@/dev/stdin"
|
||||||
|
db-schema = "test"
|
||||||
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
|
db-pool = 1
|
||||||
|
server-host = "127.0.0.1"
|
||||||
|
server-port = 49421
|
||||||
|
jwt-secret = "reallyreallyreallyreallyverysafe"
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
postgres:///postgrest_test
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
postgres:///postgrest_test
|
||||||
Reference in New Issue
Block a user