forked from varkaria/guweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·82 lines (60 loc) · 1.96 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
#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
__all__ = ()
import os
import aiohttp
import orjson
import databases
from quart import Quart
from quart import render_template
from quart import jsonify # Idk why i need to import it but prettifier doesn't work without it ¯\_(ツ)_/¯
from cmyui.logging import Ansi
from cmyui.logging import log
from cmyui.mysql import AsyncSQLPool
from cmyui.version import Version
from objects import glob
import settings
app = Quart(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
version = Version(1, 4, 0)
# used to secure session data.
# we recommend using a long randomly generated ascii string.
app.secret_key = str(settings.SECRET_KEY)
# On server load
@app.before_serving
async def mysql_conn() -> None:
glob.db = databases.Database(settings.DB_DSN)
log('Connected to MySQL!', Ansi.LGREEN)
@app.before_serving
async def http_conn() -> None:
glob.http = aiohttp.ClientSession(json_serialize=lambda x: orjson.dumps(x).decode())
log('Got our Client Session!', Ansi.LGREEN)
@app.after_serving
async def shutdown() -> None:
await glob.db.close()
await glob.http.close()
# Globals which can be used in template code
@app.template_global()
def appVersion() -> str:
return repr(version)
@app.template_global()
def appName() -> str:
return glob.config.app_name
@app.template_global()
def captchaKey() -> str:
return glob.config.hCaptcha_sitekey
@app.template_global()
def domain() -> str:
return glob.config.domain
# Register our blueprints
from blueprints.frontend import frontend
app.register_blueprint(frontend)
from blueprints.admin import admin
app.register_blueprint(admin, url_prefix='/admin')
@app.errorhandler(404)
async def page_not_found(e):
# NOTE: we set the 404 status explicitly
return (await render_template('404.html'), 404)
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.realpath(__file__)))
app.run(port=8000, debug=glob.config.debug) # blocking call