2014-12-16 09:54:29 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2023-01-02 00:23:55 -06:00
|
|
|
# Copyright (C) 2013 - 2023, The pgAdmin Development Team
|
2014-12-16 09:54:29 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
"""This is the main application entry point for pgAdmin 4. If running on
|
2015-01-21 06:00:13 -06:00
|
|
|
a webserver, this will provide the WSGI interface, otherwise, we're going
|
|
|
|
to start a web server."""
|
|
|
|
|
2015-06-30 00:51:55 -05:00
|
|
|
import sys
|
2023-05-22 00:38:29 -05:00
|
|
|
if sys.version_info <= (3, 9):
|
|
|
|
import select
|
2014-12-16 09:54:29 -06:00
|
|
|
|
2020-04-30 03:47:00 -05:00
|
|
|
if sys.version_info < (3, 4):
|
2020-08-07 02:07:00 -05:00
|
|
|
raise RuntimeError('This application must be run under Python 3.4 '
|
|
|
|
'or later.')
|
2020-04-30 03:47:00 -05:00
|
|
|
import builtins
|
|
|
|
import os
|
2017-08-25 04:54:28 -05:00
|
|
|
|
2014-12-16 09:54:29 -06:00
|
|
|
# We need to include the root directory in sys.path to ensure that we can
|
|
|
|
# find everything we need when running in the standalone runtime.
|
2020-05-04 09:28:44 -05:00
|
|
|
if sys.path[0] != os.path.dirname(os.path.realpath(__file__)):
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
2014-12-16 09:54:29 -06:00
|
|
|
|
2017-08-25 04:54:28 -05:00
|
|
|
# Grab the SERVER_MODE if it's been set by the runtime
|
2021-01-29 02:08:27 -06:00
|
|
|
if 'PGADMIN_SERVER_MODE' in os.environ:
|
|
|
|
if os.environ['PGADMIN_SERVER_MODE'] == 'OFF':
|
|
|
|
builtins.SERVER_MODE = False
|
|
|
|
else:
|
|
|
|
builtins.SERVER_MODE = True
|
2017-08-25 04:54:28 -05:00
|
|
|
else:
|
|
|
|
builtins.SERVER_MODE = None
|
|
|
|
|
2023-02-15 00:01:29 -06:00
|
|
|
if (3, 10) > sys.version_info > (3, 8) and os.name == 'posix':
|
|
|
|
# Fix eventlet issue with Python 3.9.
|
|
|
|
# Ref: https://github.com/eventlet/eventlet/issues/670
|
|
|
|
# This was causing issue in psycopg3
|
2023-03-23 01:21:21 -05:00
|
|
|
import select
|
2023-02-15 00:01:29 -06:00
|
|
|
from eventlet import hubs
|
|
|
|
hubs.use_hub("poll")
|
|
|
|
|
2023-03-21 07:07:55 -05:00
|
|
|
import selectors
|
|
|
|
selectors.DefaultSelector = selectors.PollSelector
|
|
|
|
|
2014-12-16 11:14:48 -06:00
|
|
|
import config
|
2022-07-04 00:03:12 -05:00
|
|
|
import setup
|
2021-05-25 09:42:57 -05:00
|
|
|
from pgadmin import create_app, socketio
|
2021-01-18 05:02:10 -06:00
|
|
|
from pgadmin.utils.constants import INTERNAL
|
2016-06-24 06:50:52 -05:00
|
|
|
# Get the config database schema version. We store this in pgadmin.model
|
|
|
|
# as it turns out that putting it in the config files isn't a great idea
|
|
|
|
from pgadmin.model import SCHEMA_VERSION
|
2018-03-08 03:33:43 -06:00
|
|
|
|
2022-07-04 00:45:38 -05:00
|
|
|
|
2019-03-04 10:29:25 -06:00
|
|
|
##########################################################################
|
|
|
|
# Support reverse proxying
|
|
|
|
##########################################################################
|
2022-11-18 22:43:41 -06:00
|
|
|
class ReverseProxied():
|
2019-03-04 10:29:25 -06:00
|
|
|
def __init__(self, app):
|
2019-12-02 00:10:48 -06:00
|
|
|
self.app = app
|
2020-05-04 09:28:44 -05:00
|
|
|
# https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix
|
2019-10-01 07:25:43 -05:00
|
|
|
try:
|
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
self.app = ProxyFix(app,
|
|
|
|
x_for=config.PROXY_X_FOR_COUNT,
|
|
|
|
x_proto=config.PROXY_X_PROTO_COUNT,
|
|
|
|
x_host=config.PROXY_X_HOST_COUNT,
|
|
|
|
x_port=config.PROXY_X_PORT_COUNT,
|
|
|
|
x_prefix=config.PROXY_X_PREFIX_COUNT
|
|
|
|
)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2019-03-04 10:29:25 -06:00
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
script_name = environ.get("HTTP_X_SCRIPT_NAME", "")
|
|
|
|
if script_name:
|
|
|
|
environ["SCRIPT_NAME"] = script_name
|
|
|
|
path_info = environ["PATH_INFO"]
|
|
|
|
if path_info.startswith(script_name):
|
|
|
|
environ["PATH_INFO"] = path_info[len(script_name):]
|
|
|
|
scheme = environ.get("HTTP_X_SCHEME", "")
|
|
|
|
if scheme:
|
|
|
|
environ["wsgi.url_scheme"] = scheme
|
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
|
2014-12-17 09:27:54 -06:00
|
|
|
##########################################################################
|
2020-05-04 09:28:44 -05:00
|
|
|
# Sanity checks
|
2014-12-17 09:27:54 -06:00
|
|
|
##########################################################################
|
2020-05-04 09:28:44 -05:00
|
|
|
config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
|
|
|
|
|
2022-07-01 07:42:00 -05:00
|
|
|
# Check if the database exists. If it does not, create it.
|
|
|
|
setup_db_required = False
|
|
|
|
if not os.path.isfile(config.SQLITE_PATH):
|
|
|
|
setup_db_required = True
|
2017-06-12 10:51:54 -05:00
|
|
|
|
2020-05-04 09:28:44 -05:00
|
|
|
##########################################################################
|
|
|
|
# Create the app and configure it. It is created outside main so that
|
|
|
|
# it can be imported
|
|
|
|
##########################################################################
|
2014-12-18 11:49:09 -06:00
|
|
|
app = create_app()
|
2021-05-25 09:42:57 -05:00
|
|
|
app.config['sessions'] = dict()
|
|
|
|
|
2022-07-01 07:42:00 -05:00
|
|
|
if setup_db_required:
|
2022-07-01 04:20:12 -05:00
|
|
|
setup.setup_db(app)
|
|
|
|
|
2020-05-04 09:28:44 -05:00
|
|
|
# Authentication sources
|
|
|
|
if len(config.AUTHENTICATION_SOURCES) > 0:
|
2021-11-11 00:59:30 -06:00
|
|
|
# Creating a temporary auth source list removing INTERNAL
|
|
|
|
# This change is done to avoid selecting INTERNAL authentication when user
|
|
|
|
# mistakenly keeps that the first option.
|
|
|
|
auth_source = [x for x in config.AUTHENTICATION_SOURCES
|
|
|
|
if x != INTERNAL]
|
|
|
|
app.PGADMIN_EXTERNAL_AUTH_SOURCE = auth_source[0] \
|
|
|
|
if len(auth_source) > 0 else INTERNAL
|
2015-10-20 02:03:18 -05:00
|
|
|
else:
|
2021-01-18 05:02:10 -06:00
|
|
|
app.PGADMIN_EXTERNAL_AUTH_SOURCE = INTERNAL
|
2017-06-12 10:51:54 -05:00
|
|
|
|
2013-10-04 11:12:10 -05:00
|
|
|
# Start the web server. The port number should have already been set by the
|
2015-06-29 01:58:41 -05:00
|
|
|
# runtime if we're running in desktop mode, otherwise we'll just use the
|
2014-12-16 06:53:09 -06:00
|
|
|
# Flask default.
|
2020-05-04 09:28:44 -05:00
|
|
|
app.PGADMIN_RUNTIME = False
|
2021-01-29 02:08:27 -06:00
|
|
|
app.logger.debug(
|
2021-02-15 06:01:20 -06:00
|
|
|
'Config server mode: %s', config.SERVER_MODE
|
2021-01-29 02:08:27 -06:00
|
|
|
)
|
2020-05-04 09:28:44 -05:00
|
|
|
config.EFFECTIVE_SERVER_PORT = None
|
2021-01-29 02:08:27 -06:00
|
|
|
if 'PGADMIN_INT_PORT' in os.environ:
|
2019-12-12 23:12:43 -06:00
|
|
|
port = os.environ['PGADMIN_INT_PORT']
|
2017-02-22 06:41:28 -06:00
|
|
|
app.logger.debug(
|
2021-01-29 02:08:27 -06:00
|
|
|
'Running under the desktop runtime, port: %s',
|
2018-01-26 10:54:21 -06:00
|
|
|
port
|
|
|
|
)
|
2020-05-04 09:28:44 -05:00
|
|
|
config.EFFECTIVE_SERVER_PORT = int(port)
|
2014-12-16 06:53:09 -06:00
|
|
|
else:
|
2015-06-30 00:51:55 -05:00
|
|
|
app.logger.debug(
|
2016-06-17 04:03:32 -05:00
|
|
|
'Not running under the desktop runtime, port: %s',
|
2018-01-26 10:54:21 -06:00
|
|
|
config.DEFAULT_SERVER_PORT
|
|
|
|
)
|
2020-05-04 09:28:44 -05:00
|
|
|
config.EFFECTIVE_SERVER_PORT = config.DEFAULT_SERVER_PORT
|
2016-06-21 04:42:20 -05:00
|
|
|
|
2017-03-06 08:53:49 -06:00
|
|
|
# Set the key if appropriate
|
2021-01-29 02:08:27 -06:00
|
|
|
if 'PGADMIN_INT_KEY' in os.environ:
|
|
|
|
app.PGADMIN_INT_KEY = os.environ['PGADMIN_INT_KEY']
|
2019-12-12 23:12:43 -06:00
|
|
|
app.logger.debug("Desktop security key: %s" % app.PGADMIN_INT_KEY)
|
2021-01-29 02:08:27 -06:00
|
|
|
app.PGADMIN_RUNTIME = True
|
2017-03-06 08:53:49 -06:00
|
|
|
else:
|
2019-12-12 23:12:43 -06:00
|
|
|
app.PGADMIN_INT_KEY = ''
|
2017-03-06 08:53:49 -06:00
|
|
|
|
2022-11-14 07:28:36 -06:00
|
|
|
if not app.PGADMIN_RUNTIME:
|
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
|
2020-05-04 09:28:44 -05:00
|
|
|
##########################################################################
|
|
|
|
# The entry point
|
|
|
|
##########################################################################
|
|
|
|
def main():
|
|
|
|
# Set null device file path to stdout, stdin, stderr if they are None
|
|
|
|
for _name in ('stdin', 'stdout', 'stderr'):
|
|
|
|
if getattr(sys, _name) is None:
|
|
|
|
setattr(sys, _name,
|
|
|
|
open(os.devnull, 'r' if _name == 'stdin' else 'w'))
|
|
|
|
|
|
|
|
# Output a startup message if we're not under the runtime and startup.
|
|
|
|
# If we're under WSGI, we don't need to worry about this
|
|
|
|
if not app.PGADMIN_RUNTIME:
|
2018-01-26 10:54:21 -06:00
|
|
|
print(
|
|
|
|
"Starting %s. Please navigate to http://%s:%d in your browser." %
|
2020-05-04 09:28:44 -05:00
|
|
|
(config.APP_NAME, config.DEFAULT_SERVER,
|
|
|
|
config.EFFECTIVE_SERVER_PORT)
|
2018-01-26 10:54:21 -06:00
|
|
|
)
|
2016-08-18 07:43:00 -05:00
|
|
|
sys.stdout.flush()
|
2017-02-10 16:42:44 -06:00
|
|
|
else:
|
2021-01-29 02:08:27 -06:00
|
|
|
# For unknown reason the runtime does not pass the environment
|
2017-02-10 16:42:44 -06:00
|
|
|
# variables (i.e. PYTHONHOME, and PYTHONPATH), to the Python
|
|
|
|
# sub-processes, leading to failures executing background processes.
|
|
|
|
#
|
|
|
|
# This has been observed only on windows. On *nix systems, it is likely
|
|
|
|
# picking the system python environment, which is good enough to run
|
|
|
|
# the process-executor.
|
|
|
|
#
|
|
|
|
# Setting PYTHONHOME launch them properly.
|
2017-03-09 03:54:55 -06:00
|
|
|
from pgadmin.utils import IS_WIN
|
2018-03-08 03:33:43 -06:00
|
|
|
|
2017-03-09 03:54:55 -06:00
|
|
|
if IS_WIN:
|
|
|
|
os.environ['PYTHONHOME'] = sys.prefix
|
2016-06-14 06:00:06 -05:00
|
|
|
|
2017-07-21 03:46:30 -05:00
|
|
|
# Initialize Flask service only once
|
|
|
|
# If `WERKZEUG_RUN_MAIN` is None, i.e: app is initializing for first time
|
|
|
|
# so set `use_reloader` = False, thus reload won't call.
|
|
|
|
# Reference:
|
|
|
|
# https://github.com/pallets/werkzeug/issues/220#issuecomment-11176538
|
2016-08-18 07:43:00 -05:00
|
|
|
try:
|
2021-01-29 02:08:27 -06:00
|
|
|
if config.DEBUG:
|
|
|
|
app.run(
|
|
|
|
host=config.DEFAULT_SERVER,
|
|
|
|
port=config.EFFECTIVE_SERVER_PORT,
|
Update SQLAlchemy, Flask, Flask-SQLAlchemy, and other packages to current versions. #5901
- Update Flask, Flask-SQLAlchemy, Flask-Babel, Flask-Security-Too, Flask-SocketIO, pytz, psutil, SQLAlchemy, bcrypt, cryptography, eventlet, Authlib, requests python packages
- Remove pinned dnspython, Werkzeug packages from requirements.txt
2023-03-15 01:27:16 -05:00
|
|
|
debug=config.DEBUG,
|
2021-01-29 02:08:27 -06:00
|
|
|
use_reloader=(
|
Update SQLAlchemy, Flask, Flask-SQLAlchemy, and other packages to current versions. #5901
- Update Flask, Flask-SQLAlchemy, Flask-Babel, Flask-Security-Too, Flask-SocketIO, pytz, psutil, SQLAlchemy, bcrypt, cryptography, eventlet, Authlib, requests python packages
- Remove pinned dnspython, Werkzeug packages from requirements.txt
2023-03-15 01:27:16 -05:00
|
|
|
(not app.PGADMIN_RUNTIME) and
|
2021-01-29 02:08:27 -06:00
|
|
|
os.environ.get("WERKZEUG_RUN_MAIN") is not None
|
|
|
|
),
|
|
|
|
threaded=config.THREADED_MODE
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
try:
|
2021-05-25 09:42:57 -05:00
|
|
|
socketio.run(
|
|
|
|
app,
|
Update SQLAlchemy, Flask, Flask-SQLAlchemy, and other packages to current versions. #5901
- Update Flask, Flask-SQLAlchemy, Flask-Babel, Flask-Security-Too, Flask-SocketIO, pytz, psutil, SQLAlchemy, bcrypt, cryptography, eventlet, Authlib, requests python packages
- Remove pinned dnspython, Werkzeug packages from requirements.txt
2023-03-15 01:27:16 -05:00
|
|
|
debug=config.DEBUG,
|
|
|
|
allow_unsafe_werkzeug=True,
|
2021-05-25 09:42:57 -05:00
|
|
|
host=config.DEFAULT_SERVER,
|
|
|
|
port=config.EFFECTIVE_SERVER_PORT,
|
|
|
|
)
|
2021-01-29 02:08:27 -06:00
|
|
|
except KeyboardInterrupt:
|
2021-05-25 09:42:57 -05:00
|
|
|
print("CLOSE SERVER")
|
|
|
|
socketio.stop()
|
2020-01-07 07:15:07 -06:00
|
|
|
|
2016-08-18 07:43:00 -05:00
|
|
|
except IOError:
|
|
|
|
app.logger.error("Error starting the app server: %s", sys.exc_info())
|
2020-05-04 09:28:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Server startup
|
|
|
|
##########################################################################
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|