diff --git a/README.rst b/README.rst index 80e2fb0..155302a 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ the following content:: DEBUG = True SQLACHEMY_ECHO = DEBUG -You can also set the `TESTING` flag to `True` so no mails are sent +You can also set the `TESTING` flag to `True` so no mails are sent (and no exception is raised) while you're on development mode. Deploy it @@ -48,7 +48,12 @@ To deploy it, I'm using gunicorn and supervisord:: Don't forget to set the right permission for your files ! Also, create a `settings.py` file with the appropriate values if you need to -use a different database for instance. +use a different database for instance. You can also set `APPLICATION_ROOT` if +you want to prefix your URLs to serve ihatemonney in the *folder* of a domain, +e.g: + + APPLICATION_ROOT='/budget' + How about the REST API? ======================= @@ -66,7 +71,7 @@ As a developer -------------- The best way to contribute code is to write it and to make a pull request on -github. Please, think about updating and running the tests before asking for +github. Please, think about updating and running the tests before asking for a pull request as it will help us to maintain the code clean and running. To do so:: diff --git a/budget/run.py b/budget/run.py index 845192d..dace736 100644 --- a/budget/run.py +++ b/budget/run.py @@ -4,10 +4,11 @@ from raven.contrib.flask import Sentry from web import main, db, mail from api import api - +from utils import ReverseProxied app = Flask(__name__) app.config.from_object("default_settings") +app.wsgi_app = ReverseProxied(app.wsgi_app, app.config['APPLICATION_ROOT']) app.register_blueprint(main) app.register_blueprint(api) diff --git a/budget/utils.py b/budget/utils.py index 60337fb..9d63fc2 100644 --- a/budget/utils.py +++ b/budget/utils.py @@ -32,3 +32,32 @@ class Redirect303(HTTPException, RoutingException): def get_response(self, environ): return redirect(self.new_url, 303) + + +class ReverseProxied(object): + ''' + Wrap the application in this middleware and configure the + front-end server to add these headers, to let you quietly bind + this to a URL other than / and to an HTTP scheme that is + different than what is used locally. + + Inspired from http://flask.pocoo.org/snippets/35/ + + :param app: the WSGI application + ''' + def __init__(self, app, prefix): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + script_name = self.prefix + if script_name: + environ['SCRIPT_NAME'] = script_name + path_info = environ['PATH_INFO'] + if path_info.startswith(script_name): + environ['PATH_INFO'] = path_info[len(script_name):] + + scheme = environ.get('HTTP_X_SCHEME', '') + if scheme: + environ['wsgi.url_scheme'] = scheme + return self.app(environ, start_response)