Skip to content

Commit

Permalink
Merge pull request #66 from diogob/db-uri-from-file
Browse files Browse the repository at this point in the history
Allow reading database connection string from file
  • Loading branch information
diogob authored Sep 10, 2020
2 parents e9cd961 + f379abb commit 9f2cfc4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.9.0.0

- Add @filename semantics to PGWS_DB_URI configiration variable to allow secret management to use a file instead of an environment variable.
- 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.

## 0.8.0.1
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ source sample-env && ~/.local/bin/postgres-websockets
```
After running the above command, open your browser on http://localhost:3000 to see an example of usage.

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.
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.
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`.
This is entirely optional and the URI could be exported directly as `PGWS_DB_URI` without using the prefix `@`.
You will find the complete sources for the example under the folder [client-example](https://github.com/diogob/postgres-websockets/tree/master/client-example).
To run the server without giving access to any static files one can unser the variable `PGWS_ROOT_PATH`.

Expand Down
1 change: 1 addition & 0 deletions database-uri.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postgres://localhost:5432/postgres
2 changes: 1 addition & 1 deletion sample-env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## PostgreSQL URI where the server will connect to issue NOTIFY and LISTEN commands
export PGWS_DB_URI="postgres://localhost:5432/postgres"
export PGWS_DB_URI="@./database-uri.txt"

## 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).
export PGWS_POOL_SIZE=10
Expand Down
2 changes: 1 addition & 1 deletion src/PostgresWebsockets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module : PostgresWebsockets
Description : PostgresWebsockets main library interface.
These are all function necessary to start a fully functionaing service.
These are all function necessary to configure and start the server.
-}
module PostgresWebsockets
( prettyVersion
Expand Down
10 changes: 9 additions & 1 deletion src/PostgresWebsockets/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ prettyVersion = intercalate "." $ map show $ versionBranch version

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

-- | Given a shutdown handler and an AppConfig builds a Warp Settings to start a stand-alone server
warpSettings :: (IO () -> IO ()) -> AppConfig -> Settings
Expand Down Expand Up @@ -73,6 +73,14 @@ readOptions =
<*> var auto "PGWS_POOL_SIZE" (def 10 <> helpDef show <> help "How many connection to the database should be used by the connection pool")
<*> 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")

loadDatabaseURIFile :: AppConfig -> IO AppConfig
loadDatabaseURIFile conf@AppConfig{..} =
case stripPrefix "@" configDatabase of
Nothing -> pure conf
Just filename -> setDatabase . strip <$> readFile (toS filename)
where
setDatabase uri = conf {configDatabase = uri}

loadSecretFile :: AppConfig -> IO AppConfig
loadSecretFile conf = extractAndTransform secret
where
Expand Down

0 comments on commit 9f2cfc4

Please sign in to comment.