Skip to content

Commit fc3b4df

Browse files
committed
Proxito: allow to generate proxied API URLs with a prefix
- Ref #10181 - Ref #10408 - Ref readthedocs/meta#124
1 parent f4efd14 commit fc3b4df

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

readthedocs/api/v2/serializers.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Meta:
3131
'users',
3232
'canonical_url',
3333
'urlconf',
34+
'custom_prefix',
3435
)
3536

3637

readthedocs/projects/models.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,9 @@ def proxied_api_host(self):
693693
This needs to start with a slash at the root of the domain,
694694
and end without a slash
695695
"""
696-
if self.urlconf:
697-
# Add our proxied api host at the first place we have a $variable
698-
# This supports both subpaths & normal root hosting
699-
path_prefix = self.custom_path_prefix
700-
return unsafe_join_url_path(path_prefix, "/_")
696+
custom_prefix = self.proxied_api_prefix
697+
if custom_prefix:
698+
return unsafe_join_url_path(custom_prefix, "/_")
701699
return '/_'
702700

703701
@property
@@ -715,12 +713,20 @@ def proxied_static_path(self):
715713
return f"{self.proxied_api_host}/static/"
716714

717715
@property
718-
def custom_path_prefix(self):
716+
def proxied_api_prefix(self):
719717
"""
720-
Get the path prefix from the custom urlconf.
718+
Get the path prefix for proxied API paths (``/_/``).
721719
722720
Returns `None` if the project doesn't have a custom urlconf.
723721
"""
722+
# When using a custom prefix, we can only handle serving
723+
# docs pages under the prefix, not special paths like `/_/`.
724+
# Projects using the old implementation, need to proxy `/_/`
725+
# paths as is, this is, without the suffix, while those projects
726+
# migrate to the new implementation, we will prefix special paths,
727+
# they are manually un-prefixed in nginx.
728+
if self.custom_prefix and self.has_feature(Feature.USE_PROXIED_APIS_WITH_PREFIX):
729+
return self.custom_prefix
724730
if self.urlconf:
725731
# Return the value before the first defined variable,
726732
# as that is a prefix and not part of our normal doc patterns.
@@ -1930,6 +1936,7 @@ def add_features(sender, **kwargs):
19301936
DISABLE_PAGEVIEWS = "disable_pageviews"
19311937
RESOLVE_PROJECT_FROM_HEADER = "resolve_project_from_header"
19321938
USE_UNRESOLVER_WITH_PROXITO = "use_unresolver_with_proxito"
1939+
USE_PROXIED_APIS_WITH_PREFIX = "use_proxied_apis_with_prefix"
19331940
ALLOW_VERSION_WARNING_BANNER = "allow_version_warning_banner"
19341941

19351942
# Versions sync related features
@@ -2020,6 +2027,12 @@ def add_features(sender, **kwargs):
20202027
"Proxito: Use new unresolver implementation for serving documentation files."
20212028
),
20222029
),
2030+
(
2031+
USE_PROXIED_APIS_WITH_PREFIX,
2032+
_(
2033+
"Proxito: Use proxied APIs (/_/*) with the custom prefix if the project has one (Project.custom_prefix)."
2034+
),
2035+
),
20232036
(
20242037
ALLOW_VERSION_WARNING_BANNER,
20252038
_("Dashboard: Allow project to use the version warning banner."),

0 commit comments

Comments
 (0)