Update the runtime
This commit is contained in:
parent
1d121fb83c
commit
7b09b75836
2 changed files with 29 additions and 9 deletions
16
README.md
16
README.md
|
@ -18,16 +18,24 @@ services:
|
||||||
url_previews:
|
url_previews:
|
||||||
environment:
|
environment:
|
||||||
- "PREVIEW_HOMESERVER=https://matrix.nexy7574.co.uk"
|
- "PREVIEW_HOMESERVER=https://matrix.nexy7574.co.uk"
|
||||||
|
- "FORWARDED_ALLOW_IPS=*"
|
||||||
ports:
|
ports:
|
||||||
- "2226:2226"
|
- "2226:2226"
|
||||||
restart: "unless-stopped"
|
restart: "unless-stopped"
|
||||||
container_name: "dendrite-url-previews"
|
container_name: "dendrite-url-previews"
|
||||||
volumes:
|
volumes:
|
||||||
- ./data/:/app/data/
|
- url_previews:/app/data/
|
||||||
build: . # there's no pre-built image
|
build: . # there's no pre-built image
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
url_previews:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
## 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
|
| Environment Variable | Description | Example | Default |
|
||||||
environments.
|
|-----------------------|----------------------------------------------------------------------------------------------------|---------------------------------|-----------------------------------|
|
||||||
|
| `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` |
|
||||||
|
|
22
server.py
22
server.py
|
@ -19,7 +19,6 @@ from rich.logging import RichHandler
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.asynccontextmanager
|
@contextlib.asynccontextmanager
|
||||||
async def startup(_):
|
async def startup(_):
|
||||||
if not CACHE_DIR.exists():
|
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)
|
file.seek(0)
|
||||||
|
|
||||||
|
# noinspection PyTypeChecker
|
||||||
response = httpx.post(
|
response = httpx.post(
|
||||||
"%s/_matrix/media/r0/upload" % domain,
|
"%s/_matrix/media/r0/upload" % domain,
|
||||||
headers={
|
headers={
|
||||||
|
@ -249,6 +249,7 @@ def preview_url(
|
||||||
if tag_name in og_tags:
|
if tag_name in og_tags:
|
||||||
_url = og_tags[tag_name]
|
_url = og_tags[tag_name]
|
||||||
try:
|
try:
|
||||||
|
# noinspection PyArgumentList
|
||||||
with httpx.stream(
|
with httpx.stream(
|
||||||
url=_url,
|
url=_url,
|
||||||
method="GET",
|
method="GET",
|
||||||
|
@ -285,8 +286,6 @@ def preview_url(
|
||||||
int(response_media.headers["content-length"])
|
int(response_media.headers["content-length"])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# og_tags.pop(tag_name, None)
|
|
||||||
# continue
|
|
||||||
_file.seek(0)
|
_file.seek(0)
|
||||||
upload_response = upload_media(
|
upload_response = upload_media(
|
||||||
domain,
|
domain,
|
||||||
|
@ -318,9 +317,22 @@ def preview_url(
|
||||||
"INSERT INTO cache (uuid, url, ts, metadata) VALUES (?, ?, ?, ?)",
|
"INSERT INTO cache (uuid, url, ts, metadata) VALUES (?, ?, ?, ?)",
|
||||||
(str(uuid.uuid4()), url, round(time.time()), json.dumps(og_tags))
|
(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__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
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
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue