# HG changeset patch # User Ludovic Chabant # Date 1450577176 28800 # Node ID 657384f08ca339c2d263cf17fc14fa80bdaf4f6c # Parent bc23465ed1b4feb7e37bc1fbbfab753d2f6a057c serve: Make it possible to preview pages with a custom root URL. diff -r bc23465ed1b4 -r 657384f08ca3 piecrust/serving/server.py --- a/piecrust/serving/server.py Sat Dec 19 18:05:28 2015 -0800 +++ b/piecrust/serving/server.py Sat Dec 19 18:06:16 2015 -0800 @@ -72,11 +72,12 @@ class Server(object): def __init__(self, root_dir, debug=False, sub_cache_dir=None, enable_debug_info=True, - static_preview=True): + root_url='/', static_preview=True): self.root_dir = root_dir self.debug = debug self.sub_cache_dir = sub_cache_dir self.enable_debug_info = enable_debug_info + self.root_url = root_url self.static_preview = static_preview self._page_record = ServeRecord() self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server') @@ -109,14 +110,15 @@ # Create the app for this request. app = get_app_for_server(self.root_dir, debug=self.debug, - sub_cache_dir=self.sub_cache_dir) + sub_cache_dir=self.sub_cache_dir, + root_url=self.root_url) if (app.config.get('site/enable_debug_info') and self.enable_debug_info and '!debug' in request.args): app.config.set('site/show_debug_info', True) # We'll serve page assets directly from where they are. - app.env.base_asset_url_format = '/_asset/%path%' + app.env.base_asset_url_format = self.root_url + '_asset/%path%' # Let's see if it can be a page asset. response = self._try_serve_page_asset(app, environ, request) @@ -140,7 +142,8 @@ raise InternalServerError(msg) from ex def _try_serve_asset(self, environ, request): - rel_req_path = request.path.lstrip('/').replace('/', os.sep) + offset = len(self.root_url) + rel_req_path = request.path[offset:].replace('/', os.sep) if request.path.startswith('/_cache/'): # Some stuff needs to be served directly from the cache directory, # like LESS CSS map files. @@ -156,10 +159,11 @@ return None def _try_serve_page_asset(self, app, environ, request): - if not request.path.startswith('/_asset/'): + if not request.path.startswith(self.root_url + '_asset/'): return None - full_path = os.path.join(app.root_dir, request.path[len('/_asset/'):]) + offset = len(self.root_url + '_asset/') + full_path = os.path.join(app.root_dir, request.path[offset:]) if not os.path.isfile(full_path): return None diff -r bc23465ed1b4 -r 657384f08ca3 piecrust/serving/util.py --- a/piecrust/serving/util.py Sat Dec 19 18:05:28 2015 -0800 +++ b/piecrust/serving/util.py Sat Dec 19 18:06:16 2015 -0800 @@ -16,11 +16,12 @@ logger = logging.getLogger(__name__) -def get_app_for_server(root_dir, debug=False, sub_cache_dir=None): +def get_app_for_server(root_dir, debug=False, sub_cache_dir=None, + root_url='/'): app = PieCrust(root_dir=root_dir, debug=debug) if sub_cache_dir: app._useSubCacheDir(sub_cache_dir) - app.config.set('site/root', '/') + app.config.set('site/root', root_url) app.config.set('server/is_serving', True) return app