Skip to content

Commit

Permalink
chore(feat): add fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ndu committed Dec 27, 2024
1 parent e059b8e commit 7a9134a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ Before running the application, ensure you have the following:
./switch-env.sh prod # Switch to production environment
```

### What the Script Does:
- Validates the Environment Name: Ensures only local or production environments are specified.

- Checks for Required Variables: Validates that the environment file (e.g., .env.production) contains all required variables (DJANGO_SETTINGS_MODULE, SECRET_KEY, and DEBUG).

- Backs Up the Existing .env File: If a .env file already exists, it is backed up with a timestamp.

- Switches the Environment: Copies the specified environment file to .env.

### Example:
```sh
./switch-env.sh production
```
### Output:
- **If successful**:
```sh
Existing .env backed up to .env.backup.20231025_123456
Successfully switched to production environment.
```
- If a required variable is missing:
```sh
Error: Missing required variable DEBUG in .env.production
```

### Required Variables:
Ensure your environment files (e.g., .env.local, .env.production) include the following variables:
```sh
DJANGO_SETTINGS_MODULE=core.config.local # or core.config.production for production
SECRET_KEY=your-secret-key-here
DEBUG=True # or False for production
```
For production, it is recommended to set DEBUG=False for security reasons.


6. Run the application:

Expand Down Expand Up @@ -164,4 +197,4 @@ If you want to contribute to this project, please read the [contribution guide](
- "Module not found" error: Check your dependencies and installation
- "React-Scripts Dependencies error" : Install using `--legacy-peer-deps`

Working in Progress...stay tuned
Work in Progress...stay tuned
10 changes: 7 additions & 3 deletions server/core/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

load_dotenv()

from django.core.asgi import get_asgi_application
# Ensure DJANGO_SETTINGS_MODULE is set
if not os.getenv('DJANGO_SETTINGS_MODULE'):
raise ValueError('DJANGO_SETTINGS_MODULE environment variable is not set')

# Set the default Django settings module for the ASGI application.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.getenv('DJANGO_SETTINGS_MODULE'))

settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', 'core.config.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
from django.core.asgi import get_asgi_application

application = get_asgi_application()
6 changes: 5 additions & 1 deletion server/core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

load_dotenv()

# Ensure DJANGO_SETTINGS_MODULE is set
if not os.getenv('DJANGO_SETTINGS_MODULE'):
raise ValueError('DJANGO_SETTINGS_MODULE environment variable is not set')

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.getenv('DJANGO_SETTINGS_MODULE', 'core.config.production'))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.getenv('DJANGO_SETTINGS_MODULE'))

app = Celery('core')

Expand Down
10 changes: 7 additions & 3 deletions server/core/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

load_dotenv()

from django.core.wsgi import get_wsgi_application
# Ensure DJANGO_SETTINGS_MODULE is set
if not os.getenv('DJANGO_SETTINGS_MODULE'):
raise ValueError('DJANGO_SETTINGS_MODULE environment variable is not set')

# Set the default Django settings module for the WSGI application.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.getenv('DJANGO_SETTINGS_MODULE'))

settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', 'core.config.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
24 changes: 23 additions & 1 deletion switch-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@ check_file() {
fi
}

# Function to validate environment file contents
validate_env_file() {
local file="$1"
local required_vars=("DJANGO_SETTINGS_MODULE" "SECRET_KEY")

for var in "${required_vars[@]}"; do
if ! grep -q "^${var}=" "$file"; then
echo "Error: Missing required variable $var in $file"
exit 1
fi
done

# Optional: Warn if DEBUG is not set in production
if [[ "$1" == "production" ]] && ! grep -q "^DEBUG=" "$file"; then
echo "Warning: DEBUG is not set in .env.production. Defaulting to False."
echo "DEBUG=False" >> "$file"
fi
}

# Main script
if [[ -z "$1" ]]; then
echo "Error: No environment specified. Please specify 'local' or 'prod'."
echo "Error: No environment specified. Please specify 'local' or 'production'."
exit 1
fi

Expand All @@ -27,6 +46,9 @@ validate_env "$1"
source_file=".env.$1"
check_file "$source_file"

# Validate the environment file contents
validate_env_file "$source_file"

# Backup existing .env if it exists
if [[ -f .env ]]; then
backup_file=".env.backup.$(date +%Y%m%d_%H%M%S)"
Expand Down

0 comments on commit 7a9134a

Please sign in to comment.