Created
November 6, 2014 22:38
-
-
Save blbradley/e9564f6a0c9a1d6c2245 to your computer and use it in GitHub Desktop.
WebOb middleware to force ssl on Heroku
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
import os | |
from webob.dec import wsgify | |
from webob.exc import HTTPMovedPermanently | |
from paste.deploy import loadapp | |
from waitress import serve | |
@wsgify.middleware | |
def ForceSSLMiddleware(req, app): | |
# check that the incoming request was using https | |
if req.headers['X-Forwarded-Proto'] != 'https': | |
https_url = req.url.replace('http://', 'https://') | |
return HTTPMovedPermanently(location=https_url) | |
# override the request scheme to tell the app that we are using https | |
req.scheme = 'https' | |
resp = req.get_response(app) | |
return resp | |
if __name__ == "__main__": | |
port = int(os.environ.get("PORT", 5000)) | |
app = loadapp('config:production.ini', relative_to='.') | |
app = ForceSSLMiddleware(app) | |
serve(app, host='0.0.0.0', port=port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment