Skip to content

Commit 9f2cfc4

Browse files
authored
Merge pull request #66 from diogob/db-uri-from-file
Allow reading database connection string from file
2 parents e9cd961 + f379abb commit 9f2cfc4

File tree

6 files changed

+16
-4
lines changed

6 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.9.0.0
44

5+
- Add @filename semantics to PGWS_DB_URI configiration variable to allow secret management to use a file instead of an environment variable.
56
- Add PGWS_RETRIES to limit the amount of times the server tries to open a database connection upon startup (defaults to 5). This breaks backward compatibility if you rely on the behaviour of the server to try infitite times.
67

78
## 0.8.0.1

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ source sample-env && ~/.local/bin/postgres-websockets
5252
```
5353
After running the above command, open your browser on http://localhost:3000 to see an example of usage.
5454

55-
The sample config file provided in the [sample.conf](https://github.com/diogob/postgres-websockets/tree/master/sample.conf) file comes with a jwt secret just for testing and is used in the sample client.
55+
The sample config file provided in the [sample-env](https://github.com/diogob/postgres-websockets/tree/master/sample-env) file comes with a jwt secret just for testing and is used in the sample client.
56+
Note that the `sample-env` points to `./database-uri.txt` to load the URI from an external file. This is determined by the use of `@` as a prefix to the value of the variable `PGWS_DB_URI`.
57+
This is entirely optional and the URI could be exported directly as `PGWS_DB_URI` without using the prefix `@`.
5658
You will find the complete sources for the example under the folder [client-example](https://github.com/diogob/postgres-websockets/tree/master/client-example).
5759
To run the server without giving access to any static files one can unser the variable `PGWS_ROOT_PATH`.
5860

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.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Module : PostgresWebsockets
33
Description : PostgresWebsockets main library interface.
44
5-
These are all function necessary to start a fully functionaing service.
5+
These are all function necessary to configure and start the server.
66
-}
77
module PostgresWebsockets
88
( prettyVersion

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)