From 02a5fb64e41f642ba1ca65910b6855a2033dc571 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 1 Aug 2024 13:48:20 +0200 Subject: [PATCH] backend: allow customizing admin url --- .env.example | 1 + backend/trcustoms/settings.py | 1 + backend/trcustoms/urls.py | 2 +- docker-compose.prod.yml | 2 ++ docker-compose.yml | 6 ++++-- services/nginx/Dockerfile_prod | 3 ++- services/nginx/{nginx_dev.conf => nginx_dev.conf.template} | 2 +- .../nginx/{nginx_prod.conf => nginx_prod.conf.template} | 2 +- 8 files changed, 13 insertions(+), 6 deletions(-) rename services/nginx/{nginx_dev.conf => nginx_dev.conf.template} (95%) rename services/nginx/{nginx_prod.conf => nginx_prod.conf.template} (97%) diff --git a/.env.example b/.env.example index 6bfd1617..894e6681 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ TRCUSTOMS_PORT=8000 SECRET_KEY=change_me DEBUG=1 HOST_SITE=http://localhost/ +ADMIN_DIR=admin USE_AWS_STORAGE=0 diff --git a/backend/trcustoms/settings.py b/backend/trcustoms/settings.py index 442f8774..f7e3f372 100644 --- a/backend/trcustoms/settings.py +++ b/backend/trcustoms/settings.py @@ -227,6 +227,7 @@ def get_bool_setting(name: str, **kwargs) -> bool: ] STATIC_ROOT = BASE_DIR / "django_static" +ADMIN_DIR = get_setting("ADMIN_DIR") MEDIA_URL = "/uploads/" MEDIA_ROOT = BASE_DIR / "uploads" diff --git a/backend/trcustoms/urls.py b/backend/trcustoms/urls.py index 8635fb1f..060a826f 100644 --- a/backend/trcustoms/urls.py +++ b/backend/trcustoms/urls.py @@ -52,7 +52,7 @@ urlpatterns = [ path("api/auth/token/", TokenObtainPairView.as_view()), path("api/auth/token/refresh/", TokenRefreshView.as_view()), - path("admin/", admin.site.urls), + path(settings.ADMIN_DIR + "/", admin.site.urls), path("api/", include(router.urls)), path("api/level_genres/", GenreListView.as_view()), path("api/level_genres//", GenreDetailView.as_view()), diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index f46ba5b4..0c2ed9cf 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -5,6 +5,7 @@ x-backend-environment: &base-backend-environment - DEBUG=0 - DEBUG_SQL=0 - HOST_SITE=${HOST_SITE} + - ADMIN_DIR=${ADMIN_DIR} - POSTGRES_DB=trcustoms - POSTGRES_USER=root - POSTGRES_PASSWORD=root @@ -94,6 +95,7 @@ services: target: 8000 depends_on: - trcustoms + environment: *base-backend-environment volumes: - ./backend/volumes/django_static:/app/backend/django_static - ./backend/volumes/uploads:/app/backend/django_uploads diff --git a/docker-compose.yml b/docker-compose.yml index 538dc760..3325ec0d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,7 @@ x-backend-environment: &base-backend-environment - DEBUG=${DEBUG} - DEBUG_SQL=${DEBUG_SQL} - HOST_SITE=${HOST_SITE} + - ADMIN_DIR=${ADMIN_DIR} - POSTGRES_DB=trcustoms - POSTGRES_USER=root - POSTGRES_PASSWORD=root @@ -101,13 +102,13 @@ services: trcustoms-nginx: <<: *base-trcustoms image: nginx + environment: *base-backend-environment volumes: - ./services/nginx/nginx_prerender_map.conf:/etc/nginx/nginx_prerender_map.conf:ro - ./services/nginx/nginx_prerender_server.conf:/etc/nginx/nginx_prerender_server.conf:ro - - ./services/nginx/nginx_dev.conf:/etc/nginx/nginx.conf:ro + - ./services/nginx/nginx_dev.conf.template:/etc/nginx/nginx.conf.template:ro - ./backend/volumes/uploads/:/app/uploads/ - ./backend/volumes/django_static/:/app/django_static/ - command: ["nginx", "-g", "daemon off;"] networks: - trcustoms ports: @@ -117,6 +118,7 @@ services: target: 8000 depends_on: - trcustoms + command: /bin/bash -c 'envsubst "\$${ADMIN_DIR}" < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g "daemon off;"' trcustoms-prerender: <<: *base-trcustoms-backend diff --git a/services/nginx/Dockerfile_prod b/services/nginx/Dockerfile_prod index 3b74cf7b..106eeed7 100644 --- a/services/nginx/Dockerfile_prod +++ b/services/nginx/Dockerfile_prod @@ -23,6 +23,7 @@ RUN npm run build FROM nginx:stable-alpine COPY services/nginx/nginx_prerender_map.conf /etc/nginx/nginx_prerender_map.conf COPY services/nginx/nginx_prerender_server.conf /etc/nginx/nginx_prerender_server.conf -COPY services/nginx/nginx_prod.conf /etc/nginx/nginx.conf +COPY services/nginx/nginx_prod.conf.template /etc/nginx/nginx.conf.template +RUN envsubst '${ADMIN_DIR}' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf COPY --from=build /app/frontend/build /usr/share/nginx/html CMD ["nginx", "-g", "daemon off;"] diff --git a/services/nginx/nginx_dev.conf b/services/nginx/nginx_dev.conf.template similarity index 95% rename from services/nginx/nginx_dev.conf rename to services/nginx/nginx_dev.conf.template index 4e6fa9a5..3c3a86ed 100644 --- a/services/nginx/nginx_dev.conf +++ b/services/nginx/nginx_dev.conf.template @@ -41,7 +41,7 @@ http { proxy_pass http://trcustoms-frontend:3000; } - location ~ ^/(admin|api|uploads|django_static)(/.*)?$ { + location ~ ^/(${ADMIN_DIR}|api|uploads|django_static)(/.*)?$ { proxy_pass http://trcustoms:8000; } diff --git a/services/nginx/nginx_prod.conf b/services/nginx/nginx_prod.conf.template similarity index 97% rename from services/nginx/nginx_prod.conf rename to services/nginx/nginx_prod.conf.template index 2d5cec01..b11023eb 100644 --- a/services/nginx/nginx_prod.conf +++ b/services/nginx/nginx_prod.conf.template @@ -44,7 +44,7 @@ http { try_files $uri $uri/ /index.html; } - location ~ ^/(admin|api)(/.*)?$ { + location ~ ^/(${ADMIN_DIR}|api)(/.*)?$ { proxy_pass http://trcustoms:8000; }