gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
7.9KB

  1. ##########################################################################
  2. #
  3. # pgAdmin 4 - PostgreSQL Tools
  4. #
  5. # Copyright (C) 2013 - 2020, The pgAdmin Development Team
  6. # This software is released under the PostgreSQL Licence
  7. #
  8. ##########################################################################
  9. """This is the main application entry point for pgAdmin 4. If running on
  10. a webserver, this will provide the WSGI interface, otherwise, we're going
  11. to start a web server."""
  12. import sys
  13. if sys.version_info < (3, 4):
  14. raise Exception('This application must be run under Python 3.4 or later.')
  15. import builtins
  16. import os
  17. # We need to include the root directory in sys.path to ensure that we can
  18. # find everything we need when running in the standalone runtime.
  19. if sys.path[0] != os.path.dirname(os.path.realpath(__file__)):
  20. sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
  21. # Grab the SERVER_MODE if it's been set by the runtime
  22. if 'SERVER_MODE' in globals():
  23. builtins.SERVER_MODE = globals()['SERVER_MODE']
  24. else:
  25. builtins.SERVER_MODE = None
  26. import config
  27. from pgadmin import create_app
  28. from pgadmin.utils import u_encode, fs_encoding, file_quote
  29. # Get the config database schema version. We store this in pgadmin.model
  30. # as it turns out that putting it in the config files isn't a great idea
  31. from pgadmin.model import SCHEMA_VERSION
  32. ##########################################################################
  33. # Support reverse proxying
  34. ##########################################################################
  35. class ReverseProxied(object):
  36. def __init__(self, app):
  37. self.app = app
  38. # https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix
  39. try:
  40. from werkzeug.middleware.proxy_fix import ProxyFix
  41. self.app = ProxyFix(app,
  42. x_for=config.PROXY_X_FOR_COUNT,
  43. x_proto=config.PROXY_X_PROTO_COUNT,
  44. x_host=config.PROXY_X_HOST_COUNT,
  45. x_port=config.PROXY_X_PORT_COUNT,
  46. x_prefix=config.PROXY_X_PREFIX_COUNT
  47. )
  48. except ImportError:
  49. pass
  50. def __call__(self, environ, start_response):
  51. script_name = environ.get("HTTP_X_SCRIPT_NAME", "")
  52. if script_name:
  53. environ["SCRIPT_NAME"] = script_name
  54. path_info = environ["PATH_INFO"]
  55. if path_info.startswith(script_name):
  56. environ["PATH_INFO"] = path_info[len(script_name):]
  57. scheme = environ.get("HTTP_X_SCHEME", "")
  58. if scheme:
  59. environ["wsgi.url_scheme"] = scheme
  60. return self.app(environ, start_response)
  61. ##########################################################################
  62. # Sanity checks
  63. ##########################################################################
  64. config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
  65. # Check if the database exists. If it does not, create it.
  66. if not os.path.isfile(config.SQLITE_PATH):
  67. setup_py = os.path.join(
  68. os.path.dirname(os.path.realpath(u_encode(__file__, fs_encoding))),
  69. u'setup.py'
  70. )
  71. exec(open(file_quote(setup_py), 'r').read())
  72. ##########################################################################
  73. # Create the app and configure it. It is created outside main so that
  74. # it can be imported
  75. ##########################################################################
  76. app = create_app()
  77. app.debug = False
  78. if config.SERVER_MODE:
  79. app.wsgi_app = ReverseProxied(app.wsgi_app)
  80. # Authentication sources
  81. app.PGADMIN_DEFAULT_AUTH_SOURCE = 'internal'
  82. app.PGADMIN_SUPPORTED_AUTH_SOURCE = ['internal', 'ldap']
  83. if len(config.AUTHENTICATION_SOURCES) > 0:
  84. app.PGADMIN_EXTERNAL_AUTH_SOURCE = config.AUTHENTICATION_SOURCES[0]
  85. else:
  86. app.PGADMIN_EXTERNAL_AUTH_SOURCE = app.PGADMIN_DEFAULT_AUTH_SOURCE
  87. app.logger.debug(
  88. "Authentication Source: %s" % app.PGADMIN_DEFAULT_AUTH_SOURCE)
  89. # Start the web server. The port number should have already been set by the
  90. # runtime if we're running in desktop mode, otherwise we'll just use the
  91. # Flask default.
  92. app.PGADMIN_RUNTIME = False
  93. config.EFFECTIVE_SERVER_PORT = None
  94. if 'PGADMIN_INT_PORT' in globals():
  95. app.logger.debug(
  96. 'Running under the desktop runtime, port: %s',
  97. globals()['PGADMIN_INT_PORT']
  98. )
  99. config.EFFECTIVE_SERVER_PORT = int(globals()['PGADMIN_INT_PORT'])
  100. app.PGADMIN_RUNTIME = True
  101. elif 'PGADMIN_INT_PORT' in os.environ:
  102. port = os.environ['PGADMIN_INT_PORT']
  103. app.logger.debug(
  104. 'Not running under the desktop runtime, port: %s',
  105. port
  106. )
  107. config.EFFECTIVE_SERVER_PORT = int(port)
  108. else:
  109. app.logger.debug(
  110. 'Not running under the desktop runtime, port: %s',
  111. config.DEFAULT_SERVER_PORT
  112. )
  113. config.EFFECTIVE_SERVER_PORT = config.DEFAULT_SERVER_PORT
  114. # Set the key if appropriate
  115. if 'PGADMIN_INT_KEY' in globals():
  116. app.PGADMIN_INT_KEY = globals()['PGADMIN_INT_KEY']
  117. app.logger.debug("Desktop security key: %s" % app.PGADMIN_INT_KEY)
  118. else:
  119. app.PGADMIN_INT_KEY = ''
  120. ##########################################################################
  121. # The entry point
  122. ##########################################################################
  123. def main():
  124. # Set null device file path to stdout, stdin, stderr if they are None
  125. for _name in ('stdin', 'stdout', 'stderr'):
  126. if getattr(sys, _name) is None:
  127. setattr(sys, _name,
  128. open(os.devnull, 'r' if _name == 'stdin' else 'w'))
  129. # Build Javascript files when DEBUG
  130. if config.DEBUG:
  131. from pgadmin.utils.javascript.javascript_bundler import \
  132. JavascriptBundler, JsState
  133. app.debug = True
  134. javascript_bundler = JavascriptBundler()
  135. javascript_bundler.bundle()
  136. if javascript_bundler.report() == JsState.NONE:
  137. app.logger.error(
  138. "Unable to generate javascript.\n"
  139. "To run the app ensure that yarn install command runs "
  140. "successfully"
  141. )
  142. raise Exception("No generated javascript, aborting")
  143. # Output a startup message if we're not under the runtime and startup.
  144. # If we're under WSGI, we don't need to worry about this
  145. if not app.PGADMIN_RUNTIME:
  146. print(
  147. "Starting %s. Please navigate to http://%s:%d in your browser." %
  148. (config.APP_NAME, config.DEFAULT_SERVER,
  149. config.EFFECTIVE_SERVER_PORT)
  150. )
  151. sys.stdout.flush()
  152. else:
  153. # For unknown reason the Qt runtime does not pass the environment
  154. # variables (i.e. PYTHONHOME, and PYTHONPATH), to the Python
  155. # sub-processes, leading to failures executing background processes.
  156. #
  157. # This has been observed only on windows. On *nix systems, it is likely
  158. # picking the system python environment, which is good enough to run
  159. # the process-executor.
  160. #
  161. # Setting PYTHONHOME launch them properly.
  162. from pgadmin.utils import IS_WIN
  163. if IS_WIN:
  164. os.environ['PYTHONHOME'] = sys.prefix
  165. # Initialize Flask service only once
  166. # If `WERKZEUG_RUN_MAIN` is None, i.e: app is initializing for first time
  167. # so set `use_reloader` = False, thus reload won't call.
  168. # Reference:
  169. # https://github.com/pallets/werkzeug/issues/220#issuecomment-11176538
  170. try:
  171. app.run(
  172. host=config.DEFAULT_SERVER,
  173. port=config.EFFECTIVE_SERVER_PORT,
  174. use_reloader=(
  175. (not app.PGADMIN_RUNTIME) and app.debug and
  176. os.environ.get("WERKZEUG_RUN_MAIN") is not None
  177. ),
  178. threaded=config.THREADED_MODE
  179. )
  180. except IOError:
  181. app.logger.error("Error starting the app server: %s", sys.exc_info())
  182. ##########################################################################
  183. # Server startup
  184. ##########################################################################
  185. if __name__ == '__main__':
  186. main()
上海开阖软件有限公司 沪ICP备12045867号-1