aukpad-paste-proxy/main.py

63 lines
No EOL
2.4 KiB
Python

from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
import httpx
import asyncio
import os
app = FastAPI(title="Aukpad Proxy Service", description="Proxy service for aukpad.com to linedump.com")
# Configuration from environment variables
PAD_BASEURL = os.getenv("PAD_BASEURL", "https://aukpad.com")
DUMP_BASEURL = os.getenv("DUMP_BASEURL", "https://linedump.com")
PROXY_BASEURL = os.getenv("PROXY_BASEURL", "https://bin.aukpad.com")
@app.get("/{paste_id}")
async def proxy_paste(paste_id: str):
try:
# Fetch content from pad domain
pad_url = f"{PAD_BASEURL}/{paste_id}/raw"
async with httpx.AsyncClient() as client:
# Get content from pad
pad_response = await client.get(pad_url)
pad_response.raise_for_status()
content = pad_response.text
# Post content to dump domain
dump_response = await client.post(
DUMP_BASEURL,
data=content,
headers={"Content-Type": "text/plain"}
)
dump_response.raise_for_status()
# linedump.com returns the full URL in the response body
dump_url = dump_response.text.strip()
# Redirect to the dump URL
return RedirectResponse(url=dump_url, status_code=302)
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=e.response.status_code, detail=f"Error fetching from external service: {str(e)}")
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Network error: {str(e)}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.get("/")
async def root():
return {
"service": "Aukpad Proxy Service",
"description": f"This service proxies paste content from {PAD_BASEURL} to {DUMP_BASEURL}",
"usage": f"Visit {PROXY_BASEURL}/{{paste_id}} to fetch content from {PAD_BASEURL}/{{paste_id}}/raw and redirect to the posted URL on {DUMP_BASEURL}",
"example": f"{PROXY_BASEURL}/abc123 → fetches {PAD_BASEURL}/abc123/raw → posts to {DUMP_BASEURL} → redirects to result URL",
"base_urls": {
"proxy": PROXY_BASEURL,
"source": PAD_BASEURL,
"destination": DUMP_BASEURL
}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)