This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.py
187 lines (142 loc) · 5.17 KB
/
router.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from flask import Flask, redirect, request, render_template
app = Flask(__name__)
import os
from datetime import datetime
from urllib import urlencode
from urlparse import urlparse, parse_qs
from couchdb.client import Server
google_apps_verify = os.environ.get('GOOGLE_VERIFICATION', None)
dashboards = [
os.environ.get('HEROKU_HOST', 'localhost:5000'),
]
redirects = {
}
couch_url = os.environ.get('CLOUDANT_URL', 'http://localhost:5984/')
server = Server(couch_url)
db_name = 'router'
def get_db(db_name):
if not db_name in server:
server.create(db_name)
return server[db_name]
db = get_db(db_name)
def add_redirect(source, destination, strip_path=False, strip_query=False,
prefer_destination=False):
if source in db:
doc = db[source]
else:
doc = {}
doc['destination']=destination
doc['strip_path']=strip_path
doc['strip_query']=strip_query
doc['prefer_destination']=prefer_destination
db[source]=doc
def get_redirect(source):
if source in db:
return db[source]['destination']
else:
return
def del_redirect(source):
if source in db:
destination = db[source]['destination']
del(db[source])
return destination
class Router(object):
redirects = {}
failure_template = "snap.html"
def __init__(self, redirects=None):
if redirects:
if not isinstance(redirects, dict):
raise TypeError('Router expects a dict of redirects')
else:
self.redirects = redirects
self.db = get_db(db_name)
def create(self, src, dst):
self.db[src] = {'destination': dst, 'created': datetime.now()}
def current_redirects(self):
ret = []
for name in self.db:
destination = self.db[name]['destination']
ret.append({'source': name.decode('idna'), 'destination': destination.decode('idna')})
for name in self.redirects.keys():
ret.append({'source': name.decode('idna'), 'destination': self.redirects['name'].decode('idna')})
return ret
def redirect(self, hostname, uri):
if hostname in self.db and 'destination' in self.db[hostname]:
destination = self.db[hostname]['destination']
in_db = True
elif hostname in self.redirects:
destination = self.redirects[hostname]
in_db = False
else:
return render_template(self.failure_template, hostname=hostname, uri=uri)
urlbits = urlparse(destination)
if not urlbits.netloc:
urlbits = urlparse('http://' + destination)
if not urlbits.netloc:
return render_template(self.failure_template, hostname=hostname, uri=uri)
scheme = urlbits.scheme or 'http'
destination = scheme + '://' + urlbits.netloc
query = {}
if urlbits.query:
query = parse_qs(urlbits.query)
for key, value in query.items():
query[key] = value[0]
if request.args and (in_db and not self.db[hostname].get('strip_query')):
prefer_destination = in_db and self.db[hostname].get('prefer_destination')
for key, value in request.args.items():
if key in query and prefer_destination:
continue
query[key] = value
if query:
query_string = '?' + urlencode(query)
else:
query_string = ''
if urlbits.fragment:
fragment = '#' + urlbits.fragment
else:
fragment = ''
if in_db and self.db[hostname].get('strip_path'):
path = urlbits.path
else:
path = '/' + '/'.join([
urlbits.path.strip('/'),
uri.lstrip('/')
])
destination += path + query_string + fragment
if os.environ.get('DEBUG_REDIRECT') and True or False:
return destination
return redirect(destination)
rtr = Router(redirects)
class Dash(object):
urls = []
template = 'dash.html'
def __init__(self, urls=None):
if urls:
if not isinstance(urls, list):
raise TypeError('Dash expects a list of urls')
else:
self.urls = urls
def is_dash(self, url):
return url in self.urls
def render(self, uri):
return render_template(self.template, redirects=rtr.current_redirects())
def __call__(self, uri):
return self.render(uri)
dash = Dash(dashboards)
@app.route('/_dash/')
@app.route('/_dash/<path:uri>')
def show_dash(uri=None):
return dash(uri)
@app.route('/')
@app.route('/<path:uri>')
def uri_router(uri=''):
if google_apps_verify and uri == 'google'+google_apps_verify+'.html':
return 'google-site-verification: google'+google_apps_verify+'.html'
hostname = request.host
if dash.is_dash(hostname):
return dash(uri)
return rtr.redirect(hostname, uri)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.debug = os.environ.get('DEBUG') and True or False
app.run('0.0.0.0', port=port)