Skip to content

Commit 42273f9

Browse files
committed
Allow reading database connection string from file so we can make secret management easier on platforms that drop a secret in the filesystem
1 parent e9cd961 commit 42273f9

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

database-uri.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postgres://localhost:5432/postgres

sample-env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## PostgreSQL URI where the server will connect to issue NOTIFY and LISTEN commands
2-
export PGWS_DB_URI="postgres://localhost:5432/postgres"
2+
export PGWS_DB_URI="@./database-uri.txt"
33

44
## Size of connection pool used to issue notify commands (LISTEN commands are always issued on the same connection that is not part of the pool).
55
export PGWS_POOL_SIZE=10

src/PostgresWebsockets/Config.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ prettyVersion = intercalate "." $ map show $ versionBranch version
4343

4444
-- | Load all postgres-websockets config from Environment variables. This can be used to use just the middleware or to feed into warpSettings
4545
loadConfig :: IO AppConfig
46-
loadConfig = readOptions >>= loadSecretFile
46+
loadConfig = readOptions >>= loadSecretFile >>= loadDatabaseURIFile
4747

4848
-- | Given a shutdown handler and an AppConfig builds a Warp Settings to start a stand-alone server
4949
warpSettings :: (IO () -> IO ()) -> AppConfig -> Settings
@@ -73,6 +73,14 @@ readOptions =
7373
<*> var auto "PGWS_POOL_SIZE" (def 10 <> helpDef show <> help "How many connection to the database should be used by the connection pool")
7474
<*> var auto "PGWS_RETRIES" (def 5 <> helpDef show <> help "How many times it should try to connect to the database on startup before exiting with an error")
7575

76+
loadDatabaseURIFile :: AppConfig -> IO AppConfig
77+
loadDatabaseURIFile conf@AppConfig{..} =
78+
case stripPrefix "@" configDatabase of
79+
Nothing -> pure conf
80+
Just filename -> setDatabase . strip <$> readFile (toS filename)
81+
where
82+
setDatabase uri = conf {configDatabase = uri}
83+
7684
loadSecretFile :: AppConfig -> IO AppConfig
7785
loadSecretFile conf = extractAndTransform secret
7886
where

0 commit comments

Comments
 (0)