Add static domain middleware for testing compiled CSS.

In production, static files are located in the root of the static file
host. On a local reddit install, they're only available in /static/.
This middleware allows you to test the production static files layout in
a development environment.

If debug=true and uncompressedJS=false are set in the ini, this middleware
simulates a static files host by prepending /static/ to the URL before
handing the request off to the static files app.

To use this for testing purposes, add an additional domain alias like
redditstatic.local to your hosts file and set static_domain in the ini.
Compiled static files will then load from the app using this middleware.
This commit is contained in:
Max Goodman
2012-05-15 17:45:05 -07:00
parent dcb85c6d22
commit 4994c93652

View File

@@ -435,6 +435,18 @@ class RewriteMiddleware(object):
return self.app(environ, start_response)
class StaticTestMiddleware(object):
def __init__(self, app, static_path, domain):
self.app = app
self.static_path = static_path
self.domain = domain
def __call__(self, environ, start_response):
if environ['HTTP_HOST'] == self.domain:
environ['PATH_INFO'] = self.static_path.rstrip('/') + environ['PATH_INFO']
return self.app(environ, start_response)
raise httpexceptions.HTTPNotFound()
class LimitUploadSize(object):
"""
Middleware for restricting the size of uploaded files (such as
@@ -575,7 +587,9 @@ def make_app(global_conf, full_stack=True, **app_conf):
javascripts_app = StaticJavascripts()
static_app = StaticURLParser(config['pylons.paths']['static_files'])
static_cascade = [static_app, javascripts_app, app]
if config['r2.plugins']:
g = config['pylons.g']
if config['r2.plugins'] and g.config['uncompressedJS']:
plugin_static_apps = Cascade([StaticURLParser(plugin.static_dir)
for plugin in config['r2.plugins']])
static_cascade.insert(0, plugin_static_apps)
@@ -584,4 +598,8 @@ def make_app(global_conf, full_stack=True, **app_conf):
#add the rewrite rules
app = RewriteMiddleware(app)
if not g.config['uncompressedJS'] and g.config['debug']:
static_fallback = StaticTestMiddleware(static_app, g.config['static_path'], g.config['static_domain'])
app = Cascade([static_fallback, app])
return app