File size: 1,727 Bytes
152237b
 
a48116e
 
 
 
152237b
 
a48116e
 
152237b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a48116e
 
 
 
 
152237b
 
 
 
a48116e
 
152237b
a48116e
152237b
a48116e
152237b
a48116e
 
152237b
 
 
 
 
 
a48116e
 
152237b
a48116e
152237b
a48116e
152237b
a48116e
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
from fastapi import FastAPI, Request, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
import os, requests

app = FastAPI()

# --- Config from env ---
OLLAMA_URL = os.getenv("OLLAMA_URL")  # https://studio-ollama.decidrapp.com
CF_ID = os.getenv("CF_ACCESS_CLIENT_ID")
CF_SECRET = os.getenv("CF_ACCESS_CLIENT_SECRET")
API_KEY = os.getenv("API_KEY")  # <-- NEW: server-side API key

# --- CORS (optional; safe defaults) ---
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["GET", "POST", "OPTIONS"],
    allow_headers=["*"],
)

def require_api_key(request: Request):
    """Simple header-based auth for this proxy."""
    if not API_KEY:
        # If you forgot to set it, fail CLOSED
        raise HTTPException(status_code=500, detail="Server missing API_KEY")
    if request.headers.get("x-api-key") != API_KEY:
        raise HTTPException(status_code=401, detail="Unauthorized")

@app.get("/")
def home():
    return {"status": "Decidr proxy running"}

@app.get("/api/tags")
def tags(_: None = Depends(require_api_key)):
    r = requests.get(
        f"{OLLAMA_URL}/api/tags",
        headers={
            "CF-Access-Client-Id": CF_ID,
            "CF-Access-Client-Secret": CF_SECRET,
        },
        timeout=30
    )
    r.raise_for_status()
    return r.json()

@app.post("/api/generate")
async def generate(req: Request, _: None = Depends(require_api_key)):
    body = await req.json()
    r = requests.post(
        f"{OLLAMA_URL}/api/generate",
        json=body,
        headers={
            "CF-Access-Client-Id": CF_ID,
            "CF-Access-Client-Secret": CF_SECRET,
        },
        timeout=60
    )
    r.raise_for_status()
    return r.json()