-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_shortner.py
35 lines (27 loc) · 879 Bytes
/
url_shortner.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
import redis
import base64
from clastic import Application, render_basic
def hello(url='please add the url'):
encode = base64.b64encode(url)[:5]
r.set(encode, url)
return 'localhost:5000/mapping/' + encode
def shorten(request):
for k in request.values:
if k in "url":
url = request.values[k]
encode = base64.b64encode(url)[:8]
r.set(encode, url)
return 'localhost:5000/mapping/' + encode
def decode(encode=''):
if encode is '':
return 'No url to return'
else:
return r.get(encode)
routes = [('/', hello, render_basic),
('/shorten', shorten, render_basic),
('/mapping/<encode>', decode, render_basic)]
# ,
# ('/<url>', hello, render_basic)]
r = redis.StrictRedis(host='localhost', port=6379, db=0)
app = Application(routes)
app.serve()