Skip to content

Commit d6aa2fc

Browse files
committed
Config - Load CORS_ALLOWED_ORIGINS from ENV
1 parent c46d320 commit d6aa2fc

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

django-api/core/settings.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@
1010
https://docs.djangoproject.com/en/3.2/ref/settings/
1111
"""
1212

13-
import os
13+
import os, environ
1414
from pathlib import Path
1515

16+
env = environ.Env(
17+
# set casting, default value
18+
DEBUG=(bool, False)
19+
)
20+
1621
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1722
BASE_DIR = Path(__file__).resolve().parent.parent
1823

1924
# Quick-start development settings - unsuitable for production
2025
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
2126

2227
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = os.getenv(
24-
"SECRET_KEY", "django-insecure-97s)x3c8w8h_qv3t3s7%)#k@dpk2edr0ed_(rq9y(rbb&_!ai%"
25-
)
28+
SECRET_KEY = env('SECRET_KEY', default='insecure-S#perS3crEt_007')
2629

2730
# SECURITY WARNING: don't run with debug turned on in production!
28-
DEBUG = int(os.environ.get("DEBUG", default=1))
31+
DEBUG = int(env("DEBUG", default=0))
2932

30-
ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(" ")
33+
ALLOWED_HOSTS = env("DJANGO_ALLOWED_HOSTS", default="*").split(" ")
3134

3235
# Application definition
3336

@@ -81,12 +84,12 @@
8184

8285
DATABASES = {
8386
"default": {
84-
"ENGINE": os.environ.get("DB_ENGINE", "django.db.backends.sqlite3"),
85-
"NAME": os.environ.get("DB_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
86-
"USER": os.environ.get("DB_USER"),
87-
"PASSWORD": os.environ.get("DB_PASSWORD"),
88-
"HOST": os.environ.get("DB_HOST"),
89-
"PORT": os.environ.get("DB_PORT"),
87+
"ENGINE" : env("DB_ENGINE" , default="django.db.backends.sqlite3"),
88+
"NAME" : env("DB_DATABASE", default=os.path.join(BASE_DIR, "db.sqlite3")),
89+
"USER" : env("DB_USER" , default=None),
90+
"PASSWORD": env("DB_PASSWORD", default=None),
91+
"HOST" : env("DB_HOST" , default=None),
92+
"PORT" : env("DB_PORT" , default=None),
9093
}
9194
}
9295

@@ -131,6 +134,7 @@
131134

132135
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
133136

137+
# Custom user Model
134138
AUTH_USER_MODEL = "api_user.User"
135139

136140
# ##################################################################### #
@@ -148,8 +152,15 @@
148152
# ################### CORS ############################### #
149153
# ##################################################################### #
150154

155+
# Load the default ones
151156
CORS_ALLOWED_ORIGINS = ["http://localhost:3000", "http://127.0.0.1:3000"]
152157

158+
# Leaded from Environment
159+
CORS_ALLOWED_ORIGINS_ENV = env("CORS_ALLOWED_ORIGINS", default=None)
160+
161+
if CORS_ALLOWED_ORIGINS_ENV:
162+
CORS_ALLOWED_ORIGINS += CORS_ALLOWED_ORIGINS_ENV.split(' ')
163+
153164
# ##################################################################### #
154165
# ################### TESTING ############################### #
155166
# ##################################################################### #

0 commit comments

Comments
 (0)