Fixed SocketIO calls when pgAdmin 4 server is running from a sub directory. #5521

This commit is contained in:
Aditya Toshniwal 2022-11-15 13:51:12 +05:30 committed by GitHub
parent c5e9aa6357
commit 669a3a7673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 11 deletions

View File

@ -123,7 +123,7 @@ class PgAdmin(Flask):
# like 'localhost/pgadmin4' then we have to append '/pgadmin4' # like 'localhost/pgadmin4' then we have to append '/pgadmin4'
# into endpoints # into endpoints
############################################################# #############################################################
wsgi_root_path = None wsgi_root_path = ''
if url_for('browser.index') != '/browser/': if url_for('browser.index') != '/browser/':
wsgi_root_path = url_for('browser.index').replace( wsgi_root_path = url_for('browser.index').replace(
'/browser/', '' '/browser/', ''
@ -133,10 +133,7 @@ class PgAdmin(Flask):
""" """
Generate endpoint URL at per WSGI alias Generate endpoint URL at per WSGI alias
""" """
if wsgi_root_path is not None and url: return wsgi_root_path + url
return wsgi_root_path + url
else:
return url
# Fetch all endpoints and their respective url # Fetch all endpoints and their respective url
for rule in current_app.url_map.iter_rules('static'): for rule in current_app.url_map.iter_rules('static'):
@ -147,6 +144,8 @@ class PgAdmin(Flask):
for rule in current_app.url_map.iter_rules(endpoint): for rule in current_app.url_map.iter_rules(endpoint):
yield rule.endpoint, get_full_url_path(rule.rule) yield rule.endpoint, get_full_url_path(rule.rule)
yield 'pgadmin.root', wsgi_root_path
@property @property
def javascripts(self): def javascripts(self):
scripts = [] scripts = []
@ -928,5 +927,5 @@ def create_app(app_name=None):
########################################################################## ##########################################################################
# All done! # All done!
########################################################################## ##########################################################################
socketio.init_app(app) socketio.init_app(app, cors_allowed_origins="*")
return app return app

View File

@ -8,10 +8,12 @@
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
import { io } from 'socketio'; import { io } from 'socketio';
import gettext from 'sources/gettext'; import gettext from 'sources/gettext';
import url_for from 'sources/url_for';
export function openSocket(namespace, options) { export function openSocket(namespace, options) {
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject)=>{
const socketObj = io(namespace, { const socketObj = io(namespace, {
path: `${url_for('pgadmin.root')}/socket.io`,
pingTimeout: 120000, pingTimeout: 120000,
pingInterval: 25000, pingInterval: 25000,
...options, ...options,
@ -24,11 +26,11 @@ export function openSocket(namespace, options) {
socketObj.on('connected', ()=>{ socketObj.on('connected', ()=>{
resolve(socketObj); resolve(socketObj);
}); });
socketObj.on('connect_error', ()=>{ socketObj.on('connect_error', (err)=>{
reject(); reject(err);
}); });
socketObj.on('disconnect', ()=>{ socketObj.on('disconnect', (err)=>{
reject(); reject(err);
}); });
}); });
} }

View File

@ -25,6 +25,8 @@ from pgadmin.utils.constants import MIMETYPE_APP_JS
from pgadmin.utils.driver import get_driver from pgadmin.utils.driver import get_driver
from ... import socketio as sio from ... import socketio as sio
from pgadmin.utils import get_complete_file_path from pgadmin.utils import get_complete_file_path
from pgadmin.authenticate import socket_login_required
if _platform == 'win32': if _platform == 'win32':
# Check Windows platform support for WinPty api, Disable psql # Check Windows platform support for WinPty api, Disable psql
@ -149,6 +151,7 @@ def set_term_size(fd, row, col, xpix=0, ypix=0):
@sio.on('connect', namespace='/pty') @sio.on('connect', namespace='/pty')
@socket_login_required
def connect(): def connect():
""" """
Connect to the server through socket. Connect to the server through socket.
@ -276,6 +279,7 @@ def pty_handel_io(connection_data, data, sid):
@sio.on('start_process', namespace='/pty') @sio.on('start_process', namespace='/pty')
@socket_login_required
def start_process(data): def start_process(data):
""" """
Start the pty terminal and execute psql command and emit results to user. Start the pty terminal and execute psql command and emit results to user.

View File

@ -275,7 +275,11 @@ export function initialize(gettext, url_for, $, _, pgAdmin, csrfToken, Browser)
return new SearchAddon(); return new SearchAddon();
}, },
psql_socket: function() { psql_socket: function() {
return io('/pty', {pingTimeout: 120000, pingInterval: 25000}); return io('/pty', {
path: `${url_for('pgadmin.root')}/socket.io`,
pingTimeout: 120000,
pingInterval: 25000
});
}, },
set_theme: function(term) { set_theme: function(term) {
let theme = { let theme = {