Server command ignores some values in config associated with app
dlipovetsky opened this issue · 4 comments
Running the below code with Flask 0.10.1 and Flask-Script 2.0.5 will result in a server starting at port 5000, though the app is explicitly configured to use port 8888. To the end user, the SERVER_NAME
configuration value appears to be ignored.
from flask import Flask
from flask.ext.script import Manager
app = Flask(__name__)
app.config['SERVER_NAME'] = 8888
manager = Manager(app)
if __name__ == '__main__':
manager.run()
As of 0.10, Flask parses SERVER_NAME
to set the host and port, falling to defaults if not set. (https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L823)
Server
command sets its own defaults (https://github.com/smurfix/flask-script/blob/master/flask_script/commands.py#L339) and initializes app with them (https://github.com/smurfix/flask-script/blob/master/flask_script/commands.py#L417), so the end result is an app that has all configuration settings, but some are not applied.
I'm not sure if this behavior merits changing. However, I think it should be documented, since it is confuses the end user when certain configuration values (e.g. SERVER_NAME
) appear to be ignored.
This seems to have been fixed at some point, although Flask requires SERVER_NAME
to be a string, setting it to an int throws an exception in Flask, setting it without a host makes it be ignored.
I am just starting with Flask-Script and am experiencing the same issue. It looks like runserver calls app.run
like this:
app.run(host=host,
port=port,
debug=use_debugger,
use_debugger=use_debugger,
use_reloader=use_reloader,
threaded=threaded,
processes=processes,
passthrough_errors=passthrough_errors,
**self.server_options)
with default values for host, port, etc... This overrides all the normal flask configuration environment variables, which is pretty frustrating to work around.
In case anyone else lands on this, I've found a workaround that works in the specific case of SERVER_NAME
, which is simply to make my own runserver command that goes back to providing None
s for host=
and port=
like so:
...
manager = Manager(app)
manager.add_command(
"runserver",
flask_script.commands.Server(host=None, port=None)
)
This will cause Flask to use whatever fallback it chooses, which will be the environment, then Flask's own internal defaults.