forked from Polymer/old-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (80 loc) · 3.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
import os
import re
import webapp2
import yaml
from google.appengine.api import app_identity
from google.appengine.api import memcache
from webapp2_extras.routes import RedirectRoute
def get_dirs(root='.'):
a = re.compile(r'^\d.\d(\.\d)?$')
return [x for x in os.listdir(root) if os.path.isdir(x) and a.match(x)]
def get_default_polymer_version():
current_app_version = os.environ['CURRENT_VERSION_ID'].split('.')[0]
default_version = memcache.get('default_version', namespace=current_app_version)
legacy_version = memcache.get('legacy_version', namespace=current_app_version)
if default_version is None or legacy_version is None:
f = open('_config.yml', 'r')
config = yaml.load(f)
if default_version is None:
default_version = config.get('default_version')
memcache.add('default_version', default_version, namespace=current_app_version)
if legacy_version is None:
legacy_version = config.get('legacy_version')
memcache.add('legacy_version', legacy_version, namespace=current_app_version)
return (default_version, legacy_version)
class VersionHandler(webapp2.RequestHandler):
def get(self, version=None):
version_dir, legacy_version_dir = get_default_polymer_version()
if self.request.path.startswith('/latest'):
path = self.request.path.replace('/latest', '/%s' % version_dir)
return self.redirect('%s' % path)
# redirect raw path to new version
if self.request.path == '/':
return self.redirect('/%s/' % (version_dir))
# redirect unversioned legacy URLs
return self.redirect('/%s%s' % (legacy_version_dir, self.request.path))
class ObsoleteVersionHandler(webapp2.RequestHandler):
def get(self, version=None):
logging.warning("OVH got %s" % self.request.path)
version_dir, legacy_dir = get_default_polymer_version()
path = re.sub(r'^/[^/]*/', ('/%s/' % (version_dir)), self.request.path)
logging.warning("OVH redirect to %s" % path)
return self.redirect('%s' % path)
routes = [
RedirectRoute('/apps/topeka/', name='topeka',
redirect_to='https://polymer-topeka.appspot.com/', strict_slash=True),
RedirectRoute('/apps/designer/', name='designer',
redirect_to='https://polymer-designer.appspot.com/', strict_slash=True),
RedirectRoute('/tools/designer/', name='designer',
redirect_to='https://polymer-designer.appspot.com/', strict_slash=True),
RedirectRoute('/apps/polymer-tutorial/finished/', name='tutorial',
redirect_to='https://polymer-tut.appspot.com/', strict_slash=True),
RedirectRoute('/docs/elements/core-elements.html', name='core-elements',
redirect_to='/docs/elements/', strict_slash=True),
RedirectRoute('/docs/elements/paper-elements.html', name='paper-elements',
redirect_to='/docs/elements/', strict_slash=True),
('/latest.*', VersionHandler),
('/docs/.*', VersionHandler),
('/resources/.*', VersionHandler),
('/platform/.*', VersionHandler),
('/articles/.*', VersionHandler),
('/0.[89]/.*', ObsoleteVersionHandler),
('/$', VersionHandler),
]
app = webapp2.WSGIApplication(routes=routes, debug=False)