OLD | NEW |
---|---|
1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 # This script is used to redirect HTTP requests to retrieve the correct | 5 # This script is used to redirect HTTP requests to retrieve the correct |
6 # files from Google Cloud Storage. | 6 # files from Google Cloud Storage. |
7 | 7 |
8 import logging | 8 import logging |
9 import urllib2 | 9 import urllib2 |
10 import os | 10 import os |
(...skipping 24 matching lines...) Expand all Loading... | |
35 if os.environ['SERVER_SOFTWARE'].startswith('Development'): | 35 if os.environ['SERVER_SOFTWARE'].startswith('Development'): |
36 self.GetLocal() | 36 self.GetLocal() |
37 else: | 37 else: |
38 self.GetGoogleStorage() | 38 self.GetGoogleStorage() |
39 | 39 |
40 def GetLocal(self): | 40 def GetLocal(self): |
41 """ Used for local, dev server HTTP requests. """ | 41 """ Used for local, dev server HTTP requests. """ |
42 path = LOCAL_PATH + self.request.path[len('docs/'):] | 42 path = LOCAL_PATH + self.request.path[len('docs/'):] |
43 with open(path, 'r') as file: | 43 with open(path, 'r') as file: |
44 result = file.read() | 44 result = file.read() |
45 self.HandleCacheAge(path) | 45 # The cache age should be zero seconds if being run locally. |
46 self.response.headers['Cache-Control'] = 'max-age=' + \ | |
Emily Fortuna
2013/08/15 20:26:23
the pythonic way to write this line is this:
self.
Tate Mandel
2013/08/15 20:34:04
Done.
| |
47 str(0) + ',s-maxage=' + str(0) | |
46 self.response.out.write(result) | 48 self.response.out.write(result) |
47 | 49 |
48 def GetGoogleStorage(self): | 50 def GetGoogleStorage(self): |
49 """ Used for App Engine HTTP requests. """ | 51 """ Used for App Engine HTTP requests. """ |
50 version_key = blobstore.create_gs_key(GS_VERSION) | 52 version_key = blobstore.create_gs_key(GS_VERSION) |
51 blob_reader = blobstore.BlobReader(version_key) | 53 blob_reader = blobstore.BlobReader(version_key) |
52 version = blob_reader.read() | 54 version = blob_reader.read() |
53 path = GS_PATH + version + self.request.path[len('docs/'):] | 55 path = GS_PATH + version + self.request.path[len('docs/'):] |
54 gs_key = blobstore.create_gs_key(path) | 56 gs_key = blobstore.create_gs_key(path) |
55 self.HandleCacheAge(path) | 57 self.HandleCacheAge(path) |
56 self.send_blob(gs_key) | 58 self.send_blob(gs_key) |
57 | 59 |
58 def HandleCacheAge(self, path): | 60 def HandleCacheAge(self, path): |
59 """ Assigns a document a length of time it will live in the cache. """ | 61 """ Assigns a document a length of time it will live in the cache. """ |
60 age = None | 62 age = None |
61 if re.search(r'(png|jpg)$', path): | 63 if re.search(r'(png|jpg)$', path): |
62 age = ONE_DAY | 64 age = ONE_DAY |
63 elif path.endswith('.ico'): | 65 elif path.endswith('.ico'): |
64 age = ONE_WEEK | 66 age = ONE_WEEK |
65 else: | 67 else: |
66 age = ONE_HOUR | 68 age = ONE_HOUR |
67 self.response.headers['Cache-Control'] = 'max-age=' + \ | 69 self.response.headers['Cache-Control'] = 'max-age=' + \ |
68 str(age) + ',s-maxage=' + str(age) | 70 str(age) + ',s-maxage=' + str(age) |
69 | 71 |
70 application = WSGIApplication( | 72 application = WSGIApplication( |
71 [ | 73 [ |
72 ('/docs/.*', RequestHandler), | 74 ('/docs/.*', RequestHandler), |
73 ], debug=True) | 75 ], debug=True) |
OLD | NEW |