Clean up imports and .env variables

This commit is contained in:
Keannu Christian Bernasol 2024-08-30 15:46:30 +08:00
parent 71eda8a496
commit 658753dea4
4 changed files with 51 additions and 58 deletions

View file

@ -1,15 +1,3 @@
"""
Django settings for test backend project.
Generated by 'django-admin startproject' using Django 4.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from datetime import timedelta
from pathlib import Path
from dotenv import load_dotenv, find_dotenv # Python dotenv
@ -21,17 +9,19 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# Root folder where docker-compose.yml is located
ROOT_DIR = Path(__file__).resolve().parent.parent.parent
# If you're hosting this on the cloud, have this set
CLOUD = bool(os.getenv('CLOUD', False))
# If you're hosting this with a secret provider, have this set to True
USE_VAULT = bool(os.getenv('USE_VAULT', False) == 'True')
# Have this set to True to serve media and static contents directly via Django
SERVE_MEDIA = bool(os.getenv('SERVE_MEDIA', False) == 'True')
load_dotenv(find_dotenv())
def get_secret(secret_name):
if CLOUD:
if USE_VAULT:
try:
pass
# Add specific implementations here if deploying to Azure, GCP, or AWS to get secrets
# Add specific implementations here if deploying to Azure, GCP, or AWS to get secrets from vault
except:
secret_value = ""
else:
@ -44,25 +34,18 @@ def get_secret(secret_name):
return secret_value
# Frontend Domain
DOMAIN = get_secret('DOMAIN')
# Backend Domain
BACKEND_DOMAIN = get_secret('BACKEND_DOMAIN')
# URL Prefixes
USE_HTTPS = (get_secret('USE_HTTPS') == 'True')
URL_PREFIX = 'https://' if CLOUD and USE_HTTPS else 'http://'
BACKEND_URL = f'{URL_PREFIX}{BACKEND_DOMAIN}'
FRONTEND_URL = f'{URL_PREFIX}{DOMAIN}'
URL_PREFIX = 'https://' if USE_HTTPS else 'http://'
BACKEND_URL = URL_PREFIX + get_secret('BACKEND_URL')
FRONTEND_URL = URL_PREFIX + get_secret('FRONTEND_URL')
ALLOWED_HOSTS = ['*']
CSRF_TRUSTED_ORIGINS = [
BACKEND_URL,
FRONTEND_URL
# You can also set up https://*.name.xyz for wildcards here
]
if CLOUD:
# TODO: If you require additional URLs to be trusted in cloud service providers, add them here
CSRF_TRUSTED_ORIGINS += []
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = (get_secret('BACKEND_DEBUG') == 'True')
@ -142,17 +125,18 @@ MIDDLEWARE = [
# https://docs.djangoproject.com/en/4.2/howto/static-files/
ROOT_URLCONF = 'config.urls'
if CLOUD:
if SERVE_MEDIA:
# Cloud Storage Settings
# This is assuming you use the same bucket for media and static containers
CLOUD_BUCKET = get_secret('CLOUD_BUCKET')
CLOUD_BUCKET_CONTAINER = get_secret('CLOUD_BUCKET_CONTAINER')
CLOUD_STATIC_CONTAINER = get_secret('CLOUD_STATIC_CONTAINER')
MEDIA_CONTAINER = get_secret('MEDIA_CONTAINER')
STATIC_CONTAINER = get_secret('STATIC_CONTAINER')
MEDIA_URL = f'https://{CLOUD_BUCKET}/{CLOUD_BUCKET_CONTAINER}/'
MEDIA_URL = f'https://{CLOUD_BUCKET}/{MEDIA_CONTAINER}/'
MEDIA_ROOT = f'https://{CLOUD_BUCKET}/'
STATIC_URL = f'https://{CLOUD_BUCKET}/{CLOUD_STATIC_CONTAINER}/'
STATIC_ROOT = f'https://{CLOUD_BUCKET}/{CLOUD_STATIC_CONTAINER}/'
STATIC_URL = f'https://{CLOUD_BUCKET}/{STATIC_CONTAINER}/'
STATIC_ROOT = f'https://{CLOUD_BUCKET}/{STATIC_CONTAINER}/'
# Consult django-storages documentation when filling in these values. This will vary depending on your cloud service provider
STORAGES = {
@ -235,7 +219,7 @@ WSGI_APPLICATION = 'config.wsgi.application'
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
# Have this set to True if you're using a connection bouncer
# Set to True to avoid issues with pgbouncer when pooling connections
'DISABLE_SERVER_SIDE_CURSORS': True,
"NAME": get_secret("DB_DATABASE"),
"USER": get_secret("DB_USERNAME"),
@ -247,6 +231,16 @@ DATABASES = {
},
}
}
# Django Cache
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{get_secret('REDIS_HOST')}:{get_secret('REDIS_PORT')}/2",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
AUTH_USER_MODEL = 'accounts.CustomUser'
@ -314,7 +308,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Manila'
TIME_ZONE = get_secret('TIMEZONE')
USE_I18N = True
@ -330,7 +324,7 @@ SITE_NAME = 'DRF-Template'
# JWT Token Lifetimes
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(hours=6),
"ACCESS_TOKEN_LIFETIME": timedelta(hours=1),
"REFRESH_TOKEN_LIFETIME": timedelta(days=3)
}
@ -373,14 +367,3 @@ DATA_UPLOAD_MAX_NUMBER_FIELDS = 20480
GRAPH_MODELS = {
'app_labels': ['accounts', 'user_groups', 'billing', 'emails', 'payments', 'subscriptions']
}
# Django/DRF Cache
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{get_secret('REDIS_HOST')}:{get_secret('REDIS_PORT')}/2",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}