Update the runtime

This commit is contained in:
Nexus 2024-02-16 21:10:03 +00:00
parent 1d121fb83c
commit 7b09b75836
Signed by: nex
GPG key ID: 0FA334385D0B689F
2 changed files with 29 additions and 9 deletions

View file

@ -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` |

View file

@ -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
)