Created
April 10, 2012 15:58
-
-
Save rduplain/2352388 to your computer and use it in GitHub Desktop.
Flask: serving static file from a separate domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://flask.pocoo.org/mailinglist/archive/2012/4/10/serving-static-file-from-a-separate-domain-in-production/ | |
from flask import Flask, url_for | |
# Uncomment to set server name. | |
# SERVER_NAME = 'mianos.com' | |
app = Flask(__name__, static_folder=None) | |
app.config.from_object(__name__) | |
app.add_url_rule('/<path:filename>', endpoint='static', | |
view_func=app.send_static_file, subdomain='static') | |
@app.route('/') | |
def index(): | |
return url_for('static', filename='style.css') | |
if __name__ == '__main__': | |
app.run(debug=True) |
Given that there are multiple domains here, you need a DNS record pointing at your development instance, since 'localhost' does not support subdomains. I typically set local.example.com and *.local.example.com to 127.0.0.1 on my example.com DNS manager. Then SERVER_NAME is local.example.com in this gist.
If I use "app.debug = True" and "app.run(host='0.0.0.0', port=5000)", the static content will be given the port 5000 while testing. Is there any way around this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to make this work in both development and deployment? I seem to have issues with this locally...