From 5510329a99b56c254893c197f69fd928707bdf69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Guti=C3=A9rrez?= Date: Sat, 28 May 2022 02:17:08 +0200 Subject: [PATCH] add the posibility of specifying a url prefix (#143) this commit implements a prefix for urls, which allows easily deploying dpaste into a "subdirectory". It adds a url_prefix to be read by default from the environment which makes it especially suitable for using for docker containers. For example, running docker run -e PREFIX_URL=dpaste/ -p 8000:8000 barttc/dpaste will make dpaste available on http://localhost:8000/dpaste/ instead of the default http://localhost:8000 This is specially useful if you want to proxy dpaste from another host and let it live in a sub-url. Without this commit all urls would be incorrectly generated in this case. --- dpaste/settings/base.py | 2 ++ dpaste/urls/__init__.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dpaste/settings/base.py b/dpaste/settings/base.py index 63db06f..3b61056 100644 --- a/dpaste/settings/base.py +++ b/dpaste/settings/base.py @@ -51,6 +51,8 @@ MEDIA_ROOT = env("MEDIA_ROOT", ".media") STATIC_URL = "/static/" +URL_PREFIX = env("URL_PREFIX", "") + ROOT_URLCONF = "dpaste.urls" WSGI_APPLICATION = "dpaste.wsgi.application" diff --git a/dpaste/urls/__init__.py b/dpaste/urls/__init__.py index ad746cf..13b925c 100644 --- a/dpaste/urls/__init__.py +++ b/dpaste/urls/__init__.py @@ -1,9 +1,12 @@ from django.urls import include, re_path +from django.conf import settings + +url_prefix = getattr(settings, "URL_PREFIX", "") urlpatterns = [ - re_path(r"^", include("dpaste.urls.dpaste_api")), - re_path(r"^", include("dpaste.urls.dpaste")), - re_path(r"^i18n/", include("django.conf.urls.i18n")), + re_path(r"^%s" % url_prefix, include("dpaste.urls.dpaste_api")), + re_path(r"^%s" % url_prefix, include("dpaste.urls.dpaste")), + re_path(r"^%si18n/" % url_prefix, include("django.conf.urls.i18n")), ] # Custom error handlers which load `dpaste/.html` instead of `.html`