-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
89 lines (78 loc) · 2.81 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import string
from flask import Flask,render_template,redirect,url_for,request,jsonify
from flask_wtf import FlaskForm
from wtforms.fields import StringField,SubmitField
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = 'uKUAvkuscabZjkas'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
db = SQLAlchemy(app)
base62 = list(string.digits + string.ascii_lowercase + string.ascii_uppercase)
class Mapping(db.Model):
id = db.Column(db.Integer,primary_key=True)
long_url = db.Column(db.String(200),nullable=False)
short_url = db.Column(db.String(200),nullable=False)
class UrlForm(FlaskForm):
url = StringField('Enter your url here:')
submit = SubmitField('Submit')
@app.before_first_request
def create_db():
db.create_all()
@app.route('/',methods=['POST','GET'])
def index():
form = UrlForm()
if form.validate_on_submit():
long_url = form.url.data
last_id = len(Mapping.query.all())
#logic for short url
rem = []
while True:
remainder = last_id%62
rem.append(base62[remainder])
last_id = int(last_id/62)
if last_id == 0:
break
rem.reverse()
short_url = ''.join(rem)
entry = Mapping(long_url=long_url,short_url=short_url)
db.session.add(entry)
db.session.commit()
return redirect(url_for('show_url',short_url=entry.short_url))
return render_template('index.html',form=form)
@app.route("/shorten",methods=["POST"])
def shorten_url():
if request.method == "POST":
long_url = request.form['url']
last_id = len(Mapping.query.all())
#logic for short url
rem = []
while True:
remainder = last_id%62
rem.append(base62[remainder])
last_id = int(last_id/62)
if last_id == 0:
break
rem.reverse()
short_url = ''.join(rem)
entry = Mapping(long_url=long_url,short_url=short_url)
db.session.add(entry)
db.session.commit()
host = request.headers.get("host")
return jsonify({"shorten_url":"{}/{}".format(host,short_url)})
@app.route('/short_url/<string:short_url>')
def show_url(short_url):
host = request.headers.get('host')
url = '{}/{}'.format(host,short_url)
return render_template('short_url.html',url=url)
@app.route('/<string:short_url>')
def url_redirect(short_url):
result = Mapping.query.filter_by(short_url=short_url).first()
if result is None:
return '<h2>No such url found</h2>'
return redirect('http://{}'.format(result.long_url))
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)