-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
54 lines (42 loc) · 1.46 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
from flask import Flask, render_template, request, redirect, jsonify
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ['POSTGRES_URL']
db = SQLAlchemy(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/utility/create", methods=["GET"])
def create():
data = dict()
url = request.args.get("longUrl")
shortName = request.args.get("shortName")
print("==========================>",url, shortName)
if(Links.query.filter_by(shortName=shortName).first()):
print(Links.query.filter_by(shortName=shortName).first())
data["valid"] = False
return data
link = Links(shortName=shortName, url=url)
db.session.add(link)
db.session.commit()
data["url"] = f'{request.url_root}{shortName}'
data["valid"] = True
return jsonify(data)
@app.route("/getEnv")
def getEnv():
return jsonify(os.environ)
@app.route("/<shortName>")
def navigate(shortName):
if(Links.query.filter_by(shortName=shortName).first()):
link = Links.query.filter_by(shortName=shortName).first()
return redirect(link.url)
else:
return render_template("index.html")
class Links(db.Model):
shortName = db.Column(db.Text, primary_key=True)
url = db.Column(db.Text, nullable=False)
def __repr__(self):
return f"[{self.shortName},{self.url}]"
if(__name__ == "__main__"):
app.run("0.0.0.0", 8080,debug=False)