Skip to content

Commit

Permalink
feat: Read config directly from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Dec 23, 2020
1 parent 1ad5da8 commit 57a0004
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 124 deletions.
29 changes: 17 additions & 12 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import Text.Printf (hPrintf)

import PostgREST.App (postgrest)
import PostgREST.Config (AppConfig (..), CLI (..), Command (..),
configDbPoolTimeout', dumpAppConfig,
prettyVersion, readCLIShowHelp,
Environment, configDbPoolTimeout',
dumpAppConfig, prettyVersion,
readCLIShowHelp, readEnvironment,
readValidateConfig)
import PostgREST.DbStructure (getDbStructure, getPgVersion)
import PostgREST.Error (PgError (PgError), checkIsFatal,
Expand Down Expand Up @@ -63,11 +64,14 @@ main = do
hSetBuffering stdin LineBuffering
hSetBuffering stderr NoBuffering

-- read PGRST_ env variables
env <- readEnvironment

-- read path from commad line
opts <- readCLIShowHelp
opts <- readCLIShowHelp env

-- build the 'AppConfig' from the config file path
conf <- readValidateConfig $ cliPath opts
conf <- readValidateConfig env $ cliPath opts

-- These are config values that can't be reloaded at runtime. Reloading some of them would imply restarting the web server.
let
Expand Down Expand Up @@ -140,10 +144,11 @@ main = do
Catch connWorker
) Nothing

-- Re-read the config on SIGUSR2
void $ installHandler sigUSR2 (
Catch $ reReadConfig (cliPath opts) refConf
) Nothing
-- Re-read the config on SIGUSR2, but only if we have a config file
when (isJust $ cliPath opts) $
void $ installHandler sigUSR2 (
Catch $ reReadConfig env (cliPath opts) refConf
) Nothing
#endif

-- reload schema cache on NOTIFY
Expand Down Expand Up @@ -330,12 +335,12 @@ listener dbUri dbChannel pool refConf refDbStructure mvarConnectionStatus connWo
errorMessage = "Could not listen for notifications on the " <> dbChannel <> " channel" :: Text
retryMessage = "Retrying listening for notifications on the " <> dbChannel <> " channel.." :: Text

#ifndef mingw32_HOST_OS
-- | Re-reads the config at runtime. Invoked on SIGUSR2.
-- | If it panics(config path was changed, invalid setting), it'll show an error but won't kill the main thread.
#ifndef mingw32_HOST_OS
reReadConfig :: FilePath -> IORef AppConfig -> IO ()
reReadConfig path refConf = do
conf <- readValidateConfig path
reReadConfig :: Environment -> Maybe FilePath -> IORef AppConfig -> IO ()
reReadConfig env path refConf = do
conf <- readValidateConfig env path
atomicWriteIORef refConf conf
putStrLn ("Config file reloaded" :: Text)
#endif
Expand Down
12 changes: 3 additions & 9 deletions nix/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

In order to build an optimal PostgREST Docker image, we create the image from
scratch (i.e., without a parent image like `debian` or `alpine`), and only
include the files that are essential for running PostgREST (the static
PostgREST binary and a `postgrest.conf`).
include the file that is essential for running PostgREST: the static
PostgREST binary.

This is similar to what you would get with the following `Dockerfile`:

Expand All @@ -17,12 +17,6 @@ FROM scratch
# need to include for running the application.
ADD /absolute/path/to/postgrest /bin/postgrest

# Include a default configuration file.
ADD /absolute/path/to/postgrest.conf /etc/postgrest.conf

ENV PGRST_DB_URI= \
...

EXPOSE 3000

# This is the user id that Docker will run our image under by default. Note
Expand All @@ -32,7 +26,7 @@ EXPOSE 3000
# can be run under any user you specify.
USER 1000

CMD [ "/bin/postgrest", "/etc/postgrest.conf" ]
CMD [ "/bin/postgrest" ]
```

# Building the Docker image with Nix
Expand Down
30 changes: 2 additions & 28 deletions nix/docker/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
, checkedShellScript
}:
let
config =
./postgrest.conf;

image =
dockerTools.buildImage {
name = "postgrest";
Expand All @@ -19,34 +16,11 @@ let

extraCommands =
''
mkdir etc
cp ${config} etc/postgrest.conf
rmdir share
'';

config = {
Cmd = [ "/bin/postgrest" "/etc/postgrest.conf" ];
Env = [
"PGRST_DB_URI=postgresql://?user=postgres"
"PGRST_DB_SCHEMA=public"
"PGRST_DB_ANON_ROLE="
"PGRST_DB_POOL=100"
"PGRST_DB_POOL_TIMEOUT=10"
"PGRST_DB_EXTRA_SEARCH_PATH=public"
"PGRST_DB_CHANNEL=pgrst"
"PGRST_DB_CHANNEL_ENABLED=false"
"PGRST_SERVER_HOST=*4"
"PGRST_SERVER_PORT=3000"
"PGRST_OPENAPI_SERVER_PROXY_URI="
"PGRST_JWT_SECRET="
"PGRST_SECRET_IS_BASE64=false"
"PGRST_JWT_AUD="
"PGRST_MAX_ROWS="
"PGRST_PRE_REQUEST="
"PGRST_ROLE_CLAIM_KEY=.role"
"PGRST_ROOT_SPEC="
"PGRST_RAW_MEDIA_TYPES="
];
Cmd = [ "/bin/postgrest" ];
User = "1000";
ExposedPorts = {
"3000/tcp" = { };
Expand All @@ -65,4 +39,4 @@ buildEnv
{
name = "postgrest-docker";
paths = [ load.bin ];
} // { inherit image config; }
} // { inherit image; }
30 changes: 0 additions & 30 deletions nix/docker/postgrest.conf

This file was deleted.

9 changes: 2 additions & 7 deletions nix/release/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,12 @@ let
| ${jq}/bin/jq -r .token
)"
# Plug the default config file into the full description.
defaultConfig="$(cat ${docker.config})"
export DEFAULT_CONFIG="$defaultConfig"
fullDescription="$(${envsubst}/bin/envsubst < ${fullDescription})"
# Patch the full description.
# Patch both descriptions.
responseCode="$(
${curl}/bin/curl -s --write-out "%{response_code}" \
--output /dev/null -H "Authorization: JWT $token" -X PATCH \
--data-urlencode description@${description} \
--data-urlencode "full_description=$fullDescription" \
--data-urlencode full_description@${fullDescription} \
"https://hub.docker.com/v2/repositories/$DOCKER_REPO/postgrest/"
)"
Expand Down
9 changes: 2 additions & 7 deletions nix/release/docker-hub-full-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ write from scratch.
To learn how to use this container, see the [PostgREST Docker
documentation](https://postgrest.com/en/stable/install.html#docker).

You can configure the PostgREST image by setting the enviroment variables used
in the default `/etc/postgrest.conf` file or overriding that file. This is the
default configuration file:

```
$DEFAULT_CONFIG
```
You can configure the PostgREST image by setting
[enviroment variables](https://postgrest.org/en/stable/configuration.html).

# How this image is built

Expand Down
Loading

0 comments on commit 57a0004

Please sign in to comment.