-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
39 lines (31 loc) · 1.05 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
from flask import Flask,render_template, request, jsonify, url_for, redirect, abort
from flask_pymongo import PyMongo
import shortid
import re
app = Flask(__name__)
mongodb = PyMongo(app)
def validate_url(url):
regex = re.compile('^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$', re.IGNORECASE)
return regex.match(url)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<string:short_id>')
def jump(short_id):
record = mongodb.db.short_urls.find_one_or_404({'_id': short_id})
return redirect(record['url'])
@app.route('/gen', methods=['POST'])
def generate_short():
url = request.values.get('url')
if not validate_url(url):
abort(500)
record = mongodb.db.short_urls.find_one({'url': url})
if not record:
record = {
'_id': shortid.generate(),
'url': url
}
mongodb.db.short_urls.insert_one(record)
return jsonify(url_for('jump', short_id=record['_id'], _external=True))
if __name__ == '__main__':
app.run()