|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from typing import Dict, List, Optional |
| 5 | +import base64 |
| 6 | +from fastapi import FastAPI, Request, Response, HTTPException |
| 7 | +from fastapi.staticfiles import StaticFiles |
| 8 | +import os |
| 9 | +from pydantic import BaseModel |
| 10 | + |
| 11 | +app = FastAPI(title="HTTP Intercepter Backend") |
| 12 | + |
| 13 | + |
| 14 | +class RequestSummary(BaseModel): |
| 15 | + id: int |
| 16 | + method: str |
| 17 | + path: str |
| 18 | + ts: float |
| 19 | + ip: str |
| 20 | + content_length: int |
| 21 | + |
| 22 | + |
| 23 | +class StoredRequest(BaseModel): |
| 24 | + id: int |
| 25 | + method: str |
| 26 | + path: str |
| 27 | + ts: float |
| 28 | + ip: str |
| 29 | + headers: Dict[str, str] |
| 30 | + query: Dict[str, str] |
| 31 | + body_text: Optional[str] = None |
| 32 | + body_bytes_b64: Optional[str] = None |
| 33 | + body_length: int = 0 |
| 34 | + |
| 35 | + |
| 36 | +_requests: List[StoredRequest] = [] |
| 37 | +_next_id = 1 |
| 38 | + |
| 39 | + |
| 40 | +@app.api_route("/inbound", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]) |
| 41 | +async def inbound(request: Request): |
| 42 | + global _next_id |
| 43 | + body = await request.body() |
| 44 | + try: |
| 45 | + body_text = body.decode("utf-8") if body else None |
| 46 | + except UnicodeDecodeError: |
| 47 | + body_text = None |
| 48 | + item = StoredRequest( |
| 49 | + id=_next_id, |
| 50 | + method=request.method, |
| 51 | + path=request.url.path, |
| 52 | + ts=datetime.utcnow().timestamp(), |
| 53 | + ip=request.client.host if request.client else "", |
| 54 | + headers={k: v for k, v in request.headers.items()}, |
| 55 | + query={k: v for k, v in request.query_params.items()}, |
| 56 | + body_text=body_text, |
| 57 | + body_bytes_b64=(base64.b64encode(body).decode("ascii") if body and body_text is None else None), |
| 58 | + body_length=len(body) if body else 0, |
| 59 | + ) |
| 60 | + _requests.append(item) |
| 61 | + _next_id += 1 |
| 62 | + return Response(content="OK", media_type="text/plain") |
| 63 | + |
| 64 | + |
| 65 | +@app.get("/api/requests", response_model=List[RequestSummary]) |
| 66 | +async def list_requests(): |
| 67 | + return [ |
| 68 | + RequestSummary( |
| 69 | + id=r.id, |
| 70 | + method=r.method, |
| 71 | + path=r.path, |
| 72 | + ts=r.ts, |
| 73 | + ip=r.ip, |
| 74 | + content_length=r.body_length, |
| 75 | + ) |
| 76 | + for r in reversed(_requests) |
| 77 | + ] |
| 78 | + |
| 79 | + |
| 80 | +@app.get("/api/requests/{req_id}", response_model=StoredRequest) |
| 81 | +async def get_request(req_id: int): |
| 82 | + for r in _requests: |
| 83 | + if r.id == req_id: |
| 84 | + return r |
| 85 | + raise HTTPException(status_code=404, detail="Request not found") |
| 86 | + |
| 87 | + |
| 88 | +@app.delete("/api/requests/{req_id}") |
| 89 | +async def delete_request(req_id: int): |
| 90 | + global _requests |
| 91 | + before = len(_requests) |
| 92 | + _requests = [r for r in _requests if r.id != req_id] |
| 93 | + if len(_requests) == before: |
| 94 | + raise HTTPException(status_code=404, detail="Request not found") |
| 95 | + return {"ok": True} |
| 96 | + |
| 97 | + |
| 98 | +@app.delete("/api/requests") |
| 99 | +async def delete_all_requests(): |
| 100 | + _requests.clear() |
| 101 | + return {"ok": True} |
| 102 | + |
| 103 | + |
| 104 | +@app.get("/healthz") |
| 105 | +async def healthz(): |
| 106 | + return {"ok": True} |
| 107 | + |
| 108 | +@app.get("/") |
| 109 | +async def index(): |
| 110 | + return {"name": "http-intercepter", "status": "running"} |
| 111 | + |
| 112 | +# Optionally serve built frontend if present (Docker production) |
| 113 | +_dist_dir = os.getenv("FRONTEND_DIST_DIR", "frontend-dist") |
| 114 | +if os.path.isdir(_dist_dir): |
| 115 | + app.mount("/", StaticFiles(directory=_dist_dir, html=True), name="static") |
0 commit comments