Overhaul run.sh scripts

- Default environment variables through this, instead of .env
- Added some basic checks to ensure developers don't stub their toe
- Use `railway link`, injecting environment variables without insecure dotenv files
This commit is contained in:
2024-11-01 01:03:56 -05:00
parent f034b41da1
commit 5390fb57a7
2 changed files with 67 additions and 1 deletions

View File

@@ -1,3 +1,62 @@
#!/usr/bin/env bash
poetry run hypercorn linkpulse.app:app --reload
# Check whether CWD is 'backend'
if [ "$(basename "$(pwd)")" != "backend" ]; then
echo "error: This script must be run from the 'backend' directory."
exit 1
fi
# Default to development mode if not defined
export ENVIRONMENT=${ENVIRONMENT:-development}
COMMAND='poetry run hypercorn linkpulse.app:app --reload'
# Check if Railway CLI is available
RAILWAY_AVAILABLE=false
if command -v railway &> /dev/null; then
RAILWAY_AVAILABLE=true
fi
# Check if .env file exists
ENV_FILE_EXISTS=false
if [ -f .env ]; then
ENV_FILE_EXISTS=true
fi
# Check if DATABASE_URL is defined
DATABASE_DEFINED=false
if [ -n "$DATABASE_URL" ]; then
DATABASE_DEFINED=true
else
if $ENV_FILE_EXISTS; then
if grep -E '^DATABASE_URL=.+' .env &> /dev/null; then
DATABASE_DEFINED=true
fi
fi
fi
# Check if Railway project is linked
PROJECT_LINKED=false
if $RAILWAY_AVAILABLE; then
if railway status &> /dev/null; then
PROJECT_LINKED=true
fi
fi
if $DATABASE_DEFINED; then
$COMMAND
else
if $RAILWAY_AVAILABLE; then
if $PROJECT_LINKED; then
DATABASE_URL="$(railway variables -s Postgres --json | jq .DATABASE_PUBLIC_URL -cMr)" $COMMAND
else
echo "error: Railway project not linked."
echo "Run 'railway link' to link the project."
exit 1
fi
else
echo "error: Could not find DATABASE_URL environment variable."
echo "Install the Railway CLI and link the project, or create a .env file with a DATABASE_URL variable."
exit 1
fi
fi

View File

@@ -1,3 +1,10 @@
#!/usr/bin/env bash
# Check whether CWD is 'frontend'
if [ "$(basename "$(pwd)")" != "frontend" ]; then
echo "error: This script must be run from the 'frontend' directory."
exit 1
fi
export VITE_BACKEND_TARGET=${VITE_BACKEND_TARGET:-localhost:8000}
pnpm run dev