Intuit correct SCRIPT_NAME setting from public_url

This commit is contained in:
Ryan Kelly
2014-05-01 14:27:59 +10:00
parent 4e6bef8086
commit 02d3cfdc3b

View File

@@ -3,7 +3,11 @@
# You can obtain one at http://mozilla.org/MPL/2.0/. # You can obtain one at http://mozilla.org/MPL/2.0/.
import os import os
from urlparse import urlparse
from pyramid.response import Response from pyramid.response import Response
from pyramid.events import NewRequest, subscriber
import mozsvc.config import mozsvc.config
@@ -19,6 +23,8 @@ def includeme(config):
if public_url is None: if public_url is None:
raise RuntimeError("you much configure syncserver.public_url") raise RuntimeError("you much configure syncserver.public_url")
public_url = public_url.rstrip("/") public_url = public_url.rstrip("/")
settings["syncserver.public_url"] = public_url
secret = settings.get("syncserver.secret") secret = settings.get("syncserver.secret")
if secret is None: if secret is None:
secret = os.urandom(32).encode("hex") secret = os.urandom(32).encode("hex")
@@ -76,6 +82,7 @@ def includeme(config):
settings["cef.product"] = "syncserver" settings["cef.product"] = "syncserver"
# Include the relevant sub-packages. # Include the relevant sub-packages.
config.scan("syncserver")
config.include("syncstorage", route_prefix="/storage") config.include("syncstorage", route_prefix="/storage")
config.include("tokenserver", route_prefix="/token") config.include("tokenserver", route_prefix="/token")
@@ -87,6 +94,21 @@ def includeme(config):
config.add_view(itworks, route_name='itworks') config.add_view(itworks, route_name='itworks')
@subscriber(NewRequest)
def fixup_script_name(event):
"""Event-listener to fix up SCRIPT_NAME based on public_url setting.
This is a simple little trick to avoid futzing with configuration in
multiple places. The public_url setting tells us exactly what the root
URL of the app should be, so we can use it to infer the proper value of
SCRIPT_NAME without depending on it being configured in the WSGI server.
"""
request = event.request
public_url = request.registry.settings["syncserver.public_url"]
if not request.script_name:
request.script_name = urlparse(public_url).path.rstrip("/")
def get_configurator(global_config, **settings): def get_configurator(global_config, **settings):
"""Load a SyncStorge configurator object from deployment settings.""" """Load a SyncStorge configurator object from deployment settings."""
config = mozsvc.config.get_configurator(global_config, **settings) config = mozsvc.config.get_configurator(global_config, **settings)