Index: chrome/common/extensions/docs/server2/echo_handler.py |
diff --git a/chrome/common/extensions/docs/server2/echo_handler.py b/chrome/common/extensions/docs/server2/echo_handler.py |
index 20ae79cfb5e4d6d8adfa6c16009406c72a8fbc9f..78195cb94a22d9477d5880410b23507ae2eba67c 100755 |
--- a/chrome/common/extensions/docs/server2/echo_handler.py |
+++ b/chrome/common/extensions/docs/server2/echo_handler.py |
@@ -12,70 +12,62 @@ SERVER_PATH = 'chrome/common/extensions/docs/server2/' |
if os.path.abspath(SERVER_PATH) not in sys.path: |
sys.path.append(os.path.abspath(SERVER_PATH)) |
+import branch_utility |
import logging |
import urlfetch |
from google.appengine.ext import webapp |
from google.appengine.ext.webapp.util import run_wsgi_app |
-from branch_utility import BranchUtility |
from local_fetcher import LocalFetcher |
+from server_instance import ServerInstance |
from subversion_fetcher import SubversionFetcher |
-from template_fetcher import TemplateFetcher |
-from template_utility import TemplateUtility |
+from template_data_source import TemplateDataSource |
EXTENSIONS_PATH = 'chrome/common/extensions/' |
DOCS_PATH = 'docs/' |
-STATIC_PATH = DOCS_PATH + 'static/' |
PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' |
PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' |
-class MainPage(webapp.RequestHandler): |
- def _FetchResource(self, fetcher, branch, path): |
- result = fetcher.FetchResource(branch, path) |
- for key in result.headers: |
- self.response.headers[key] = result.headers[key] |
- return result.content |
+# Global cache of instances because the Server is recreated for every request. |
+SERVER_INSTANCES = {} |
+ |
+class Server(webapp.RequestHandler): |
+ def _GetInstanceForBranch(self, branch): |
+ if branch in SERVER_INSTANCES: |
+ return SERVER_INSTANCES[branch] |
+ if branch == 'local': |
+ fetcher = LocalFetcher(EXTENSIONS_PATH) |
+ # No cache for local doc server. |
+ cache_timeout_seconds = 0 |
+ else: |
+ fetcher = SubversionFetcher(branch, EXTENSIONS_PATH, urlfetch) |
+ cache_timeout_seconds = 300 |
+ data_source = TemplateDataSource( |
+ fetcher, |
+ [PUBLIC_TEMPLATE_PATH, PRIVATE_TEMPLATE_PATH], |
+ cache_timeout_seconds) |
+ SERVER_INSTANCES[branch] = ServerInstance(data_source, fetcher) |
+ return SERVER_INSTANCES[branch] |
+ |
+ def _HandleRequest(self, path): |
+ channel_name = branch_utility.GetChannelNameFromPath(path) |
+ branch = branch_utility.GetBranchNumberForChannelName(channel_name, |
+ urlfetch) |
+ self._GetInstanceForBranch(branch).Run(path, self) |
def get(self): |
path = self.request.path |
path = path.replace('/chrome/extensions/', '') |
- parts = path.split('/') |
- filename = parts[-1] |
- |
if len(path) > 0 and path[0] == '/': |
path = path.strip('/') |
- |
- branch_util = BranchUtility(urlfetch) |
- channel_name = branch_util.GetChannelNameFromPath(path) |
- branch = branch_util.GetBranchNumberForChannelName(channel_name) |
- |
- if channel_name == 'local': |
- fetcher = LocalFetcher(EXTENSIONS_PATH) |
- else: |
- fetcher = SubversionFetcher(EXTENSIONS_PATH, urlfetch) |
- template_fetcher = TemplateFetcher(branch, fetcher) |
- template_util = TemplateUtility() |
- |
- template = template_fetcher[PUBLIC_TEMPLATE_PATH + filename] |
- if template: |
- content = template_util.RenderTemplate(template, '{"test": "Hello"}') |
- else: |
- logging.info('Template for %s not found.' % filename) |
- |
- try: |
- content = self._FetchResource(fetcher, branch, STATIC_PATH + filename) |
- except: |
- content = 'File not found.' |
- |
- self.response.out.write(content) |
- |
-application = webapp.WSGIApplication([ |
- ('/.*', MainPage), |
-], debug=False) |
+ self._HandleRequest(path) |
def main(): |
- run_wsgi_app(application) |
+ handlers = [ |
+ ('/.*', Server), |
+ ] |
+ run_wsgi_app(webapp.WSGIApplication(handlers, debug=False)) |
if __name__ == '__main__': |