From 3738f7ff99661fa0319ebe2d8228ad8c479fb46a Mon Sep 17 00:00:00 2001 From: David Lord Date: Wed, 10 Jan 2018 12:02:13 -0800 Subject: [PATCH] match run command behavior in app.run output extra server information handle env var changed by load_dotenv enable threading by default --- flask/app.py | 27 ++++++++++++++++---- flask/cli.py | 70 +++++++++++++++++++++++++++------------------------- 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/flask/app.py b/flask/app.py index e84e0a3f..1079bb27 100644 --- a/flask/app.py +++ b/flask/app.py @@ -864,21 +864,34 @@ class Flask(_PackageBoundObject): If installed, python-dotenv will be used to load environment variables from :file:`.env` and :file:`.flaskenv` files. - .. versionchanged:: 0.10 - The default port is now picked from the ``SERVER_NAME`` variable. + If set, the :envvar:`FLASK_ENV` and :envvar:`FLASK_DEBUG` + environment variables will override :attr:`env` and + :attr:`debug`. + Threaded mode is enabled by default. + + .. versionchanged:: 0.10 + The default port is now picked from the ``SERVER_NAME`` + variable. """ # Change this into a no-op if the server is invoked from the - # command line. Have a look at cli.py for more information. + # command line. Have a look at cli.py for more information. if os.environ.get('FLASK_RUN_FROM_CLI') == 'true': from .debughelpers import explain_ignored_app_run explain_ignored_app_run() return if load_dotenv: - from flask.cli import load_dotenv - load_dotenv() + cli.load_dotenv() + # if set, let env vars override previous values + if 'FLASK_ENV' in os.environ: + self.env = get_env() + self.debug = get_debug_flag() + elif 'FLASK_DEBUG' in os.environ: + self.debug = get_debug_flag() + + # debug passed to method overrides all other sources if debug is not None: self.debug = bool(debug) @@ -892,8 +905,12 @@ class Flask(_PackageBoundObject): host = host or sn_host or _host port = int(port or sn_port or _port) + options.setdefault('use_reloader', self.debug) options.setdefault('use_debugger', self.debug) + options.setdefault('threaded', True) + + cli.show_server_banner(self.env, self.debug, self.name) from werkzeug.serving import run_simple diff --git a/flask/cli.py b/flask/cli.py index 2adadcc7..52a4f596 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -577,6 +577,28 @@ def load_dotenv(path=None): return new_dir is not None # at least one file was located and loaded +def show_server_banner(env, debug, app_import_path): + """Show extra startup messages the first time the server is run, + ignoring the reloader. + """ + if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': + return + + if app_import_path is not None: + print(' * Serving Flask app "{0}"'.format(app_import_path)) + + print(' * Environment: {0}'.format(env)) + + if env == 'production': + click.secho( + ' WARNING: Do not use the development server in a production' + ' environment.', fg='red') + click.secho(' Use a production WSGI server instead.', dim=True) + + if debug is not None: + print(' * Debug mode: {0}'.format('on' if debug else 'off')) + + @click.command('run', short_help='Runs a development server.') @click.option('--host', '-h', default='127.0.0.1', help='The interface to bind to.') @@ -596,51 +618,31 @@ def load_dotenv(path=None): @pass_script_info def run_command(info, host, port, reload, debugger, eager_loading, with_threads): - """Runs a local development server for the Flask application. + """Run a local development server. - This local server is recommended for development purposes only but it - can also be used for simple intranet deployments. By default it will - not support any sort of concurrency at all to simplify debugging. This - can be changed with the --with-threads option which will enable basic - multithreading. + This server is for development purposes only. It does not provide + the stability, security, or performance of production WSGI servers. - The reloader and debugger are by default enabled if the debug flag of - Flask is enabled and disabled otherwise. + The reloader and debugger are enabled by default if + FLASK_ENV=development or FLASK_DEBUG=1. """ - from werkzeug.serving import run_simple - - if get_env() == 'production': - click.secho('Warning: Detected a production environment. Do not ' - 'use `flask run` for production use.', - fg='red') - click.secho('Use a production ready WSGI server instead', - dim=True) - debug = get_debug_flag() + if reload is None: - reload = bool(debug) + reload = debug + if debugger is None: - debugger = bool(debug) + debugger = debug + if eager_loading is None: eager_loading = not reload + show_server_banner(get_env(), debug, info.app_import_path) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) - # Extra startup messages. This depends a bit on Werkzeug internals to - # not double execute when the reloader kicks in. - if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': - # If we have an import path we can print it out now which can help - # people understand what's being served. If we do not have an - # import path because the app was loaded through a callback then - # we won't print anything. - if info.app_import_path is not None: - print(' * Serving Flask app "%s"' % info.app_import_path) - print(' * Env %s' % get_env()) - if debug is not None: - print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) - - run_simple(host, port, app, use_reloader=reload, - use_debugger=debugger, threaded=with_threads) + from werkzeug.serving import run_simple + run_simple(host, port, app, use_reloader=reload, use_debugger=debugger, + threaded=with_threads) @click.command('shell', short_help='Runs a shell in the app context.')