|
@@ -1,5 +1,3 @@
|
|
|
-import logging
|
|
|
-import sys
|
|
|
from contextlib import asynccontextmanager
|
|
|
from json import JSONDecodeError
|
|
|
from pathlib import Path
|
|
@@ -14,6 +12,7 @@ from selectolax.parser import HTMLParser, Node
|
|
|
|
|
|
@asynccontextmanager
|
|
|
async def lifespan(app: FastAPI):
|
|
|
+ """Establishing an aiohttp ClientSession for the duration of the app's lifecycle."""
|
|
|
global session
|
|
|
session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(10))
|
|
|
yield
|
|
@@ -28,7 +27,7 @@ session: aiohttp.ClientSession | None = None
|
|
|
|
|
|
|
|
|
def remove_classes(node: Node) -> Node:
|
|
|
- """Remove all classes from all nodes recursively."""
|
|
|
+ """Recursively remove all classes from all nodes."""
|
|
|
if "class" in node.attributes:
|
|
|
del node.attrs["class"]
|
|
|
for child in node.iter():
|
|
@@ -38,7 +37,7 @@ def remove_classes(node: Node) -> Node:
|
|
|
|
|
|
@app.get("/{path:path}", response_class=HTMLResponse)
|
|
|
async def catch_all(response: Request):
|
|
|
- """Check all routes on Urban Dictionary and redirect if needed."""
|
|
|
+ """Handle all routes on Urban Dictionary and perform redirection if necessary."""
|
|
|
path_without_host = (
|
|
|
f"{response.url.path}{f'?{response.url.query}' if response.url.query else ''}"
|
|
|
)
|
|
@@ -91,8 +90,12 @@ async def catch_all(response: Request):
|
|
|
)
|
|
|
|
|
|
|
|
|
-if __name__ == "__main__":
|
|
|
+def main():
|
|
|
+ """Run the app in production mode. It is intended to be executed within a container."""
|
|
|
import uvicorn
|
|
|
|
|
|
- logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8080, access_log=False)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|