From 4994c93652926ef00ee1b04c7509a39508fdbe0b Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Tue, 15 May 2012 17:45:05 -0700 Subject: [PATCH] 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. --- r2/r2/config/middleware.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/r2/r2/config/middleware.py b/r2/r2/config/middleware.py index 437664dcc..075f3f5cb 100644 --- a/r2/r2/config/middleware.py +++ b/r2/r2/config/middleware.py @@ -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