-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (68 loc) · 2.09 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
@Author: Timlzh <[email protected]>
"""
import os
from configparser import ConfigParser
from flask import Flask, render_template, request, redirect, abort
from url_shortener import UrlShortener, InvalidUrlError
config = ConfigParser()
config.read('config.ini')
port = config['server']['port']
base_url = config['server']['base_url']
if base_url[-1] != '/':
base_url += '/'
db_host = config['database']['host']
db_port = config['database']['port']
db_login = bool(int(config['database']['login']))
db_user = config['database']['user'] if db_login else None
db_password = config['database']['password'] if db_login else None
urlShortener = UrlShortener((db_host, int(db_port)),
db_login, db_user, db_password)
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
def alert(msg: str, redirect_url: str) -> str:
"""alert
Args:
msg (str): Message to alert
redirect_url (str): Redirect url after alert
Returns:
str: alert script
"""
return f"<script>alert('{msg}');window.location.href='{redirect_url}';</script>"
@app.errorhandler(404)
def page_not_found(_) -> tuple:
"""
404 Error Handler
"""
return render_template('404.html'), 404
@app.route('/')
def index() -> str:
"""index_page
Returns:
str: index page
"""
return render_template('index.html')
@app.route('/gen', methods=['POST'])
def gen() -> str:
"""gen_short_url_page
Returns:
str: short url page
"""
url = request.form['url']
try:
short_url = urlShortener.shorten_url(url)
return render_template('gen.html', url=base_url + short_url)
except InvalidUrlError:
return alert('Invalid URL', '/')
@app.route('/<short_url>', methods=['GET'])
def redirect_short_url(short_url):
"""redirect_short_url_page
Args:
short_url (str): short url
Returns:
str: redirect page
"""
url = urlShortener.get_url(short_url)
return abort(404) if url is None else redirect(url)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(port))