forked from orbit-oss/flask
Merge pull request #2591 from pallets/run-behavior
match run command behavior in app.run
This commit is contained in:
commit
f2cf4d0449
2 changed files with 58 additions and 39 deletions
27
flask/app.py
27
flask/app.py
|
|
@ -864,21 +864,34 @@ class Flask(_PackageBoundObject):
|
||||||
If installed, python-dotenv will be used to load environment
|
If installed, python-dotenv will be used to load environment
|
||||||
variables from :file:`.env` and :file:`.flaskenv` files.
|
variables from :file:`.env` and :file:`.flaskenv` files.
|
||||||
|
|
||||||
.. versionchanged:: 0.10
|
If set, the :envvar:`FLASK_ENV` and :envvar:`FLASK_DEBUG`
|
||||||
The default port is now picked from the ``SERVER_NAME`` variable.
|
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
|
# 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':
|
if os.environ.get('FLASK_RUN_FROM_CLI') == 'true':
|
||||||
from .debughelpers import explain_ignored_app_run
|
from .debughelpers import explain_ignored_app_run
|
||||||
explain_ignored_app_run()
|
explain_ignored_app_run()
|
||||||
return
|
return
|
||||||
|
|
||||||
if load_dotenv:
|
if load_dotenv:
|
||||||
from flask.cli import load_dotenv
|
cli.load_dotenv()
|
||||||
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:
|
if debug is not None:
|
||||||
self.debug = bool(debug)
|
self.debug = bool(debug)
|
||||||
|
|
||||||
|
|
@ -892,8 +905,12 @@ class Flask(_PackageBoundObject):
|
||||||
|
|
||||||
host = host or sn_host or _host
|
host = host or sn_host or _host
|
||||||
port = int(port or sn_port or _port)
|
port = int(port or sn_port or _port)
|
||||||
|
|
||||||
options.setdefault('use_reloader', self.debug)
|
options.setdefault('use_reloader', self.debug)
|
||||||
options.setdefault('use_debugger', 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
|
from werkzeug.serving import run_simple
|
||||||
|
|
||||||
|
|
|
||||||
70
flask/cli.py
70
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
|
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.command('run', short_help='Runs a development server.')
|
||||||
@click.option('--host', '-h', default='127.0.0.1',
|
@click.option('--host', '-h', default='127.0.0.1',
|
||||||
help='The interface to bind to.')
|
help='The interface to bind to.')
|
||||||
|
|
@ -596,51 +618,31 @@ def load_dotenv(path=None):
|
||||||
@pass_script_info
|
@pass_script_info
|
||||||
def run_command(info, host, port, reload, debugger, eager_loading,
|
def run_command(info, host, port, reload, debugger, eager_loading,
|
||||||
with_threads):
|
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
|
This server is for development purposes only. It does not provide
|
||||||
can also be used for simple intranet deployments. By default it will
|
the stability, security, or performance of production WSGI servers.
|
||||||
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.
|
|
||||||
|
|
||||||
The reloader and debugger are by default enabled if the debug flag of
|
The reloader and debugger are enabled by default if
|
||||||
Flask is enabled and disabled otherwise.
|
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()
|
debug = get_debug_flag()
|
||||||
|
|
||||||
if reload is None:
|
if reload is None:
|
||||||
reload = bool(debug)
|
reload = debug
|
||||||
|
|
||||||
if debugger is None:
|
if debugger is None:
|
||||||
debugger = bool(debug)
|
debugger = debug
|
||||||
|
|
||||||
if eager_loading is None:
|
if eager_loading is None:
|
||||||
eager_loading = not reload
|
eager_loading = not reload
|
||||||
|
|
||||||
|
show_server_banner(get_env(), debug, info.app_import_path)
|
||||||
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
|
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
|
||||||
|
|
||||||
# Extra startup messages. This depends a bit on Werkzeug internals to
|
from werkzeug.serving import run_simple
|
||||||
# not double execute when the reloader kicks in.
|
run_simple(host, port, app, use_reloader=reload, use_debugger=debugger,
|
||||||
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
|
threaded=with_threads)
|
||||||
# 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)
|
|
||||||
|
|
||||||
|
|
||||||
@click.command('shell', short_help='Runs a shell in the app context.')
|
@click.command('shell', short_help='Runs a shell in the app context.')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue