-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
app.py
115 lines (81 loc) · 3.04 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
__version__ = "1.1.2"
import html
import os
from dotenv import load_dotenv
from motor.motor_asyncio import AsyncIOMotorClient
from sanic import Sanic, response
from sanic.exceptions import NotFound
from jinja2 import Environment, FileSystemLoader
from core.models import LogEntry
load_dotenv()
if "URL_PREFIX" in os.environ:
print("Using the legacy config var `URL_PREFIX`, rename it to `LOG_URL_PREFIX`")
prefix = os.environ["URL_PREFIX"]
else:
prefix = os.getenv("LOG_URL_PREFIX", "/logs")
if prefix == "NONE":
prefix = ""
MONGO_URI = os.getenv("MONGO_URI") or os.getenv("CONNECTION_URI")
if not MONGO_URI:
print("No CONNECTION_URI config var found. "
"Please enter your MongoDB connection URI in the configuration or .env file.")
exit(1)
app = Sanic(__name__)
app.static("/static", "./static")
jinja_env = Environment(loader=FileSystemLoader("templates"))
def render_template(name, *args, **kwargs):
template = jinja_env.get_template(name + ".html")
return response.html(template.render(*args, **kwargs))
app.ctx.render_template = render_template
def strtobool(val):
"""
Copied from distutils.strtobool.
Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return 1
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return 0
else:
raise ValueError("invalid truth value %r" % (val,))
@app.listener("before_server_start")
async def init(app, loop):
app.ctx.db = AsyncIOMotorClient(MONGO_URI).modmail_bot
use_attachment_proxy = strtobool(os.getenv("USE_ATTACHMENT_PROXY", "no"))
if use_attachment_proxy:
app.ctx.attachment_proxy_url = os.getenv("ATTACHMENT_PROXY_URL", "https://cdn.discordapp.xyz")
app.ctx.attachment_proxy_url = html.escape(app.ctx.attachment_proxy_url).rstrip("/")
else:
app.ctx.attachment_proxy_url = None
@app.exception(NotFound)
async def not_found(request, exc):
return render_template("not_found")
@app.get("/")
async def index(request):
return render_template("index")
@app.get(prefix + "/raw/<key>")
async def get_raw_logs_file(request, key):
"""Returns the plain text rendered log entry"""
document = await app.ctx.db.logs.find_one({"key": key})
if document is None:
raise NotFound
log_entry = LogEntry(app, document)
return log_entry.render_plain_text()
@app.get(prefix + "/<key>")
async def get_logs_file(request, key):
"""Returns the html rendered log entry"""
document = await app.ctx.db.logs.find_one({"key": key})
if document is None:
raise NotFound
log_entry = LogEntry(app, document)
return log_entry.render_html()
if __name__ == "__main__":
app.run(
host=os.getenv("HOST", "0.0.0.0"),
port=os.getenv("PORT", 8000),
debug=bool(os.getenv("DEBUG", False)),
)