Skip to content

Commit

Permalink
chore(feat): add env handling
Browse files Browse the repository at this point in the history
ndu committed Dec 27, 2024
1 parent 7c70a9c commit 91c8b4f
Showing 9 changed files with 128 additions and 40 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dumps/
.venv/
.env
.env.local
.env.production
__pycache__/
test.py
test.json
@@ -16,4 +18,6 @@ test.rest
db.sqlite3
old-backup-db.sqlite3
dump.rdp
client/
client/

switch-env.sh
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -76,7 +76,29 @@ Before running the application, ensure you have the following:
EMAIL_HOST_PASSWORD = 'enter password' #for Gmail, generate app password
```

5. Run the application in development mode:
5. Environment Switching: Use this script (switch-env.sh) to easily switch between environments:
```sh
#!/bin/bash
if [ "$1" == "local" ]; then
cp .env.local .env
echo "Switched to local environment"
elif [ "$1" == "prod" ]; then
cp .env.production .env
echo "Switched to production environment"
else
echo "Please specify environment: local or prod"
fi
```
Make it executable and use:

```sh
chmod +x switch-env.sh
./switch-env.sh local # Switch to local environment
./switch-env.sh prod # Switch to production environment
```


6. Run the application:

- Start Server:

7 changes: 5 additions & 2 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Environment Settings
# Use core.config.local for Dev environment and core.config.production for production environment
DJANGO_SETTINGS_MODULE='core.config.local'
DJANGO_SETTINGS_MODULE='core.config.local' # Changes based on environment

# Sqlite3 database config
SECRET_KEY='paste db.sqlite3 key here'
@@ -13,9 +13,12 @@ DB_PASS=<enter password>
DB_HOST=localhost
DB_PORT=5432

# Host Settings
ALLOWED_HOSTS=localhost,127.0.0.1
SITE_URL='http://localhost:8000'

# OpenAI
# API Keys
TINYMCE_API_KEY=xxxxxx
OPENAI_API_KEY=xxxxxx

# Cloudinary
5 changes: 3 additions & 2 deletions server/core/asgi.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.config.local')
settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', 'core.config.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)

application = get_asgi_application()
application = get_asgi_application()
46 changes: 44 additions & 2 deletions server/core/config/local.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
from .base import *

# Development-specific settings
DEBUG = True

# Media settings
MEDIA_URL = '/media/'

# Development hosts
ALLOWED_HOSTS = ['localhost', '127.0.0.1']

# CSRF settings for local development
CSRF_TRUSTED_ORIGINS = [
'http://127.0.0.1:8000',
'http://localhost:8000',
'http://localhost:4321',
]
]

CORS_ALLOW_CREDENTIALS = True

# Database settings (you might want to use SQLite for local development)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DB_NAME', default='your_local_db_name'),
'USER': config('DB_USER', default='your_local_db_user'),
'PASSWORD': config('DB_PASS', default='your_local_db_password'),
'HOST': config('DB_HOST', default='localhost'),
'PORT': config('DB_PORT', default='5432'),
}
}

# Security settings appropriate for development
CSRF_COOKIE_SECURE = False
SESSION_COOKIE_SECURE = False
SECURE_SSL_REDIRECT = False

# Content Security Policy settings for development
CSP_DEFAULT_SRC = ("'self'",)
CSP_STYLE_SRC = ("'self'",)
CSP_SCRIPT_SRC = ("'self'", "blob:")
CSP_IMG_SRC = ("'self'", "blob:")
CSP_MEDIA_SRC = ("'self'", "blob:")
CSP_CONNECT_SRC = ("'self'", "blob:")
CSP_FONT_SRC = ("'self'",)

# Static files
STATIC_URL = 'static/'

# File upload settings
DATA_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB
FILE_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB
64 changes: 38 additions & 26 deletions server/core/config/production.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
from .base import *

MEDIA_URL = 'https://cms.2077.xyz/media/'
MEDIA_URL = "https://cms.2077.xyz/media/"

DEBUG = bool(config('DJANGO_DEBUG', default=False))
DEBUG = bool(config("DJANGO_DEBUG", default=False))

ALLOWED_HOSTS = ['74.119.195.253', 'cms.2077.xyz']
ALLOWED_HOSTS = config(
"ALLOWED_HOSTS",
default="74.119.195.253,cms.2077.xyz",
cast=lambda v: [s.strip() for s in v.split(",")],
)

# Add localhost for testing production settings locally
if DEBUG:
ALLOWED_HOSTS.extend(["localhost", "127.0.0.1"])

CSRF_TRUSTED_ORIGINS = []
for host in ALLOWED_HOSTS:
CSRF_TRUSTED_ORIGINS.append(f"https://{host}")
CSRF_TRUSTED_ORIGINS.append(f"http://{host}")

CORS_ALLOWED_ORIGINS = [
"https://cms.2077.xyz", # Https version of Django
"http://74.119.195.253", # Http version of Astro
"http://127.0.0.1:4321", # Local Dev
"http://localhost:4321", # Local Dev
]
"https://cms.2077.xyz", # Https version of Django
"http://74.119.195.253",
]

CORS_ALLOW_CREDENTIALS = True

#REDISCLOUD_URL = config("REDISCLOUD_URL")
# REDISCLOUD_URL = config("REDISCLOUD_URL")

STATIC_URL = 'staticfiles/'
STATIC_URL = "staticfiles/"

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASS'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"USER": config("DB_USER"),
"PASSWORD": config("DB_PASS"),
"HOST": config("DB_HOST"),
"PORT": config("DB_PORT"),
}
}

CSP_DEFAULT_SRC = ("self",)
CSP_DEFAULT_SRC = ("'self'",)

CSP_STYLE_SRC = ("self",)
CSP_STYLE_SRC = ("'self'",)

CSP_SCRIPT_SRC = ("self", "blob:")
CSP_SCRIPT_SRC = ("'self'", "blob:")

CSP_IMG_SRC = ("self", "http://cms.2077.xyz", "https://cms.2077.xyz", "http://74.119.195.253", "blob:")
CSP_IMG_SRC = (
"'self'",
"http://cms.2077.xyz",
"https://cms.2077.xyz",
"http://74.119.195.253",
"blob:",
)

CSP_MEDIA_SRC = ("'self'", "blob:")

CSP_CONNECT_SRC = ("'self'", "blob:")

CSP_FONT_SRC = ("self",)
CSP_FONT_SRC = ("'self'",)

CSRF_COOKIE_SECURE = True

@@ -57,15 +69,15 @@

SECURE_HSTS_PRELOAD = True

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

SECURE_SSL_REDIRECT = False

SESSION_COOKIE_SECURE = True

#PROXY_SETTING for Astro server
# PROXY_SETTING for Astro server
PROXY_ASTERISK = True

DATA_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB
DATA_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB

FILE_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB
FILE_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 MB
5 changes: 3 additions & 2 deletions server/core/wsgi.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.config.local')
settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', 'core.config.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)

application = get_wsgi_application()
application = get_wsgi_application()
10 changes: 6 additions & 4 deletions server/manage.py
Original file line number Diff line number Diff line change
@@ -5,10 +5,12 @@
from dotenv import load_dotenv

load_dotenv()
print(os.environ.get('DJANGO_SETTINGS_MODULE'))
print(os.environ.get("DJANGO_SETTINGS_MODULE"))

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.config.local')
settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', 'core.config.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
@@ -20,5 +22,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ kombu==5.4.0
openai==1.57.4
pillow==10.4.0
prompt_toolkit==3.0.47
psycopg2-binary==2.9.10
pycparser==2.22
pycryptodome==3.21.0
pydantic==2.10.3

0 comments on commit 91c8b4f

Please sign in to comment.