diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f58c058 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.11-slim + +WORKDIR /app + +COPY main.py ./main.py +COPY requirements.txt ./requirements.txt + +RUN pip install --no-cache-dir -r requirements.txt + +EXPOSE 8000 + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/main.py b/main.py new file mode 100644 index 0000000..6ed1821 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +from fastapi import FastAPI, HTTPException +import requests +import os +from fastapi.middleware.cors import CORSMiddleware + +app = FastAPI() + +origins = [ + "*", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +ACCESS_TOKEN = os.getenv("ACCESS_TOKEN") + +EXTERNAL_API_BASE_URL = os.getenv("EXTERNAL_API_BASE_URL") +ALLOWED_ACCOUNTS = str(os.getenv("ALLOWED_ACCOUNTS")).split(',') +print(f"Account IDs that are allowed to be accessed: {ALLOWED_ACCOUNTS}") + + +@app.get("/api/v1/accounts/{account_id}/statuses") +async def fetch_data(account_id): + if not account_id.isalnum(): + raise HTTPException(status_code=401, detail="Account ID is not alphanumeric") + if account_id not in ALLOWED_ACCOUNTS: + raise HTTPException(status_code=401, detail="You can only use this proxy to access configured accounts") + if not ACCESS_TOKEN: + raise HTTPException(status_code=500, detail="Access token not configured") + + headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"} + try: + response = requests.get(f"{EXTERNAL_API_BASE_URL}/api/v1/accounts/{account_id}/statuses", headers=headers) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + raise HTTPException(status_code=502, detail=f"Error fetching data from API: {e}") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5e7e855 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +fastapi +uvicorn +requests