2014-12-16 09:54:29 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2019-01-02 04:24:12 -06:00
|
|
|
# Copyright (C) 2013 - 2019, 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 os
|
|
|
|
import sys
|
2019-09-23 01:20:32 -05:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2014-12-16 09:54:29 -06:00
|
|
|
|
2017-08-25 04:54:28 -05:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
import builtins
|
|
|
|
else:
|
|
|
|
import __builtin__ as builtins
|
|
|
|
|
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.
|
2015-03-10 08:09:11 -05:00
|
|
|
root = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
if sys.path[0] != root:
|
|
|
|
sys.path.insert(0, root)
|
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
|
|
|
|
if 'SERVER_MODE' in globals():
|
|
|
|
builtins.SERVER_MODE = globals()['SERVER_MODE']
|
|
|
|
else:
|
|
|
|
builtins.SERVER_MODE = None
|
|
|
|
|
2019-03-19 10:55:34 -05:00
|
|
|
# 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'))
|
|
|
|
|
2014-12-16 11:14:48 -06:00
|
|
|
import config
|
2014-12-18 11:49:09 -06:00
|
|
|
from pgadmin import create_app
|
2017-06-12 10:51:54 -05:00
|
|
|
from pgadmin.utils import u, fs_encoding, file_quote
|
|
|
|
|
|
|
|
if config.DEBUG:
|
2018-01-26 10:54:21 -06:00
|
|
|
from pgadmin.utils.javascript.javascript_bundler import \
|
|
|
|
JavascriptBundler, JsState
|
2013-10-04 11:12:10 -05:00
|
|
|
|
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
|
|
|
|
2016-06-24 06:50:52 -05:00
|
|
|
config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
|
|
|
|
|
2017-06-12 10:51:54 -05:00
|
|
|
##########################################################################
|
|
|
|
# Sanity checks
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
# Check if the database exists. If it does not, create it.
|
|
|
|
if not os.path.isfile(config.SQLITE_PATH):
|
|
|
|
setupfile = os.path.join(
|
2018-01-26 10:54:21 -06:00
|
|
|
os.path.dirname(os.path.realpath(u(__file__, fs_encoding))),
|
|
|
|
u'setup.py'
|
2017-06-12 10:51:54 -05:00
|
|
|
)
|
2018-03-08 05:07:05 -06:00
|
|
|
exec(open(file_quote(setupfile), 'r').read())
|
2015-01-26 09:20:28 -06:00
|
|
|
|
2019-03-04 10:29:25 -06:00
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Support reverse proxying
|
|
|
|
##########################################################################
|
|
|
|
class ReverseProxied(object):
|
|
|
|
def __init__(self, app):
|
2019-09-23 01:20:32 -05:00
|
|
|
# https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/#module-werkzeug.middleware.proxy_fix
|
2019-09-27 02:54:35 -05:00
|
|
|
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
|
|
|
|
)
|
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
|
|
|
##########################################################################
|
2017-06-12 10:51:54 -05:00
|
|
|
# Server startup
|
2014-12-17 09:27:54 -06:00
|
|
|
##########################################################################
|
|
|
|
|
2017-06-12 10:51:54 -05:00
|
|
|
# Build Javascript files
|
|
|
|
if config.DEBUG:
|
|
|
|
javascriptBundler = JavascriptBundler()
|
|
|
|
javascriptBundler.bundle()
|
|
|
|
|
2014-12-18 11:49:09 -06:00
|
|
|
# Create the app!
|
|
|
|
app = create_app()
|
2019-09-23 01:20:32 -05:00
|
|
|
|
|
|
|
if config.SERVER_MODE:
|
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2014-12-18 11:49:09 -06:00
|
|
|
|
2015-06-29 03:11:56 -05:00
|
|
|
if config.DEBUG:
|
|
|
|
app.debug = True
|
2015-10-20 02:03:18 -05:00
|
|
|
else:
|
|
|
|
app.debug = False
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2017-06-12 10:51:54 -05:00
|
|
|
# respond to JS
|
|
|
|
if config.DEBUG:
|
|
|
|
if javascriptBundler.report() == JsState.NONE:
|
|
|
|
app.logger.error("Unable to generate javascript")
|
2018-01-26 10:54:21 -06:00
|
|
|
app.logger.error(
|
|
|
|
"To run the app ensure that yarn install command runs successfully"
|
|
|
|
)
|
2017-06-12 10:51:54 -05:00
|
|
|
raise Exception("No generated javascript, aborting")
|
|
|
|
|
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.
|
2016-06-17 04:03:32 -05:00
|
|
|
PGADMIN_RUNTIME = False
|
2014-12-16 06:53:09 -06:00
|
|
|
if 'PGADMIN_PORT' in globals():
|
2018-01-26 10:54:21 -06:00
|
|
|
app.logger.debug(
|
|
|
|
'Running under the desktop runtime, port: %s',
|
|
|
|
globals()['PGADMIN_PORT']
|
|
|
|
)
|
2015-06-30 00:51:55 -05:00
|
|
|
server_port = int(globals()['PGADMIN_PORT'])
|
2016-06-17 04:03:32 -05:00
|
|
|
PGADMIN_RUNTIME = True
|
2017-02-22 06:41:28 -06:00
|
|
|
elif 'PGADMIN_PORT' in os.environ:
|
|
|
|
port = os.environ['PGADMIN_PORT']
|
|
|
|
app.logger.debug(
|
|
|
|
'Not running under the desktop runtime, port: %s',
|
2018-01-26 10:54:21 -06:00
|
|
|
port
|
|
|
|
)
|
2017-02-22 06:41:28 -06:00
|
|
|
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
|
|
|
|
)
|
2014-12-16 11:14:48 -06:00
|
|
|
server_port = config.DEFAULT_SERVER_PORT
|
2014-12-16 06:53:09 -06:00
|
|
|
|
2016-06-21 04:42:20 -05:00
|
|
|
# Let the application save the status about the runtime for using it later.
|
|
|
|
app.PGADMIN_RUNTIME = PGADMIN_RUNTIME
|
|
|
|
|
2017-03-06 08:53:49 -06:00
|
|
|
# Set the key if appropriate
|
|
|
|
if 'PGADMIN_KEY' in globals():
|
|
|
|
app.PGADMIN_KEY = globals()['PGADMIN_KEY']
|
|
|
|
app.logger.debug("Desktop security key: %s" % app.PGADMIN_KEY)
|
|
|
|
else:
|
|
|
|
app.PGADMIN_KEY = ''
|
|
|
|
|
2016-08-18 07:43:00 -05:00
|
|
|
# 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 __name__ == '__main__':
|
|
|
|
if not PGADMIN_RUNTIME:
|
2018-01-26 10:54:21 -06:00
|
|
|
print(
|
|
|
|
"Starting %s. Please navigate to http://%s:%d in your browser." %
|
|
|
|
(config.APP_NAME, config.DEFAULT_SERVER, server_port)
|
|
|
|
)
|
2016-08-18 07:43:00 -05:00
|
|
|
sys.stdout.flush()
|
2017-02-10 16:42:44 -06:00
|
|
|
else:
|
|
|
|
# For unknown reason the Qt runtime does not pass the environment
|
|
|
|
# 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:
|
|
|
|
app.run(
|
|
|
|
host=config.DEFAULT_SERVER,
|
|
|
|
port=server_port,
|
2017-07-21 03:46:30 -05:00
|
|
|
use_reloader=(
|
2018-03-08 03:33:43 -06:00
|
|
|
(not PGADMIN_RUNTIME) and app.debug and
|
|
|
|
os.environ.get("WERKZEUG_RUN_MAIN") is not None
|
2017-07-21 03:46:30 -05:00
|
|
|
),
|
2016-08-18 07:43:00 -05:00
|
|
|
threaded=config.THREADED_MODE
|
|
|
|
)
|
2018-03-19 12:09:19 -05:00
|
|
|
|
2016-08-18 07:43:00 -05:00
|
|
|
except IOError:
|
|
|
|
app.logger.error("Error starting the app server: %s", sys.exc_info())
|