|
14 | 14 | from datetime import timedelta |
15 | 15 | from pathlib import Path |
16 | 16 |
|
17 | | -from corsheaders.defaults import default_headers |
18 | 17 | from dotenv import load_dotenv |
19 | 18 |
|
20 | 19 | # Build paths inside the project like this: BASE_DIR / 'subdir'. |
21 | | -BASE_DIR = Path(__file__).resolve().parent.parent.parent |
| 20 | +BASE_DIR = Path(__file__).resolve().parent.parent |
22 | 21 |
|
23 | 22 | dotenv_path = BASE_DIR / ".env" |
24 | 23 | load_dotenv(dotenv_path, override=True) |
|
29 | 28 | # SECURITY WARNING: keep the secret key used in production secret! |
30 | 29 | SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "fallback-secret-key-for-dev") |
31 | 30 |
|
| 31 | +# Environment |
| 32 | +ENV = os.getenv("ENV", "local") |
| 33 | + |
32 | 34 | # SECURITY WARNING: don't run with debug turned on in production! |
33 | | -DEBUG = True |
| 35 | +DEBUG = ENV == "local" |
34 | 36 |
|
35 | 37 | ALLOWED_HOSTS: list = os.getenv("DJANGO_ALLOWED_HOSTS", "localhost").split(",") |
36 | 38 |
|
|
45 | 47 | "django.contrib.messages", |
46 | 48 | "django.contrib.staticfiles", |
47 | 49 | "rest_framework", |
48 | | - "corsheaders", |
49 | 50 | "api", |
50 | 51 | ] |
51 | 52 |
|
52 | 53 | MIDDLEWARE = [ |
53 | | - "corsheaders.middleware.CorsMiddleware", |
| 54 | + "whitenoise.middleware.WhiteNoiseMiddleware", |
54 | 55 | "django.middleware.security.SecurityMiddleware", |
55 | 56 | "django.contrib.sessions.middleware.SessionMiddleware", |
56 | 57 | "django.middleware.common.CommonMiddleware", |
|
60 | 61 | "django.middleware.clickjacking.XFrameOptionsMiddleware", |
61 | 62 | ] |
62 | 63 |
|
63 | | -CORS_ALLOWED_ORIGINS = [ |
64 | | - "https://nextshape.onrender.com", # prod |
65 | | - "https://nextshape-dev.onrender.com", # dev |
66 | | - "http://localhost:5173", |
67 | | - "http://127.0.0.1:5173", |
68 | | -] |
69 | | - |
70 | | -CORS_ALLOW_CREDENTIALS = True |
71 | | -CORS_ALLOW_HEADERS = list(default_headers) + [ |
72 | | - "content-type", |
73 | | -] |
74 | | - |
75 | | -CORS_ALLOW_METHODS = [ |
76 | | - "DELETE", |
77 | | - "GET", |
78 | | - "OPTIONS", |
79 | | - "PATCH", |
80 | | - "POST", |
81 | | - "PUT", |
82 | | -] |
83 | | - |
84 | | -ENV = os.getenv("ENV", "local") |
85 | | - |
86 | | -if ENV == "local": |
87 | | - CORS_ALLOWED_ORIGINS = [ |
88 | | - "http://localhost:5173", |
89 | | - ] |
90 | | - |
91 | | -elif ENV == "dev": |
92 | | - CORS_ALLOWED_ORIGINS = [ |
93 | | - "http://localhost:5173", |
94 | | - "https://nextshape-dev.onrender.com", |
95 | | - "https://nextshape.onrender.com", # prod front calls back dev for the current moment |
96 | | - ] |
97 | | -elif ENV == "prod": |
98 | | - CORS_ALLOWED_ORIGINS = ["https://nextshape.onrender.com"] |
99 | 64 |
|
100 | 65 | ROOT_URLCONF = "next_shape_ws.urls" |
101 | 66 |
|
102 | 67 | TEMPLATES = [ |
103 | 68 | { |
104 | 69 | "BACKEND": "django.template.backends.django.DjangoTemplates", |
105 | | - "DIRS": [], |
| 70 | + "DIRS": [os.path.join(BASE_DIR, "UI", "dist")], |
106 | 71 | "APP_DIRS": True, |
107 | 72 | "OPTIONS": { |
108 | 73 | "context_processors": [ |
|
192 | 157 | # Static files (CSS, JavaScript, Images) |
193 | 158 | # https://docs.djangoproject.com/en/5.1/howto/static-files/ |
194 | 159 |
|
195 | | -STATIC_URL = "static/" |
| 160 | +STATIC_URL = "/assets/" |
| 161 | +MEDIA_URL = "/media/" |
| 162 | + |
| 163 | +STATICFILES_DIRS = [BASE_DIR / "UI" / "dist"] |
| 164 | +STATIC_ROOT = BASE_DIR / "staticfiles" |
| 165 | +MEDIA_ROOT = BASE_DIR / "media" |
196 | 166 |
|
197 | 167 | # Default primary key field type |
198 | 168 | # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field |
|
201 | 171 |
|
202 | 172 |
|
203 | 173 | # JWT Settings |
204 | | - |
205 | 174 | SIMPLE_JWT = { |
206 | 175 | "ACCESS_TOKEN_LIFETIME": timedelta(minutes=30), |
207 | 176 | "REFRESH_TOKEN_LIFETIME": timedelta(days=1), |
|
210 | 179 | } |
211 | 180 |
|
212 | 181 |
|
213 | | -# Default AutoField |
214 | | - |
215 | | -DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" |
216 | | - |
217 | | - |
218 | 182 | # Cookie settings for auth |
219 | | -def get_cookie_settings(): |
220 | | - if ENV in ["dev", "prod"]: |
221 | | - return { |
222 | | - "httponly": True, |
223 | | - "secure": True, |
224 | | - "samesite": "None", |
225 | | - "domain": "nextshape-backend-dev.onrender.com", |
226 | | - "path": "/", |
227 | | - } |
228 | | - else: |
229 | | - return {"httponly": True, "secure": False, "samesite": "Lax", "path": "/"} |
230 | | - |
231 | | - |
232 | | -COOKIE_PARAMS = get_cookie_settings() |
| 183 | +COOKIE_PARAMS = { |
| 184 | + "httponly": True, |
| 185 | + "secure": not DEBUG, |
| 186 | + "samesite": "None" if not DEBUG else "Lax", |
| 187 | + "path": "/", |
| 188 | +} |
| 189 | + |
| 190 | +if ENV == "local": |
| 191 | + # CORS config in local when we don't use docker, |
| 192 | + # we may need the access between back and front |
| 193 | + INSTALLED_APPS += ["corsheaders"] |
| 194 | + MIDDLEWARE.insert(0, "corsheaders.middleware.CorsMiddleware") |
| 195 | + |
| 196 | + CORS_ALLOWED_ORIGINS = ["http://localhost:5173"] |
| 197 | + CORS_ALLOW_CREDENTIALS = True |
| 198 | + CSRF_TRUSTED_ORIGINS = ["http://localhost:5173"] |
0 commit comments