-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp4.py
45 lines (37 loc) · 859 Bytes
/
app4.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
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask import request, jsonify
app = Flask(__name__)
urls = [
{
'id': 1,
's_path': 'abc',
'l_url': 'https://www.google.com'
}
]
# http://bit.ly/2tUMEgL
@app.route('/<string:url>')
def url_shorten(url):
for single_url in urls:
if url == single_url['s_path']:
return redirect(single_url['l_url'])
return 'request url doesnt exist'
import time
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/add_url', methods=['POST'])
def add_url():
if request.form['url']:
url_payload = {
'id': len(urls) + 1,
's_path': str(time.time())[0:5],
'l_url': request.form['url']
}
urls.append(url_payload)
return jsonify(urls)
# run the server
if __name__ == '__main__':
app.run(debug=True)