diff --git a/apps/passport-server/src/database/postgresConfiguration.ts b/apps/passport-server/src/database/postgresConfiguration.ts index 8b84afb246..e07f96af77 100644 --- a/apps/passport-server/src/database/postgresConfiguration.ts +++ b/apps/passport-server/src/database/postgresConfiguration.ts @@ -1,3 +1,4 @@ +import { isNaN, parseInt } from "lodash"; import { ClientConfig } from "pg"; import { PoolOptionsExplicit, SslSettings } from "postgres-pool"; @@ -24,6 +25,12 @@ export function getDatabaseConfiguration( if (process.env.DATABASE_DB_NAME === undefined) { throw new Error("Missing environment variable: DATABASE_DB_NAME"); } + if ( + process.env.DATABASE_PORT !== undefined && + isNaN(process.env.DATABASE_PORT) + ) { + throw new Error("Invalid environment variable: DATABASE_PORT"); + } if (!["true", "false"].includes(process.env.DATABASE_SSL || "")) { throw new Error("Missing or incorrect env variable: DATABASE_SSL"); } @@ -49,7 +56,7 @@ export function getDatabaseConfiguration( password: process.env.DATABASE_PASSWORD, host: process.env.DATABASE_HOST, database: process.env.DATABASE_DB_NAME, - port: 5432, + port: parseInt(process.env.DATABASE_PORT ?? "5432"), ssl: process.env.DATABASE_SSL === "true" ? { rejectUnauthorized: false } diff --git a/apps/passport-server/src/types.ts b/apps/passport-server/src/types.ts index 5c6f27584e..106baa538e 100644 --- a/apps/passport-server/src/types.ts +++ b/apps/passport-server/src/types.ts @@ -104,6 +104,7 @@ export interface APIs { export interface EnvironmentVariables { MAILGUN_API_KEY?: string; DATABASE_USERNAME?: string; + DATABASE_PORT?: string; DATABASE_PASSWORD?: string; DATABASE_HOST?: string; DATABASE_DB_NAME?: string; diff --git a/scripts/db-up-zupass.sh b/scripts/db-up-zupass.sh new file mode 100755 index 0000000000..90b16fbfae --- /dev/null +++ b/scripts/db-up-zupass.sh @@ -0,0 +1,42 @@ +#!/bin/bash +PORT=${1:-5432} +if ! [[ $PORT =~ ^[0-9]+$ && $PORT -gt 0 ]]; then + echo "Error: $PORT is not a port number." >&2 + exit 1 +fi + + +# Get a list of all listening port numbers +ports=$(sudo lsof -i -P -n | grep LISTEN | awk '{print $(NF - 1)}' | awk -F':' '{print $NF}') + +# Convert the list into an array +port_array=($ports) + +# Print the list of ports +for port in "${port_array[@]}"; do + if [[ $PORT -eq $port ]]; then + echo "Error: $PORT is taken." >&2 + exit 1 + fi +done + +ENV_FILE="./apps/passport-server/.env" + +# Check if .env file exists +if [[ ! -f $ENV_FILE ]]; then + echo "Error: $ENV_FILE does not exist." >&2 + exit 1 +fi + +# Update DATABASE_PORT in the .env file or add it if it doesn't exist +if grep -q "^DATABASE_PORT=" "$ENV_FILE"; then + # Update the existing DATABASE_PORT line + sed -i '' "s/^DATABASE_PORT=.*/DATABASE_PORT=$PORT/" "$ENV_FILE" + echo "Updated DATABASE_PORT to $PORT in $ENV_FILE." +else + # Add DATABASE_PORT to the .env file + echo "DATABASE_PORT=$PORT" >> "$ENV_FILE" + echo "Added DATABASE_PORT=$PORT to $ENV_FILE." +fi + +pg_ctl -D apps/passport-server/local-db-data -l apps/passport-server/local-db-log -o "-p $PORT" start diff --git a/turbo.json b/turbo.json index 5fafbc8b38..e1f8e039be 100644 --- a/turbo.json +++ b/turbo.json @@ -119,6 +119,7 @@ "EMBEDDED_ZAPPS", "DEVCON_TICKET_QUERY_ORIGINS", "GENERATED_CHUNKS", + "DATABASE_PORT", "TELEGRAM_CHAT_TO_PRODUCT_IDS", "IGNORE_NON_PRIORITY_FEEDS", "PRIORITY_FEED_PROVIDER_URLS",