Hi,

PFA patch to fix the issue where user were not able to run pgAdmin4 under
WSGI Alias (example: localhost/pgadmin).
RM#2563

*Issue:*
- Removed hard coded url paths from base.html
- URL's are not generated with WSGI alias when we use
current_app.url_map.iter_rules() function to fetch all the endpoints and
their respective url's.

Please review.

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py
index 49c4fdd..0ca1c58 100644
--- a/web/pgadmin/__init__.py
+++ b/web/pgadmin/__init__.py
@@ -14,7 +14,7 @@ import os, sys
 from collections import defaultdict
 from importlib import import_module
 
-from flask import Flask, abort, request, current_app, session
+from flask import Flask, abort, request, current_app, session, url_for
 from flask_babel import Babel, gettext
 from flask_htmlmin import HTMLMIN
 from flask_login import user_logged_in
@@ -89,13 +89,39 @@ class PgAdmin(Flask):
 
     @property
     def exposed_endpoint_url_map(self):
+        #############################################################
+        # To handle WSGI paths
+        # If user has setup application under WSGI alias
+        # like 'localhost/pgadmin4' then we have to append '/pgadmin4'
+        # into endpoints
+        #############################################################
+        import config
+        is_wsgi_root_present = False
+        if config.SERVER_MODE:
+            pgadmin_root_path = url_for('browser.index')
+            if pgadmin_root_path != '/browser/':
+                is_wsgi_root_present = True
+                wsgi_root_path = pgadmin_root_path.replace(
+                    '/browser/', ''
+                )
+
+        def get_full_url_path(url):
+            """
+            Generate endpoint URL at per WSGI alias
+            """
+            if is_wsgi_root_present and url:
+                return wsgi_root_path + url
+            else:
+                return url
+
+        # Fetch all endpoints and thier respective url
         for rule in current_app.url_map.iter_rules('static'):
-            yield rule.endpoint, rule.rule
+            yield rule.endpoint, get_full_url_path(rule.rule)
 
         for module in self.submodules:
             for endpoint in module.exposed_endpoints:
                 for rule in current_app.url_map.iter_rules(endpoint):
-                    yield rule.endpoint, rule.rule
+                    yield rule.endpoint, get_full_url_path(rule.rule)
 
     @property
     def javascripts(self):
diff --git a/web/pgadmin/templates/base.html b/web/pgadmin/templates/base.html
index 87e3f78..f0d1703 100755
--- a/web/pgadmin/templates/base.html
+++ b/web/pgadmin/templates/base.html
@@ -41,14 +41,14 @@
                     slickgrid: "{{ url_for('static', 
filename='js/generated/slickgrid') }}",
                     codemirror: "{{ url_for('static', 
filename='js/generated/codemirror') }}",
                     datagrid: "{{ url_for('static', 
filename='js/generated/datagrid') }}",
-                    sqleditor: "{{ url_for('static', 
filename='js/generated/sqleditor') }}"
-                    ,'browser_node': "{{ url_for('static', 
filename='js/generated/browser_node') }}"
-                    ,'pgadmin.browser.utils': "/browser/js/utils"
-                    ,'pgadmin.browser.endpoints': "/browser/js/endpoints"
-                    ,'pgadmin.browser.messages': "/browser/js/messages"
-                    ,'pgadmin.server.supported_servers': 
"/browser/server/supported_servers"
-                    ,'pgadmin.user_management.current_user': 
"/user_management/current_user"
-                    ,'translations': "/tools/translations"
+                    sqleditor: "{{ url_for('static', 
filename='js/generated/sqleditor') }}",
+                    'browser_node': "{{ url_for('static', 
filename='js/generated/browser_node') }}",
+                    'pgadmin.browser.utils': "{{ url_for('browser.index') }}" 
+ "js/utils",
+                    'pgadmin.browser.endpoints': "{{ url_for('browser.index') 
}}" + "js/endpoints",
+                    'pgadmin.browser.messages': "{{ url_for('browser.index') 
}}" + "js/messages",
+                    'pgadmin.server.supported_servers': "{{ 
url_for('browser.index') }}" + "server/supported_servers",
+                    'pgadmin.user_management.current_user': "{{ 
url_for('user_management.index') }}" + "current_user",
+                    'translations': "{{ url_for('tools.index') }}" + 
"translations"
                 }
             });
 

Reply via email to