|
| 1 | +from fastapi import FastAPI, Request, Form, status, Depends |
| 2 | +from fastapi.responses import HTMLResponse, JSONResponse |
| 3 | +from fastapi.templating import Jinja2Templates |
| 4 | +from fastapi.staticfiles import StaticFiles |
| 5 | +from fastapi.security import HTTPBasic, HTTPBasicCredentials |
| 6 | +import secrets |
| 7 | + |
| 8 | +from . import dbtools |
| 9 | + |
| 10 | +app = FastAPI(title="DB Endpoint Latency Tester", docs_url="/docs", redoc_url=None) |
| 11 | + |
| 12 | +security = HTTPBasic() |
| 13 | +templates = Jinja2Templates(directory="app/templates") |
| 14 | +app.mount("/static", StaticFiles(directory="app/static"), name="static") |
| 15 | + |
| 16 | +USERNAME = "admin" |
| 17 | +PASSWORD = "change_this" # Set a strong secret in prod |
| 18 | + |
| 19 | +def get_current_user(credentials: HTTPBasicCredentials = Depends(security)): |
| 20 | + correct_username = secrets.compare_digest(credentials.username, USERNAME) |
| 21 | + correct_password = secrets.compare_digest(credentials.password, PASSWORD) |
| 22 | + if not (correct_username and correct_password): |
| 23 | + raise JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"detail": "Invalid credentials"}) |
| 24 | + return credentials.username |
| 25 | + |
| 26 | +@app.get("/", response_class=HTMLResponse) |
| 27 | +def form(request: Request, user: str = Depends(get_current_user)): |
| 28 | + # No results on initial GET |
| 29 | + return templates.TemplateResponse("index.html", {"request": request, "user": user, "result": None}) |
| 30 | + |
| 31 | +@app.post("/test-latency", response_class=HTMLResponse) |
| 32 | +def test_latency( |
| 33 | + request: Request, |
| 34 | + dbtype: str = Form(...), |
| 35 | + host: str = Form(""), |
| 36 | + port: str = Form(""), |
| 37 | + username: str = Form(""), |
| 38 | + password: str = Form(""), |
| 39 | + database: str = Form(""), |
| 40 | + url: str = Form(""), |
| 41 | + interval: float = Form(1.0), |
| 42 | + period: int = Form(10), |
| 43 | + user: str = Depends(get_current_user) |
| 44 | +): |
| 45 | + # All credentials are held transiently only for test run |
| 46 | + result = dbtools.run_latency_test( |
| 47 | + dbtype=dbtype, |
| 48 | + host=host, |
| 49 | + port=port, |
| 50 | + username=username, |
| 51 | + password=password, |
| 52 | + database=database, |
| 53 | + url=url, |
| 54 | + interval=interval, |
| 55 | + period=period |
| 56 | + ) |
| 57 | + # Render index.html with results below |
| 58 | + return templates.TemplateResponse("index.html", { |
| 59 | + "request": request, |
| 60 | + "user": user, |
| 61 | + "result": result, |
| 62 | + "dbtype": dbtype, |
| 63 | + "host": host, |
| 64 | + "port": port, |
| 65 | + "username": username, |
| 66 | + "database": database, |
| 67 | + "url": url, |
| 68 | + "interval": interval, |
| 69 | + "period": period |
| 70 | + }) |
| 71 | + |
| 72 | +@app.post("/api/test-latency") |
| 73 | +def api_test_latency( |
| 74 | + dbtype: str, |
| 75 | + host: str = "", |
| 76 | + port: str = "", |
| 77 | + username: str = "", |
| 78 | + password: str = "", |
| 79 | + database: str = "", |
| 80 | + url: str = "", |
| 81 | + interval: float = 1.0, |
| 82 | + period: int = 10, |
| 83 | + credentials: HTTPBasicCredentials = Depends(security) |
| 84 | +): |
| 85 | + # For API/CLI use -- returns result JSON |
| 86 | + get_current_user(credentials) |
| 87 | + result = dbtools.run_latency_test( |
| 88 | + dbtype=dbtype, |
| 89 | + host=host, |
| 90 | + port=port, |
| 91 | + username=username, |
| 92 | + password=password, |
| 93 | + database=database, |
| 94 | + url=url, |
| 95 | + interval=interval, |
| 96 | + period=period |
| 97 | + ) |
| 98 | + return JSONResponse(result) |
0 commit comments