Skip to content

Commit

Permalink
Add setting for configuring optional URL prefix for /api (#14939)
Browse files Browse the repository at this point in the history
* Add setting for configuring optional URL prefix for /api

Add OPTIONAL_API_URLPATTERN_PREFIX setting

examples:
- if set to `''` (empty string) API pattern will be `/api`
- if set to 'controller' API pattern will be `/api` AND `/api/controller`
  • Loading branch information
TheRealHaoLiu authored Mar 19, 2024
1 parent 065bd3a commit ab593bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions awx/api/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def drf_reverse(viewname, args=None, kwargs=None, request=None, format=None, **e
else:
url = _reverse(viewname, args, kwargs, request, format, **extra)

if settings.OPTIONAL_API_URLPATTERN_PREFIX and request:
if request.path.startswith(f"/api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}"):
url = url.replace('/api', f"/api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}")

return url


Expand Down
5 changes: 5 additions & 0 deletions awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,3 +1126,8 @@

settings_file = os.path.join(os.path.dirname(dynamic_config.__file__), 'dynamic_settings.py')
include(settings_file)

# Add a postfix to the API URL patterns
# example if set to '' API pattern will be /api
# example if set to 'controller' API pattern will be /api AND /api/controller
OPTIONAL_API_URLPATTERN_PREFIX = ''
12 changes: 10 additions & 2 deletions awx/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# All Rights Reserved.

from django.conf import settings
from django.urls import re_path, include
from django.urls import path, re_path, include

from ansible_base.resource_registry.urls import urlpatterns as resource_api_urls

Expand All @@ -12,7 +12,15 @@
urlpatterns = [
re_path(r'', include('awx.ui.urls', namespace='ui')),
re_path(r'^ui_next/.*', include('awx.ui_next.urls', namespace='ui_next')),
re_path(r'^api/', include('awx.api.urls', namespace='api')),
path('api/', include('awx.api.urls', namespace='api')),
]

if settings.OPTIONAL_API_URLPATTERN_PREFIX:
urlpatterns += [
path(f'api/{settings.OPTIONAL_API_URLPATTERN_PREFIX}/', include('awx.api.urls')),
]

urlpatterns += [
re_path(r'^api/v2/', include(resource_api_urls)),
re_path(r'^sso/', include('awx.sso.urls', namespace='sso')),
re_path(r'^sso/', include('social_django.urls', namespace='social')),
Expand Down

0 comments on commit ab593bd

Please sign in to comment.