File size: 3,115 Bytes
0223188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9805c9f
 
 
 
 
 
 
 
0223188
 
 
9805c9f
0223188
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import httpx
import uvicorn
from typing import Dict, Any, Optional
import os

app = FastAPI(title="LLM7.io API Proxy")

# Configure CORS for all origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Target API URL
TARGET_API_URL = "https://api.llm7.io"

@app.get("/")
async def root():
    return {
        "status": "online",
        "message": "LLM7.io API Proxy is running. Send requests to /v1/... endpoints.",
        "usage": "This proxy forwards requests to api.llm7.io. No API key is required."
    }

@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
async def proxy(path: str, request: Request):
    """
    Proxy all requests to the LLM7.io API.
    This handles any path and HTTP method, forwarding the request to the target API.
    """
    # Construct target URL
    target_url = f"{TARGET_API_URL}/{path}"
    
    # Get request body if present
    body = None
    if request.method in ["POST", "PUT", "PATCH"]:
        try:
            body = await request.json()
        except Exception:
            try:
                body = await request.body()
            except Exception:
                body = None
    
    # Get request headers, excluding host
    headers = dict(request.headers)
    headers.pop("host", None)
    
    # Create httpx client with timeout
    async with httpx.AsyncClient(timeout=120.0) as client:
        try:
            # Forward the request to the target API
            response = await client.request(
                method=request.method,
                url=target_url,
                headers=headers,
                json=body if isinstance(body, dict) else None,
                content=body if not isinstance(body, dict) and body is not None else None,
                params=request.query_params,
                follow_redirects=True
            )
            
            # Create response with same status code and content, but filter headers
            # Remove problematic headers that could cause Content-Length issues
            response_headers = dict(response.headers)
            # Remove content-length header to let FastAPI calculate it correctly
            response_headers.pop("content-length", None)
            # Also remove transfer-encoding as it can conflict with content-length
            response_headers.pop("transfer-encoding", None)
            
            return Response(
                content=response.content,
                status_code=response.status_code,
                headers=response_headers,
            )
        except httpx.RequestError as exc:
            raise HTTPException(status_code=503, detail=f"Error forwarding request: {str(exc)}")

if __name__ == "__main__":
    # Get port from environment variable or use default
    port = int(os.environ.get("PORT", 7860))
    
    # Run the server
    uvicorn.run(app, host="0.0.0.0", port=port)