comparison piecrust/serving/server.py @ 666:81d9c3a3a0b5

internal: Get rid of the whole "sub cache" business. * Compute cache keys up front, so the cache directory is only chosen once. * Buffer up config variants to apply before loading the config. Makes it possible to cache variant-resulting configs, too. * Make a factory class to reuse the logic that creates the `PieCrust` object correctly for multi-process workers and such. * Add a test.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Mar 2016 08:22:41 -0800
parents 3ceeca7bb71c
children c2ea75e37540
comparison
equal deleted inserted replaced
665:5dc13c816045 666:81d9c3a3a0b5
20 20
21 logger = logging.getLogger(__name__) 21 logger = logging.getLogger(__name__)
22 22
23 23
24 class WsgiServer(object): 24 class WsgiServer(object):
25 def __init__(self, root_dir, **kwargs): 25 def __init__(self, appfactory, **kwargs):
26 self.server = Server(root_dir, **kwargs) 26 self.server = Server(appfactory, **kwargs)
27 27
28 def __call__(self, environ, start_response): 28 def __call__(self, environ, start_response):
29 return self.server._run_request(environ, start_response) 29 return self.server._run_request(environ, start_response)
30 30
31 31
68 desc += '</p>' 68 desc += '</p>'
69 return desc 69 return desc
70 70
71 71
72 class Server(object): 72 class Server(object):
73 def __init__(self, root_dir, 73 def __init__(self, appfactory,
74 debug=False, theme_site=False, 74 enable_debug_info=True,
75 sub_cache_dir=None, enable_debug_info=True, 75 root_url='/',
76 root_url='/', static_preview=True): 76 static_preview=True):
77 self.root_dir = root_dir 77 self.appfactory = appfactory
78 self.debug = debug
79 self.theme_site = theme_site
80 self.sub_cache_dir = sub_cache_dir
81 self.enable_debug_info = enable_debug_info 78 self.enable_debug_info = enable_debug_info
82 self.root_url = root_url 79 self.root_url = root_url
83 self.static_preview = static_preview 80 self.static_preview = static_preview
84 self._page_record = ServeRecord() 81 self._page_record = ServeRecord()
85 self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server') 82 self._out_dir = os.path.join(
86 if sub_cache_dir: 83 appfactory.root_dir,
87 self._out_dir = os.path.join(sub_cache_dir, 'server') 84 CACHE_DIR,
85 (appfactory.cache_key or 'default'),
86 'server')
88 87
89 def _run_request(self, environ, start_response): 88 def _run_request(self, environ, start_response):
90 try: 89 try:
91 response = self._try_run_request(environ) 90 response = self._try_run_request(environ)
92 return response(environ, start_response) 91 return response(environ, start_response)
109 response = self._try_serve_asset(environ, request) 108 response = self._try_serve_asset(environ, request)
110 if response is not None: 109 if response is not None:
111 return response 110 return response
112 111
113 # Create the app for this request. 112 # Create the app for this request.
114 app = get_app_for_server(self.root_dir, debug=self.debug, 113 app = get_app_for_server(self.appfactory,
115 theme_site=self.theme_site,
116 sub_cache_dir=self.sub_cache_dir,
117 root_url=self.root_url) 114 root_url=self.root_url)
118 if (app.config.get('site/enable_debug_info') and 115 if (app.config.get('site/enable_debug_info') and
119 self.enable_debug_info and 116 self.enable_debug_info and
120 '!debug' in request.args): 117 '!debug' in request.args):
121 app.config.set('site/show_debug_info', True) 118 app.config.set('site/show_debug_info', True)