/
URL_shortner.py
46 lines (35 loc) · 1.04 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask,render_template,request
import uuid
import validators
import redis
app = Flask(__name__)
r = redis.StrictRedis(host='localhost', port=6379, db=0)
#Check database connection
def checkConnection(r):
try:
if r.ping() == True:
return 200
except Exception as e:
return 400
def uuid_gen(url):
if url is None:
return 401
else:
return str(uuid.uuid3(uuid.NAMESPACE_DNS,url))[:7]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/shorten_url',methods=['POST'])
def shorten_url():
url = request.form['url']
if validators.url(url):
#All the database operations from here follow atomic operations
pipe = r.pipeline()
unique_id = uuid_gen(url)
pipe.set('localhost:5000/'+unique_id, url)
result = pipe.execute()
return render_template('short_url.html',new_url='localhost:5000/'+unique_id,result=url)
else:
return "Please enter a valid url"
if __name__ == '__main__':
app.run()