Files
hermes-hub/app/routers/proxy.py
T

42 lines
2.0 KiB
Python

from fastapi import APIRouter, Request, HTTPException
from app.config import UNIVERSES, get_universe
from app.proxy import proxy_request
router = APIRouter(prefix="/u", tags=["Universe Proxy"])
@router.api_route("/{universe_id}/files", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
@router.api_route("/{universe_id}/files/", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
@router.api_route("/{universe_id}/files/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
async def dynamic_files_proxy(universe_id: str, request: Request, path: str = ""):
if universe_id not in UNIVERSES:
raise HTTPException(status_code=404, detail=f"Universe '{universe_id}' not found.")
universe = UNIVERSES[universe_id]
if not universe.supports_files or not universe.files_url:
raise HTTPException(status_code=404, detail=f"Universe '{universe_id}' does not have an active filebrowser.")
# Filebrowser runs with --baseurl /u/{universe_id}/files
full_subpath = f"u/{universe_id}/files/{path}".rstrip("/") if path else f"u/{universe_id}/files/"
return await proxy_request(
request=request,
backend_url=universe.files_url,
path=full_subpath,
universe_id=universe_id
)
@router.api_route("/{universe_id}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
@router.api_route("/{universe_id}/", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
@router.api_route("/{universe_id}/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
async def dynamic_universe_proxy(universe_id: str, request: Request, path: str = ""):
if universe_id not in UNIVERSES:
raise HTTPException(status_code=404, detail=f"Universe '{universe_id}' not found.")
universe = UNIVERSES[universe_id]
return await proxy_request(
request=request,
backend_url=universe.backend_url,
path=path,
universe_id=universe_id
)