-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (27 loc) · 1.05 KB
/
main.py
File metadata and controls
36 lines (27 loc) · 1.05 KB
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
from flask import Flask, render_template, request, flash, redirect, abort
from inputform import InputForm
import secrets
app = Flask(__name__)
app.config['SECRET_KEY'] = "j34D^$hIh%i7utv0"
shortened_urls = []
@app.route("/", methods=["GET", "POST"])
def index():
form = InputForm()
if request.method == "POST":
if form.validate_on_submit():
destination_url = form.url_input.data
id = secrets.token_urlsafe(8)
shortened_urls.append({"destination-url": destination_url, "id": id})
form.url_input.data = ""
flash(f"Success! Your Shortened URL: {request.base_url + id}", category="success message")
else:
flash("Invalid URL!", category="error message")
return render_template("index.html", form=form)
@app.route("/<id>")
def redirect_user(id):
for shortened_url in shortened_urls:
if id == shortened_url["id"]:
return redirect(shortened_url["destination-url"])
return abort(404)
if __name__ == "__main__":
app.run(port=5000, debug=True)