Skip to content

Commit

Permalink
Simplify the db connstring configuration (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri authored Sep 22, 2022
1 parent b3a88ea commit 72827d6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 50 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ jobs:
cabal install postgresql-migration
- name: Test
run: |
source environment.ci.sh
set -x
source ./environment.ci.sh
createdb -h "${FLORA_DB_HOST}" -p "${FLORA_DB_PORT}" -U "${FLORA_DB_USER}" -w "${FLORA_DB_DATABASE}"
migrate init "${FLORA_PG_CONNSTRING}"
migrate migrate "${FLORA_PG_CONNSTRING}" migrations
migrate init "${FLORA_DB_CONNSTRING}"
migrate migrate "${FLORA_DB_CONNSTRING}" migrations
cabal run -- flora-cli create-user --username "hackage-user" --email "[email protected]" --password "foobar2000"
cabal test
env:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Remove padding from README code paragraphs (#203)
* Simplify the database pool settings (#210)
* Readjust the size of the main title across browser sizes (#211)
* Take the database settings as a libpq connstring (#213)

## v1.0.2 -- 2022-09-13

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ db-drop: ## Drop the database
@dropdb -f --if-exists -h $(FLORA_DB_HOST) -p $(FLORA_DB_PORT) -U $(FLORA_DB_USER) $(FLORA_DB_DATABASE)

db-setup: db-create ## Setup the dev database
@migrate init "$(FLORA_PG_CONNSTRING)"
@migrate migrate "$(FLORA_PG_CONNSTRING)" migrations
@migrate init "$(FLORA_DB_CONNSTRING)"
@migrate migrate "$(FLORA_DB_CONNSTRING)" migrations

db-reset: db-drop db-setup db-provision ## Reset the dev database (uses Cabal)

Expand Down
2 changes: 1 addition & 1 deletion database.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DB="flora_dev"
POSTGRES_DB="flora_dev"
5 changes: 2 additions & 3 deletions environment.ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ export FLORA_DB_PASSWORD="postgres"
export FLORA_DB_POOL_CONNECTIONS=20
export FLORA_DB_PORT=5432
export FLORA_DB_HOST="localhost"
export FLORA_DB_SUB_POOLS=10
export FLORA_DB_TIMEOUT=10
export FLORA_DB_USER="postgres"
export FLORA_LOGGING_DESTINATION="stdout"
export FLORA_HTTP_PORT=8083

export FLORA_PG_URI="postgresql://${FLORA_DB_USER}:${FLORA_DB_PASSWORD}@${FLORA_DB_HOST}:${FLORA_DB_PORT}/${FLORA_DB_DATABASE}"
export FLORA_PG_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE} user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD}"
export FLORA_DB_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE}\
user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD}"
7 changes: 5 additions & 2 deletions environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export FLORA_DB_PASSWORD="postgres"
export FLORA_DB_DATABASE="flora_dev"
export FLORA_DB_POOL_CONNECTIONS="100"
export FLORA_DB_TIMEOUT="10"
export FLORA_DB_SSLMODE="allow"
export FLORA_DB_PARAMETERS="?sslmode=verify-ca"

export FLORA_PG_URI="postgresql://${FLORA_DB_USER}:${FLORA_DB_PASSWORD}@${FLORA_DB_HOST}:${FLORA_DB_PORT}/${FLORA_DB_DATABASE}"
export FLORA_PG_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE} user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD}"
export FLORA_DB_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE} \
user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD} \
sslmode=${FLORA_DB_SSLMODE:-allow}"

export FLORA_HTTP_PORT=8083
export FLORA_ENVIRONMENT="development"
Expand Down
4 changes: 3 additions & 1 deletion environment.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export FLORA_DOMAIN="localhost"

export FLORA_DB_DATABASE="flora_test"

export FLORA_PG_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE} user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD}"
export FLORA_DB_CONNSTRING="host=${FLORA_DB_HOST} dbname=${FLORA_DB_DATABASE} \
user=${FLORA_DB_USER} password=${FLORA_DB_PASSWORD} \
sslmode=allow"
31 changes: 9 additions & 22 deletions src/Flora/Environment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ module Flora.Environment
where

import Colourista.IO (blueMessage)
import Data.ByteString (ByteString)
import Data.Pool (Pool)
import Data.Pool qualified as Pool
import Data.Text
import Data.Text qualified as T
import Data.Text.Encoding qualified as Text
import Data.Time (NominalDiffTime)
import Data.Word (Word16)
import Database.PostgreSQL.Simple qualified as PG
Expand Down Expand Up @@ -44,15 +45,15 @@ data TestEnv = TestEnv
deriving stock (Generic)

