diff --git a/.gitignore b/.gitignore index 8f0678c..337241a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /node_modules +.env diff --git a/configuration/base.yaml b/configuration/base.yaml new file mode 100644 index 0000000..d4d65a1 --- /dev/null +++ b/configuration/base.yaml @@ -0,0 +1,4 @@ +email_client: + timeout_milliseconds: 10000 + base_url: "https://api.alphonsepaix.xyz" + sender_email: "newsletter@alphonsepaix.xyz" diff --git a/configuration/local.yaml b/configuration/local.yaml new file mode 100644 index 0000000..8a2c03e --- /dev/null +++ b/configuration/local.yaml @@ -0,0 +1,16 @@ +application: + port: 8080 + host: "127.0.0.1" + base_url: "http://127.0.0.1:8080" +database: + host: "127.0.0.1" + port: 5432 + database_name: "newsletter" + username: "postgres" + password: "password" + require_ssl: false + timeout_milliseconds: 1000 +email_client: + authorization_token: "secret-token" +redis_uri: "redis://127.0.0.1:6379" +require_tls: false diff --git a/configuration/production.yaml b/configuration/production.yaml new file mode 100644 index 0000000..3d2b415 --- /dev/null +++ b/configuration/production.yaml @@ -0,0 +1,4 @@ +application: + host: "0.0.0.0" +database: + timeout_milliseconds: 500 diff --git a/scripts/init_db.sh b/scripts/init_db.sh new file mode 100755 index 0000000..44e9410 --- /dev/null +++ b/scripts/init_db.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +set -x +set -eo pipefail + +if ! [ -x "$(command -v psql)" ]; then + echo >&2 "Error: psql is not installed." + exit 1 +fi + +if ! [ -x "$(command -v sqlx)" ]; then + echo >&2 "Error: sqlx is not installed." + exit 1 +fi + +DB_USER="${POSTGRES_USER:=postgres}" +DB_PASSWORD="${POSTGRES_PASSWORD:=password}" +DB_NAME="${POSTGRES_DB:=newsletter}" +DB_PORT="${POSTGRES_PORT:=5432}" +DB_HOST="${POSTGRES_HOST:=localhost}" + +if [[ -z "${SKIP_DOCKER}" ]]; then + docker run \ + -e POSTGRES_USER=${DB_USER} \ + -e POSTGRES_PASSWORD=${DB_PASSWORD} \ + -e POSTGRES_DB=${DB_NAME} \ + -p "127.0.0.1:${DB_PORT}":5432 \ + -d postgres \ + postgres -N 1000 +fi + +export PGPASSWORD="${DB_PASSWORD}" +until psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do + >&2 echo "Postgres is still unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" + +DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME} +export DATABASE_URL +sqlx database create +sqlx migrate run + +>&2 echo "Postgres has been migrated, ready to go!" diff --git a/src/configuration.rs b/src/configuration.rs index fa7a434..4bd9673 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -110,7 +110,7 @@ pub struct DatabaseSettings { pub host: String, pub database_name: String, pub require_ssl: bool, - pub timeout_millis: u64, + pub timeout_milliseconds: u64, } impl DatabaseSettings { diff --git a/src/startup.rs b/src/startup.rs index 6fb54bf..607e965 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -38,7 +38,9 @@ impl Application { configuration.application.host, configuration.application.port ); let connection_pool = PgPoolOptions::new() - .acquire_timeout(Duration::from_millis(configuration.database.timeout_millis)) + .acquire_timeout(Duration::from_millis( + configuration.database.timeout_milliseconds, + )) .connect_lazy_with(configuration.database.with_db()); let email_client = EmailClient::build(configuration.email_client).unwrap(); let pool = Pool::new( diff --git a/tests/api/subscriptions.rs b/tests/api/subscriptions.rs index 23a4122..69153c0 100644 --- a/tests/api/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -108,5 +108,11 @@ async fn subscribe_fails_if_there_is_a_fatal_database_error() { let response = app.post_subscriptions(body).await; - assert_eq!(response.status().as_u16(), 500); + assert!( + response + .text() + .await + .unwrap() + .contains("An internal server error occured") + ); }