Initial commit

This commit is contained in:
moanos [he/him] 2024-12-11 09:26:29 +01:00
parent 9ae3b2dae3
commit 526e8b9f1a
3 changed files with 57 additions and 0 deletions

12
Dockerfile Normal file
View File

@ -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"]

42
main.py Normal file
View File

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

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
fastapi
uvicorn
requests