mkPool
:: PG.ConnectInfo -- Database access information
:: ByteString -- Database access information
-> NominalDiffTime -- Allowed timeout
-> Int -- Number of connections
-> Eff '[IOE] (Pool PG.Connection)
mkPool connectInfo timeout' connections =
mkPool connectionInfo timeout' connections =
liftIO $
Pool.newPool $
Pool.PoolConfig
{ createResource = PG.connect connectInfo
{ createResource = PG.connectPostgreSQL connectionInfo
, freeResource = PG.close
, poolCacheTTL = realToFrac timeout'
, poolMaxResources = connections
Expand All @@ -61,36 +62,22 @@ mkPool connectInfo timeout' connections =
configToEnv :: FloraConfig -> Eff '[IOE] FloraEnv
configToEnv x@FloraConfig{..} = do
let PoolConfig{..} = dbConfig
pool <- mkPool connectInfo connectionTimeout connections
jobsPool <- mkPool connectInfo connectionTimeout connections
pool <- mkPool connectionInfo connectionTimeout connections
jobsPool <- mkPool connectionInfo connectionTimeout connections
pure FloraEnv{..}
where
config = x

testConfigToTestEnv :: TestConfig -> Eff '[IOE] TestEnv
testConfigToTestEnv config@TestConfig{..} = do
let PoolConfig{..} = config.dbConfig
pool <- mkPool connectInfo connectionTimeout connections
pool <- mkPool connectionInfo connectionTimeout connections
pure TestEnv{..}

displayConnectInfo :: PG.ConnectInfo -> Text
displayConnectInfo PG.ConnectInfo{..} =
T.pack $
"postgresql://"
<> connectUser
<> ":"
<> connectPassword
<> "@"
<> connectHost
<> ":"
<> show connectPort
<> "/"
<> connectDatabase

getFloraEnv :: Eff '[IOE] FloraEnv
getFloraEnv = do
config <- liftIO $ Env.parse id parseConfig
liftIO $ blueMessage $ "🔌 Connecting to database at " <> displayConnectInfo (config.connectInfo)
liftIO $ blueMessage $ "🔌 Connecting to database at " <> Text.decodeUtf8 config.connectionInfo
configToEnv config

getFloraTestEnv :: Eff '[IOE] TestEnv
Expand Down
38 changes: 22 additions & 16 deletions src/Flora/Environment/Config.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{-# OPTIONS_GHC -Wno-orphans #-}

-- | Externally facing config parsed from the environment.
module Flora.Environment.Config
( FloraConfig (..)
, LoggingEnv (..)
, ConnectionInfo (..)
, TestConfig (..)
, PoolConfig (..)
, DeploymentEnv (..)
Expand All @@ -15,10 +14,12 @@ where

import Control.Monad ((>=>))
import Data.Bifunctor
import Data.ByteString (ByteString)
import Data.Pool (Pool)
import Data.Text
import Data.Text (Text)
import Data.Text.Display (Display (..))
import Data.Time (NominalDiffTime)
import Data.Typeable
import Data.Word (Word16)
import Database.PostgreSQL.Simple qualified as PG
import Env
Expand All @@ -37,6 +38,16 @@ import Env
import GHC.Generics
import Text.Read (readMaybe)

data ConnectionInfo = ConnectionInfo
{ connectHost :: Text
, connectPort :: Word16
, connectUser :: Text
, connectPassword :: Text
, connectDatabase :: Text
, sslMode :: Text
}
deriving (Generic, Eq, Read, Show, Typeable)

data DeploymentEnv
= Production
| Development
Expand Down Expand Up @@ -67,7 +78,7 @@ data LoggingEnv = LoggingEnv
-- | The datatype that is used to model the external configuration
data FloraConfig = FloraConfig
{ dbConfig :: PoolConfig
, connectInfo :: PG.ConnectInfo
, connectionInfo :: ByteString
, domain :: Text
, httpPort :: Word16
, logging :: LoggingEnv
Expand All @@ -84,7 +95,7 @@ data PoolConfig = PoolConfig
data TestConfig = TestConfig
{ httpPort :: Word16
, dbConfig :: PoolConfig
, connectInfo :: PG.ConnectInfo
, connectionInfo :: ByteString
}
deriving stock (Generic)

Expand All @@ -94,14 +105,9 @@ data TestEnv = TestEnv
}
deriving stock (Generic)

parseConnectInfo :: Parser Error PG.ConnectInfo
parseConnectInfo =
PG.ConnectInfo
<$> var str "FLORA_DB_HOST" (help "PostgreSQL host")
<*> var port "FLORA_DB_PORT" (help "PostgreSQL port")
<*> var str "FLORA_DB_USER" (help "PostgreSQL user")
<*> var str "FLORA_DB_PASSWORD" (help "PostgreSQL password")
<*> var str "FLORA_DB_DATABASE" (help "Control-Plane database")
parseConnectionInfo :: Parser Error ByteString
parseConnectionInfo =
var str "FLORA_DB_CONNSTRING" (help "libpq-compatible connection string")

parsePoolConfig :: Parser Error PoolConfig
parsePoolConfig =
Expand All @@ -127,13 +133,13 @@ parseDomain = var str "FLORA_DOMAIN" (help "URL domain for Flora")

parseDeploymentEnv :: Parser Error DeploymentEnv
parseDeploymentEnv =
var deploymentEnv "FLORA_ENVIRONMENT" (help "Name of the current environment (production, development)")
var deploymentEnv "FLORA_ENVIRONMENT" (help "Name of the current environment (production, development, test)")

parseConfig :: Parser Error FloraConfig
parseConfig =
FloraConfig
<$> parsePoolConfig
<*> parseConnectInfo
<*> parseConnectionInfo
<*> parseDomain
<*> parsePort
<*> parseLoggingEnv
Expand All @@ -144,7 +150,7 @@ parseTestConfig =
TestConfig
<$> parsePort
<*> parsePoolConfig
<*> parseConnectInfo
<*> parseConnectionInfo

-- Env parser helpers

Expand Down

0 comments on commit 72827d6

Please sign in to comment.