def run_command(info, host, port, reload, debugger, eager_loading, with_threads, cert): env = get_env() click.echo(' * Environment: {0}'.format(env)) if env == 'production': from ..gunicorn import WSGIApplication app = WSGIApplication(loader=info.load_app) app.run() else: debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if eager_loading is None: eager_loading = not reload click.echo(' * Debug mode: {0}'.format('on' if debug else 'off')) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) from werkzeug.serving import run_simple run_simple(host, port, app, use_reloader=reload, use_debugger=debugger, threaded=with_threads, ssl_context=cert)
def ssl(info, host, port, reload, debugger, eager_loading, with_threads, cert, key): """Run a local development server. 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 enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if eager_loading is None: eager_loading = not reload show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) if cert is None: ssl = info._loaded_app.config.get('SSL', {}) if ssl is {}: raise Exception("'SSL' section in configuration is missing") try: certificate = ssl['CERTIFICATE'] if not os.path.isfile(certificate): raise Exception("Certificate file '%s' not preset.") keyfile = ssl['KEYFILE'] if not os.path.isfile(keyfile): raise Exception("Certificate file '%s' not preset.") cert = (certificate, keyfile) except AttributeError: pass except Exception as exc: raise from werkzeug.serving import run_simple run_simple(host, port, app, use_reloader=reload, reloader_type='stat', use_debugger=debugger, threaded=with_threads, ssl_context=cert) return
def run(info, host, port, reload, debugger, eager_loading, with_threads): """Runs a local development server for the Flask-SocketIO-self application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # 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-SocketIO-self app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) else: # if this is the child process of the reloader, then make sure we don't # start the reloader once again reload = False def run_server(): app = info.load_app() if 'socketio' not in app.extensions: # flask-socketio is installed, but it isn't in this application # so we invoke Flask's original run command run_index = sys.argv.index('run') sys.argv = sys.argv[run_index:] return run_command() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def run(info, host, port, reload, debugger, eager_loading): """Runs a local development server for the Flask-SocketIO application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # 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-SocketIO app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) def run_server(): app = info.load_app() if 'socketio' not in app.extensions: # flask-socketio is installed, but it isn't in this application # so we invoke Flask's original run command run_index = sys.argv.index('run') sys.argv = sys.argv[run_index:] return run_command() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def run(info, host, port, reload, debugger, eager_loading): """Runs a local development server for the Flask-SocketIO application. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. """ debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload # 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-SocketIO app "%s"' % info.app_import_path) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) def run_server(): app = info.load_app() socketio = app.extensions['socketio'] socketio.run(app, host=host, port=port, debug=debugger, use_reloader=False, log_output=debugger) if reload: from werkzeug.serving import run_with_reloader run_with_reloader(run_server) else: run_server()
def run(info, host, port, reload, debugger, eager_loading, with_threads, cert, extra_files): """Run a local development server. 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 enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if debugger: os.environ['AIOFLASK_USE_DEBUGGER'] = 'true' certfile = None keyfile = None if cert is not None and len(cert) == 2: certfile = cert[0] keyfile = cert[1] show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app_import_path = info.app_import_path if app_import_path.endswith('.py'): app_import_path = app_import_path[:-3] + ':app' uvicorn.run( app_import_path, host=host, port=port, reload=reload, workers=1, log_level='debug' if debug else 'info', ssl_certfile=certfile, ssl_keyfile=keyfile, )
def dev(info, host, port, reload, debugger, eager_loading, with_threads, cert): """Run a local development server. 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 enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if eager_loading is None: eager_loading = not reload show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) applic = info.load_app() host = applic.config.get('HOST', host) port = applic.config.get('PORT', port) from werkzeug.serving import run_simple run_simple(host, port, app, use_reloader=reload, reloader_type='stat', use_debugger=debugger, threaded=with_threads, ssl_context=cert) return
def run_command(info, host, port, reload, debugger, eager_loading, with_threads, adhoc): """Runs a local development server for the Flask application. 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. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. Runs https with self singed certificates for localhost. """ if adhoc: context = 'adhoc' else: # get certificates or exit base = os.path.abspath(os.path.dirname(__file__)) keyfiles = set(['localhost.crt', 'localhost.key']) dirfiles = set(os.listdir(base)) missing = keyfiles.difference(dirfiles) if not keyfiles.issubset(dirfiles): click.secho('{} not found.'.format(" and ".join(missing)), fg='red') click.echo('Create a certificate with the following command: ') click.secho( 'openssl req -nodes -x509 -subj "/CN=localhost" -sha256 ' '-newkey rsa:4096 -keyout localhost.key -out localhost.crt ' '-days 365', fg='yellow') click.echo( 'Or use --adhoc for the adhoc ssl context (requires pyopenssl).' ) return from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.load_cert_chain(certfile='localhost.crt', keyfile='localhost.key') from werkzeug.serving import run_simple from flask.cli import get_debug_flag, DispatchingApp debug = get_debug_flag() if reload is None: reload = bool(debug) if debugger is None: debugger = bool(debug) if eager_loading is None: eager_loading = not reload 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) if debug is not None: print(' * Forcing debug mode %s' % (debug and 'on' or 'off')) run_simple(host, port, app, use_reloader=reload, ssl_context=context, use_debugger=debugger, threaded=with_threads)
def dev(info, host, port, reload, debugger, eager_loading, with_threads, cert): """Run a local development server. 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 enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if eager_loading is None: eager_loading = not reload show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) applic = info.load_app() if host is None: host = applic.config.get('HOST', 'localhost') if port is None: port = applic.config.get('PORT', 5000) else: port = int(port) API.logger.info("Serving application on http://{}:{}".format(host, port)) # appPath = applic.config.get( 'APP_PATH', os.curdir ) # appApiMod = applic.config.get( 'API_MODULE', '' ) # As those files may change, but are only loaded when the application starts # we monitor them, so that the application restart when they change extra_files = applic.config.get('EXTRA_FILES', []) appPath = applic.config.get('APP_PATH', os.curdir) appApiMod = applic.config.get('API_MODULE', '') extra_files.extend([ os.path.join(appPath, appApiMod, 'menu.yaml'), os.path.join(appPath, appApiMod, 'release.yaml') ]) if API.socketio is not None: app.debug = True API.socketio.run( app, host, port, debug=debugger, use_reloader=reload, # reloader_type = 'stat', # threaded = with_threads, # ssl_context = cert, extra_files=extra_files) else: from werkzeug.serving import run_simple run_simple(host, port, app, use_reloader=reload, reloader_type='stat', use_debugger=debugger, threaded=with_threads, ssl_context=cert, extra_files=extra_files) return
def ssl(info, host, port, reload, debugger, eager_loading, with_threads, cert, key): """Run a local development server. 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 enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug if eager_loading is None: eager_loading = not reload show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) applic = info.load_app() if cert is None: ssl = applic.config.get('SSL', {}) if ssl is {}: raise Exception("'SSL' section in configuration is missing") try: certificate = ssl['CERTIFICATE'] if not os.path.isfile(certificate): raise Exception("Certificate file '%s' not preset.") keyfile = ssl['KEYFILE'] if not os.path.isfile(keyfile): raise Exception("Certificate file '%s' not preset.") cert = (certificate, keyfile) except AttributeError: pass except Exception as exc: raise appPath = applic.config.get('APP_PATH', os.curdir) appApiMod = applic.config.get('API_MODULE', '') # As those files may change, but are only loaded when the application starts # we monitor them, so that the application restart when they change extra_files = [ os.path.join(appPath, appApiMod, 'menu.yaml'), os.path.join(appPath, appApiMod, 'release.yaml') ] from werkzeug.serving import run_simple API.logger.info("Serving application on https://{}:{}".format(host, port)) run_simple(host, port, app, use_reloader=reload, reloader_type='stat', use_debugger=debugger, threaded=with_threads, ssl_context=cert, extra_files=extra_files) return