forked from Willy-JL/F95Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer-main.py
88 lines (72 loc) · 2.19 KB
/
indexer-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
#!/usr/bin/env python3
import contextlib
import logging
import os
import pathlib
import re
import fastapi
import uvicorn
from indexer import (
cache,
f95zone,
threads,
watcher,
)
logger = logging.getLogger()
@contextlib.asynccontextmanager
async def lifespan(app: fastapi.FastAPI):
path = pathlib.Path(__file__).absolute().parent
script = path / "main.py"
match = re.search(rb'version = "(\S+)"', script.read_bytes())
version = match.group(1).decode()
async with (
cache.lifespan(version),
f95zone.lifespan(version),
watcher.lifespan(),
):
yield
app = fastapi.FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None)
app.include_router(threads.router)
def main() -> None:
logger.setLevel(logging.INFO)
log_handler = logging.StreamHandler()
log_handler.setFormatter(_ColourFormatter())
logger.addHandler(log_handler)
uvicorn.run(
"indexer-main:app",
host=os.environ.get("BIND_HOST", "127.0.0.1"),
port=int(os.environ.get("BIND_HOST", 8069)),
workers=1,
log_config=None,
log_level=logging.INFO,
access_log=False,
env_file="indexer.env",
)
# https://github.com/Rapptz/discord.py/blob/master/discord/utils.py
class _ColourFormatter(logging.Formatter):
LEVEL_COLOURS = [
(logging.DEBUG, "\x1b[30;1m"),
(logging.INFO, "\x1b[34;1m"),
(logging.WARNING, "\x1b[33;1m"),
(logging.ERROR, "\x1b[31m"),
(logging.CRITICAL, "\x1b[41m"),
]
FORMATS = {
level: logging.Formatter(
f"\x1b[30;1m%(asctime)s\x1b[0m {colour}%(levelname)-8s\x1b[0m \x1b[35m%(name)s\x1b[0m %(message)s",
"%Y-%m-%d %H:%M:%S",
)
for level, colour in LEVEL_COLOURS
}
def format(self, record):
formatter = self.FORMATS.get(record.levelno)
if formatter is None:
formatter = self.FORMATS[logging.DEBUG]
if record.exc_info:
text = formatter.formatException(record.exc_info)
record.exc_text = f"\x1b[31m{text}\x1b[0m"
output = formatter.format(record)
record.exc_text = None
return output
if __name__ == "__main__":
main()