From 7b09b75836b4379da4084fa9aeee68fe183f4fcd Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Fri, 16 Feb 2024 21:10:03 +0000 Subject: [PATCH] Update the runtime --- README.md | 16 ++++++++++++---- server.py | 22 +++++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9abd54e..ed87233 100644 --- a/README.md +++ b/README.md @@ -18,16 +18,24 @@ services: url_previews: environment: - "PREVIEW_HOMESERVER=https://matrix.nexy7574.co.uk" + - "FORWARDED_ALLOW_IPS=*" ports: - "2226:2226" restart: "unless-stopped" container_name: "dendrite-url-previews" volumes: - - ./data/:/app/data/ + - url_previews:/app/data/ build: . # there's no pre-built image + +volumes: + url_previews: ``` ## Configuration -The only configuration option is `PREVIEW_HOMESERVER`, which is the homeserver to use for the previews. -If not specified, it defaults to the host name of the request URL, which may not work in some reverse-proxy -environments. + +| Environment Variable | Description | Example | Default | +|-----------------------|----------------------------------------------------------------------------------------------------|---------------------------------|-----------------------------------| +| `PREVIEW_HOMESERVER` | The homeserver to use for the previews. | `https://matrix.nexy7574.co.uk` | The host name of the request URL. | +| `PREVIEW_HOST` | The host IP/Name to listen to. | `192.168.0.2` | `0.0.0.0` | +| `PREVIEW_PORT` | The port to listen to. | `8080` | `2226` | +| `FORWARDED_ALLOW_IPS` | The list of reverse proxy IPs to trust. See [Uvicorn docs](https://www.uvicorn.org/settings/#http) | `*` | `127.0.0.1` | diff --git a/server.py b/server.py index 95c79c6..f7f5e7d 100644 --- a/server.py +++ b/server.py @@ -19,7 +19,6 @@ from rich.logging import RichHandler from fastapi.middleware.cors import CORSMiddleware - @contextlib.asynccontextmanager async def startup(_): if not CACHE_DIR.exists(): @@ -158,6 +157,7 @@ def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str ) file.seek(0) + # noinspection PyTypeChecker response = httpx.post( "%s/_matrix/media/r0/upload" % domain, headers={ @@ -249,6 +249,7 @@ def preview_url( if tag_name in og_tags: _url = og_tags[tag_name] try: + # noinspection PyArgumentList with httpx.stream( url=_url, method="GET", @@ -285,8 +286,6 @@ def preview_url( int(response_media.headers["content-length"]) ) ) - # og_tags.pop(tag_name, None) - # continue _file.seek(0) upload_response = upload_media( domain, @@ -318,9 +317,22 @@ def preview_url( "INSERT INTO cache (uuid, url, ts, metadata) VALUES (?, ?, ?, ?)", (str(uuid.uuid4()), url, round(time.time()), json.dumps(og_tags)) ) - return og_tags + return JSONResponse( + og_tags, + 200, + headers={ + "Cache-Control": "public, max-age=86400" + } + ) if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="0.0.0.0", port=2226) + + uvicorn.run( + app, + host=os.getenv("PREVIEW_HOST", "0.0.0.0"), + port=int(os.getenv("PREVIEW_PORT", 2226)), + # If you want to enable reverse-proxy support, you must set the $FORWARDED_ALLOW_IPS environment variable. + # See: https://www.uvicorn.org/settings/#http + )