-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
75 lines (63 loc) · 1.76 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
import atexit
from flask import (
Flask,
jsonify,
make_response,
render_template,
request,
)
from flask_cors import CORS
from blueprints.api import api
from blueprints.contact import contact
from blueprints.docs import docs
from blueprints.limiter import limiter
from blueprints.seo import seo
from blueprints.stats import stats
from blueprints.url_shortener import url_shortener
from blueprints.cache import cache
from utils.mongo_utils import client
app = Flask(__name__)
CORS(app)
limiter.init_app(app)
cache.init_app(app)
app.register_blueprint(url_shortener)
app.register_blueprint(docs)
app.register_blueprint(seo)
app.register_blueprint(contact)
app.register_blueprint(api)
app.register_blueprint(stats)
@app.errorhandler(404)
def page_not_found(error):
return (
render_template(
"error.html",
error_code="404",
error_message="URL NOT FOUND!",
host_url=request.host_url,
),
404,
)
@app.errorhandler(429)
def ratelimit_handler(e):
if request.path == "/contact":
return render_template(
"contact.html",
error=f"ratelimit exceeded {e.description}",
host_url=request.host_url,
)
if request.path == "/report":
return render_template(
"report.html",
error=f"ratelimit exceeded {e.description}",
host_url=request.host_url,
)
return make_response(jsonify(error=f"ratelimit exceeded {e.description}"), 429)
@atexit.register
def cleanup():
try:
client.close()
print("MongoDB connection closed successfully")
except Exception as e:
print(f"Error closing MongoDB connection: {e}")
if __name__ == "__main__":
app.run(port=8000, use_reloader=False)