Mercurial > piecrust2
comparison piecrust/serving/server.py @ 575:657384f08ca3
serve: Make it possible to preview pages with a custom root URL.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 19 Dec 2015 18:06:16 -0800 |
parents | daf8df5ade7d |
children | 3ceeca7bb71c |
comparison
equal
deleted
inserted
replaced
574:bc23465ed1b4 | 575:657384f08ca3 |
---|---|
70 | 70 |
71 | 71 |
72 class Server(object): | 72 class Server(object): |
73 def __init__(self, root_dir, | 73 def __init__(self, root_dir, |
74 debug=False, sub_cache_dir=None, enable_debug_info=True, | 74 debug=False, sub_cache_dir=None, enable_debug_info=True, |
75 static_preview=True): | 75 root_url='/', static_preview=True): |
76 self.root_dir = root_dir | 76 self.root_dir = root_dir |
77 self.debug = debug | 77 self.debug = debug |
78 self.sub_cache_dir = sub_cache_dir | 78 self.sub_cache_dir = sub_cache_dir |
79 self.enable_debug_info = enable_debug_info | 79 self.enable_debug_info = enable_debug_info |
80 self.root_url = root_url | |
80 self.static_preview = static_preview | 81 self.static_preview = static_preview |
81 self._page_record = ServeRecord() | 82 self._page_record = ServeRecord() |
82 self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server') | 83 self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server') |
83 if sub_cache_dir: | 84 if sub_cache_dir: |
84 self._out_dir = os.path.join(sub_cache_dir, 'server') | 85 self._out_dir = os.path.join(sub_cache_dir, 'server') |
107 if response is not None: | 108 if response is not None: |
108 return response | 109 return response |
109 | 110 |
110 # Create the app for this request. | 111 # Create the app for this request. |
111 app = get_app_for_server(self.root_dir, debug=self.debug, | 112 app = get_app_for_server(self.root_dir, debug=self.debug, |
112 sub_cache_dir=self.sub_cache_dir) | 113 sub_cache_dir=self.sub_cache_dir, |
114 root_url=self.root_url) | |
113 if (app.config.get('site/enable_debug_info') and | 115 if (app.config.get('site/enable_debug_info') and |
114 self.enable_debug_info and | 116 self.enable_debug_info and |
115 '!debug' in request.args): | 117 '!debug' in request.args): |
116 app.config.set('site/show_debug_info', True) | 118 app.config.set('site/show_debug_info', True) |
117 | 119 |
118 # We'll serve page assets directly from where they are. | 120 # We'll serve page assets directly from where they are. |
119 app.env.base_asset_url_format = '/_asset/%path%' | 121 app.env.base_asset_url_format = self.root_url + '_asset/%path%' |
120 | 122 |
121 # Let's see if it can be a page asset. | 123 # Let's see if it can be a page asset. |
122 response = self._try_serve_page_asset(app, environ, request) | 124 response = self._try_serve_page_asset(app, environ, request) |
123 if response is not None: | 125 if response is not None: |
124 return response | 126 return response |
138 logger.error(str(ex)) | 140 logger.error(str(ex)) |
139 msg = "There was an error trying to serve: %s" % request.path | 141 msg = "There was an error trying to serve: %s" % request.path |
140 raise InternalServerError(msg) from ex | 142 raise InternalServerError(msg) from ex |
141 | 143 |
142 def _try_serve_asset(self, environ, request): | 144 def _try_serve_asset(self, environ, request): |
143 rel_req_path = request.path.lstrip('/').replace('/', os.sep) | 145 offset = len(self.root_url) |
146 rel_req_path = request.path[offset:].replace('/', os.sep) | |
144 if request.path.startswith('/_cache/'): | 147 if request.path.startswith('/_cache/'): |
145 # Some stuff needs to be served directly from the cache directory, | 148 # Some stuff needs to be served directly from the cache directory, |
146 # like LESS CSS map files. | 149 # like LESS CSS map files. |
147 full_path = os.path.join(self.root_dir, rel_req_path) | 150 full_path = os.path.join(self.root_dir, rel_req_path) |
148 else: | 151 else: |
154 except OSError: | 157 except OSError: |
155 pass | 158 pass |
156 return None | 159 return None |
157 | 160 |
158 def _try_serve_page_asset(self, app, environ, request): | 161 def _try_serve_page_asset(self, app, environ, request): |
159 if not request.path.startswith('/_asset/'): | 162 if not request.path.startswith(self.root_url + '_asset/'): |
160 return None | 163 return None |
161 | 164 |
162 full_path = os.path.join(app.root_dir, request.path[len('/_asset/'):]) | 165 offset = len(self.root_url + '_asset/') |
166 full_path = os.path.join(app.root_dir, request.path[offset:]) | |
163 if not os.path.isfile(full_path): | 167 if not os.path.isfile(full_path): |
164 return None | 168 return None |
165 | 169 |
166 return make_wrapped_file_response(environ, request, full_path) | 170 return make_wrapped_file_response(environ, request, full_path) |
167 | 171 |