-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
39 lines (30 loc) · 926 Bytes
/
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
#!/usr/bin/python3
# rpyshort
# Simple python program to let you shorten any URL
from flask import Flask, redirect, request
import redis, uuid
red = redis.Redis(host='localhost', port=6379, db=1, decode_responses=True)
app = Flask(__name__)
@app.route('/<url_id>', methods=['GET'])
def redirect_func(url_id):
if red.exists(url_id):
url = red.get(url_id)
return(redirect(url, code=302))
else:
return(f'ID {url_id} wasn\'t found.')
@app.route('/short', methods=['POST'])
def shorten_url():
url_data = request.form['url_to_shorten']
url_uuid = uuid.uuid4().hex[:8]
red.set(url_uuid, url_data)
print(url_uuid)
return(url_uuid)
@app.route('/short_json', methods=['POST'])
def shorten_url():
url_data = request.get_json()
url_uuid = uuid.uuid4().hex[:8]
red.set(url_uuid, url_data['url'])
print(url_uuid)
return(url_uuid)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)