Added serve view with settings cleaned up
This commit is contained in:
parent
81bed38ada
commit
5d7774cdd8
6 changed files with 48 additions and 52 deletions
|
|
@ -16,6 +16,9 @@ services:
|
||||||
- "traefik.http.routers.fyp-web.tls.certresolver=${CERTRESOLVER}"
|
- "traefik.http.routers.fyp-web.tls.certresolver=${CERTRESOLVER}"
|
||||||
- "traefik.http.services.fyp-web.loadbalancer.server.port=${PORT}"
|
- "traefik.http.services.fyp-web.loadbalancer.server.port=${PORT}"
|
||||||
- "com.centurylinklabs.watchtower.enable=true"
|
- "com.centurylinklabs.watchtower.enable=true"
|
||||||
|
volumes:
|
||||||
|
- ../../static:/app/static
|
||||||
|
- ../../media:/app/media
|
||||||
networks:
|
networks:
|
||||||
- proxy
|
- proxy
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -1,26 +1,25 @@
|
||||||
|
from dotenv import load_dotenv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
load_dotenv(dotenv_path = BASE_DIR / '.env')
|
load_dotenv(dotenv_path = BASE_DIR / '.env')
|
||||||
|
|
||||||
|
BUILD_DIR = os.getenv('DJANGO_BUILD_DIR', BASE_DIR / 'build')
|
||||||
|
|
||||||
|
|
||||||
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-pf#9a$@vq1o91n#mxwba_dm-+v9&*u4f3$#bts%zanu-$0*whk')
|
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
|
||||||
|
DEBUG = str(os.getenv('DJANGO_DEBUG')).lower() in ('1', 'true', 'yes', 'on')
|
||||||
|
|
||||||
|
ALLOWED_HOSTS = [stripped_host for host in os.getenv('DJANGO_ALLOWED_HOSTS', 'localhost').split(',') if (stripped_host:=host.strip())]
|
||||||
|
|
||||||
_debug_env = os.getenv('DJANGO_DEBUG', 'True')
|
PARENT_NAME = Path(__file__).resolve().parent.name
|
||||||
DEBUG = str(_debug_env).lower() in ('1', 'true', 'yes', 'on')
|
|
||||||
|
|
||||||
_hosts = os.getenv('DJANGO_ALLOWED_HOSTS', '')
|
STATIC_URL = os.getenv('DJANGO_STATIC_URL', '/static/')
|
||||||
ALLOWED_HOSTS = [h.strip() for h in _hosts.split(',') if h.strip()]
|
MEDIA_URL = os.getenv('DJANGO_MEDIA_URL', '/media/')
|
||||||
|
STATIC_ROOT = os.getenv('DJANGO_STATIC_ROOT', BASE_DIR / 'static')
|
||||||
|
MEDIA_ROOT = os.getenv('DJANGO_MEDIA_ROOT', BASE_DIR / 'media')
|
||||||
# Application definition
|
|
||||||
|
|
||||||
DJANGO_APPS = [
|
DJANGO_APPS = [
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
|
|
@ -30,24 +29,23 @@ DJANGO_APPS = [
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
]
|
]
|
||||||
|
|
||||||
THIRD_PARTY_APPS = [
|
THIRD_PARTY_APPS = [
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
]
|
]
|
||||||
|
|
||||||
LOCAL_APPS = [
|
LOCAL_APPS = [
|
||||||
'apps.users',
|
'apps.users',
|
||||||
'apps.domains',
|
'apps.domains',
|
||||||
'apps.agents',
|
'apps.agents',
|
||||||
]
|
]
|
||||||
|
|
||||||
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
|
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
|
||||||
|
|
||||||
|
|
||||||
AUTH_USER_MODEL = 'users.User'
|
AUTH_USER_MODEL = 'users.User'
|
||||||
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
|
|
@ -56,7 +54,10 @@ MIDDLEWARE = [
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'config.urls'
|
ROOT_URLCONF = f'{PARENT_NAME}.urls'
|
||||||
|
WSGI_APPLICATION = f'{PARENT_NAME}.wsgi.application'
|
||||||
|
ASGI_APPLICATION = f'{PARENT_NAME}.asgi.application'
|
||||||
|
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
|
|
@ -73,11 +74,6 @@ TEMPLATES = [
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
WSGI_APPLICATION = 'config.wsgi.application'
|
|
||||||
|
|
||||||
|
|
||||||
# Database
|
|
||||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
|
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
|
|
@ -86,14 +82,11 @@ DATABASES = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Allow overriding sqlite file via DJANGO_DB_NAME environment variable (relative to project root).
|
STORAGES = {
|
||||||
_db_name = os.getenv('DJANGO_DB_NAME')
|
"staticfiles": {
|
||||||
if _db_name:
|
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
|
||||||
DATABASES['default']['NAME'] = BASE_DIR / _db_name
|
},
|
||||||
|
}
|
||||||
|
|
||||||
# Password validation
|
|
||||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
|
|
||||||
|
|
||||||
AUTH_PASSWORD_VALIDATORS = [
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
{
|
{
|
||||||
|
|
@ -110,33 +103,12 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
LANGUAGE_CODE = 'en-uk'
|
||||||
# Internationalization
|
|
||||||
# https://docs.djangoproject.com/en/5.2/topics/i18n/
|
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
|
||||||
|
|
||||||
TIME_ZONE = 'UTC'
|
TIME_ZONE = 'UTC'
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
||||||
USE_TZ = True
|
USE_TZ = True
|
||||||
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
|
||||||
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
|
||||||
|
|
||||||
STATIC_URL = 'static/'
|
|
||||||
STATIC_ROOT = 'static/'
|
|
||||||
|
|
||||||
# Default primary key field type
|
|
||||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
||||||
|
|
||||||
# Django REST framework basic configuration for the POC. Adjust auth/permissions
|
|
||||||
# as features become defined. For production, tighten permissions and use token
|
|
||||||
# or JWT authentication as appropriate.
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||||
'rest_framework.authentication.SessionAuthentication',
|
'rest_framework.authentication.SessionAuthentication',
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,14 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include, re_path
|
||||||
|
from django.conf import settings
|
||||||
|
from django.conf.urls.static import static
|
||||||
|
|
||||||
|
from .views import serve_frontend
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('api/', include('config.api')),
|
path('api/', include('config.api')),
|
||||||
|
re_path(r'^(?!static/|media/)(?P<path>.*)$', serve_frontend, {'document_root': settings.BUILD_DIR}),
|
||||||
|
*static(settings.STATIC_URL, document_root=settings.STATIC_ROOT),
|
||||||
|
*static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
14
config/views.py
Normal file
14
config/views.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
from django.utils._os import safe_join
|
||||||
|
from django.views.static import serve as static_serve
|
||||||
|
from django.views.decorators.csrf import ensure_csrf_cookie
|
||||||
|
import posixpath
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ensure_csrf_cookie
|
||||||
|
def serve_frontend(request, path, document_root = None):
|
||||||
|
path = posixpath.normpath(path).lstrip("/")
|
||||||
|
fullpath = Path(safe_join(document_root, path))
|
||||||
|
if fullpath.is_file():
|
||||||
|
return static_serve(request, path, document_root)
|
||||||
|
else:
|
||||||
|
return static_serve(request, "index.html", document_root)
|
||||||
Loading…
Reference in a new issue