-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (52 loc) · 1.97 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
from flask import Flask, json, request, jsonify, redirect, render_template
from werkzeug.utils import redirect
from werkzeug.wrappers import response
from db import Database
from dotenv import dotenv_values
from tiny_id import UniqueString
from Schema import Create, Find, FindId
import os
client = Database.Connect()
app = Flask(__name__, static_folder="./static")
config = dotenv_values(".env")
# app.config["CACHE_TYPE"] = "null"
@app.route("/", methods=["GET", "POST"])
def Home():
try:
if request.method == "POST":
long_url = request.form["url"]
id = UniqueString()
d_b = client["myFirstDatabase"]
collection = d_b["url"]
flag, code, lu = Find(collection, long_url)
if flag == False:
short_url = "https://sbbi.herokuapp.com/" + id
data = {"code": id, "long_url": long_url, "short_url": short_url}
response = {"long_url": long_url, "short_url": short_url}
Create(collection, data)
return jsonify(response), 201
elif code != None and lu != None:
short_url = "https://sbbi.herokuapp.com/" + code
response = {"long_url": lu, "short_url": short_url}
return jsonify(response), 200
elif request.method == "GET":
return render_template("index.html", title="TinyUrl")
except Exception as e:
response = {"messsage": "Server Error!"}
return jsonify(response), 500
@app.route("/<id>", methods=["GET"])
def redirect_url(id):
d_b = client["myFirstDatabase"]
collection = d_b["url"]
lu = FindId(collection, id)
print(lu)
if lu == None:
response = {"message": "Url may be expired"}
return jsonify(response), 404
else:
return redirect(lu)
if __name__ == "__main__":
# port = dict(config)["PORT"]
# host = dict(config)["HOST"]
port = int(os.environ.get("PORT"))
app.run(host="0.0.0.0", port=port)