OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import sys | 6 import sys |
7 import os | 7 import os |
8 | 8 |
9 # Add the original server location to sys.path so we are able to import | 9 # Add the original server location to sys.path so we are able to import |
10 # modules from there. | 10 # modules from there. |
11 SERVER_PATH = 'chrome/common/extensions/docs/server2/' | 11 SERVER_PATH = 'chrome/common/extensions/docs/server2/' |
12 if os.path.abspath(SERVER_PATH) not in sys.path: | 12 if os.path.abspath(SERVER_PATH) not in sys.path: |
13 sys.path.append(os.path.abspath(SERVER_PATH)) | 13 sys.path.append(os.path.abspath(SERVER_PATH)) |
14 | 14 |
| 15 import branch_utility |
15 import logging | 16 import logging |
16 import urlfetch | 17 import urlfetch |
17 | 18 |
18 from google.appengine.ext import webapp | 19 from google.appengine.ext import webapp |
19 from google.appengine.ext.webapp.util import run_wsgi_app | 20 from google.appengine.ext.webapp.util import run_wsgi_app |
20 | 21 |
21 from branch_utility import BranchUtility | |
22 from local_fetcher import LocalFetcher | 22 from local_fetcher import LocalFetcher |
| 23 from server_instance import ServerInstance |
23 from subversion_fetcher import SubversionFetcher | 24 from subversion_fetcher import SubversionFetcher |
24 from template_fetcher import TemplateFetcher | 25 from template_data_source import TemplateDataSource |
25 from template_utility import TemplateUtility | |
26 | 26 |
27 EXTENSIONS_PATH = 'chrome/common/extensions/' | 27 EXTENSIONS_PATH = 'chrome/common/extensions/' |
28 DOCS_PATH = 'docs/' | 28 DOCS_PATH = 'docs/' |
29 STATIC_PATH = DOCS_PATH + 'static/' | |
30 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' | 29 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' |
31 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' | 30 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' |
32 | 31 |
33 class MainPage(webapp.RequestHandler): | 32 # Global cache of instances because the Server is recreated for every request. |
34 def _FetchResource(self, fetcher, branch, path): | 33 SERVER_INSTANCES = {} |
35 result = fetcher.FetchResource(branch, path) | 34 |
36 for key in result.headers: | 35 class Server(webapp.RequestHandler): |
37 self.response.headers[key] = result.headers[key] | 36 def _GetInstanceForBranch(self, branch): |
38 return result.content | 37 if branch in SERVER_INSTANCES: |
| 38 return SERVER_INSTANCES[branch] |
| 39 if branch == 'local': |
| 40 fetcher = LocalFetcher(EXTENSIONS_PATH) |
| 41 # No cache for local doc server. |
| 42 cache_timeout_seconds = 0 |
| 43 else: |
| 44 fetcher = SubversionFetcher(branch, EXTENSIONS_PATH, urlfetch) |
| 45 cache_timeout_seconds = 300 |
| 46 data_source = TemplateDataSource( |
| 47 fetcher, |
| 48 [PUBLIC_TEMPLATE_PATH, PRIVATE_TEMPLATE_PATH], |
| 49 cache_timeout_seconds) |
| 50 SERVER_INSTANCES[branch] = ServerInstance(data_source, fetcher) |
| 51 return SERVER_INSTANCES[branch] |
| 52 |
| 53 def _HandleRequest(self, path): |
| 54 channel_name = branch_utility.GetChannelNameFromPath(path) |
| 55 branch = branch_utility.GetBranchNumberForChannelName(channel_name, |
| 56 urlfetch) |
| 57 self._GetInstanceForBranch(branch).Run(path, self) |
39 | 58 |
40 def get(self): | 59 def get(self): |
41 path = self.request.path | 60 path = self.request.path |
42 path = path.replace('/chrome/extensions/', '') | 61 path = path.replace('/chrome/extensions/', '') |
43 parts = path.split('/') | |
44 filename = parts[-1] | |
45 | |
46 if len(path) > 0 and path[0] == '/': | 62 if len(path) > 0 and path[0] == '/': |
47 path = path.strip('/') | 63 path = path.strip('/') |
48 | 64 self._HandleRequest(path) |
49 branch_util = BranchUtility(urlfetch) | |
50 channel_name = branch_util.GetChannelNameFromPath(path) | |
51 branch = branch_util.GetBranchNumberForChannelName(channel_name) | |
52 | |
53 if channel_name == 'local': | |
54 fetcher = LocalFetcher(EXTENSIONS_PATH) | |
55 else: | |
56 fetcher = SubversionFetcher(EXTENSIONS_PATH, urlfetch) | |
57 template_fetcher = TemplateFetcher(branch, fetcher) | |
58 template_util = TemplateUtility() | |
59 | |
60 template = template_fetcher[PUBLIC_TEMPLATE_PATH + filename] | |
61 if template: | |
62 content = template_util.RenderTemplate(template, '{"test": "Hello"}') | |
63 else: | |
64 logging.info('Template for %s not found.' % filename) | |
65 | |
66 try: | |
67 content = self._FetchResource(fetcher, branch, STATIC_PATH + filename) | |
68 except: | |
69 content = 'File not found.' | |
70 | |
71 self.response.out.write(content) | |
72 | |
73 application = webapp.WSGIApplication([ | |
74 ('/.*', MainPage), | |
75 ], debug=False) | |
76 | 65 |
77 def main(): | 66 def main(): |
78 run_wsgi_app(application) | 67 handlers = [ |
| 68 ('/.*', Server), |
| 69 ] |
| 70 run_wsgi_app(webapp.WSGIApplication(handlers, debug=False)) |
79 | 71 |
80 | 72 |
81 if __name__ == '__main__': | 73 if __name__ == '__main__': |
82 main() | 74 main() |
OLD | NEW |