Skip to content

Commit

Permalink
fix: add urls helper
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 18, 2023
1 parent edcf656 commit 1c8ebca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
1 change: 0 additions & 1 deletion codeforlife/settings/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# The base url of the current service.
# The root service does not need its name included in the base url.
SERVICE_BASE_URL = f"{SERVICE_PROTOCOL}://{SERVICE_DOMAIN}:{SERVICE_PORT}"
SERVICE_BASE_ROUTE = "" if SERVICE_IS_ROOT else f"{SERVICE_NAME}/"
if not SERVICE_IS_ROOT:
SERVICE_BASE_URL += f"/{SERVICE_NAME}"

Expand Down
57 changes: 57 additions & 0 deletions codeforlife/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.contrib import admin
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import include, path, re_path
from rest_framework import status

from .settings import SERVICE_IS_ROOT, SERVICE_NAME


def service_urlpatterns(
api_urls_path: str = "api.urls",
frontend_template_name: str = "frontend.html",
):
urlpatterns = [
path(
"admin/",
admin.site.urls,
name="admin",
),
path(
"api/",
include(api_urls_path),
name="api",
),
re_path(
r"^api/.*",
lambda request: HttpResponse(
"API endpoint not found",
status=status.HTTP_404_NOT_FOUND,
),
name="api-endpoint-not-found",
),
re_path(
r".*",
lambda request: render(request, frontend_template_name),
name="frontend",
),
]

if SERVICE_IS_ROOT:
return urlpatterns

return [
path(
f"{SERVICE_NAME}/",
include(urlpatterns),
name="service",
),
re_path(
r".*",
lambda request: HttpResponse(
f'The base route is "{SERVICE_NAME}/".',
status=status.HTTP_404_NOT_FOUND,
),
name="service-not-found",
),
]

0 comments on commit 1c8ebca

Please sign in to comment.