-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (36 loc) · 1.14 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
from flask import (
Flask,
render_template,
request,
redirect
)
from db.database import SessionLocal
from db.models import Link
from utils.short_link_helper import (
generate_short_link,
save_link,
get_link
)
app = Flask(__name__)
db = SessionLocal()
@app.route("/", methods=["POST", "GET"])
def index():
if request.method == "POST":
url = request.form["link"]
short_url, url = generate_short_link(url)
save_link(url=url, short_url=short_url)
return redirect(f"/short_links/{short_url}")
else:
return render_template("index.html")
@app.route("/short_links/<short_url>", methods=["GET"])
def short_link(short_url):
new_link = get_link(short_url=short_url, request=request)
return render_template("short_link.html", new_link=new_link)
@app.route("/<short_url>", methods=["GET"])
def redirect_link(short_url):
existing_link = db.query(Link).filter_by(short_url=short_url).first()
if existing_link:
return redirect(existing_link.url)
return "Your short link is deactivated"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)