-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
34 lines (28 loc) · 939 Bytes
/
app.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
from flask import Flask
from flask import render_template, request,redirect
import string
import random
app = Flask(__name__)
app.config['BASE_URL'] = 'http://localhost:5000/'
url_map= {}
def generate_short_link():
char = string.ascii_letters + string.digits
return ''.join(random.choice(char) for _ in range(4))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/shorten', methods=['POST'])
def shorten_url():
original_url = request.form['url']
short_url = generate_short_link()
url_map[short_url] = original_url
return render_template('short.html', short_url=app.config['BASE_URL'] + short_url)
@app.route('/<short_url>')
def redirect_to_original(short_url):
original_url = url_map.get(short_url)
if original_url:
return redirect(original_url)
else:
return render_template('not_found.html'), 404
if __name__ == '__main__':
app.run(debug=True)