-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (72 loc) · 2.29 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
96
97
98
99
100
101
from __future__ import annotations
import os
import databases
from cmyui import Ansi
from cmyui import log
from quart import Quart
from quart import render_template
from quart import request
from werkzeug.exceptions import HTTPException
import settings
from constants.privileges import Privileges
from objects.user import User
# app
app = Quart(__name__)
app.secret_key = os.urandom(32)
app.permanent_session_lifetime = 86400 # 1 day
# expose objects to jinja
exposed_objects = {"User": User, "Privileges": Privileges}
for obj in exposed_objects:
app.jinja_env.globals[obj] = exposed_objects[obj]
# before serving
@app.before_serving
async def before_serving():
log("=== undeniab.ly ===", Ansi.LRED)
# Check database connection
try:
log(f"Connecting to database...", Ansi.LGREEN)
db = databases.Database(settings.DB_DSN)
await db.connect()
await db.disconnect()
except Exception as e:
log(f"Failed to connect to database: {e}", Ansi.LRED)
log("===================", Ansi.LRED)
os._exit(1)
log("===================", Ansi.LRED)
# before request
@app.before_request
async def before_request():
# maintenance mode
if settings.MAINTENANCE:
if "/static/" not in request.path:
return await render_template("maintenance.html"), 503
# register blueprints
from blueprints.home import home
app.register_blueprint(home)
from blueprints.store import store
app.register_blueprint(store)
from blueprints.login import login
app.register_blueprint(login)
from blueprints.# import #
app.register_blueprint(#)
from blueprints.logout import logout
app.register_blueprint(logout)
from blueprints.dashboard import dashboard
app.register_blueprint(dashboard, url_prefix="/dashboard")
# error handling
@app.errorhandler(Exception)
async def handle_exception(exception):
if isinstance(exception, HTTPException):
# 404
if exception.code == 404:
return await render_template("404.html"), 404
# 5XX
log(f"Unhandled exception: {exception}", Ansi.LRED)
return await render_template("500.html", exception=exception), 500
# run
if __name__ == "__main__":
app.run(
debug=settings.QUART_DEBUG,
host=settings.QUART_HOST,
port=settings.QUART_PORT,
)