42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
from fastapi import FastAPI, Request, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import httpx
|
|
import os
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
PORTAINER_URL = os.getenv("PORTAINER_URL", "http://192.168.0.62:9000")
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "ok"}
|
|
|
|
@app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
|
async def proxy(path: str, request: Request):
|
|
token = request.headers.get("X-API-Key")
|
|
if not token:
|
|
raise HTTPException(status_code=401, detail="Missing API key")
|
|
|
|
url = f"{PORTAINER_URL}/api/{path}"
|
|
if request.url.query:
|
|
url += f"?{request.url.query}"
|
|
|
|
body = await request.body()
|
|
|
|
async with httpx.AsyncClient(timeout=30) as client:
|
|
response = await client.request(
|
|
method=request.method,
|
|
url=url,
|
|
headers={"X-API-Key": token, "Content-Type": "application/json"},
|
|
content=body,
|
|
)
|
|
|
|
return response.json()
|