File size: 9,941 Bytes
d51edaf 0a04351 18daa0e 7de41e9 db678e6 18daa0e 7de41e9 db678e6 69da73e edb1cb0 18daa0e 1535504 e97cc7a 77cb7c7 18daa0e 69da73e 2ff4f67 18daa0e 7de41e9 18daa0e aa5c5bf 2b920f4 aa5c5bf 01b80c6 aa5c5bf 01b80c6 2b920f4 01b80c6 2b920f4 aa5c5bf 2b920f4 aa5c5bf 18daa0e 7de41e9 edb1cb0 18daa0e 1535504 18daa0e 7de41e9 18daa0e 7de41e9 18daa0e 7de41e9 18daa0e 69da73e 7de41e9 69da73e 7de41e9 69da73e 7de41e9 69da73e 7de41e9 69da73e 7de41e9 1535504 69da73e 7de41e9 69da73e 7de41e9 69da73e 2ff4f67 7de41e9 18daa0e 7de41e9 18daa0e edb1cb0 18daa0e 7de41e9 18daa0e 7de41e9 18daa0e 7de41e9 18daa0e b94dde4 18daa0e | 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | # Copyright (c) 2026 K.N.Narin (github.com/narin-kura). All rights reserved.
# Non-commercial use only. Commercial use requires written permission: github.com/narin-kura
# See LICENSE for full terms.
"""ArchLens FastAPI web server."""
from __future__ import annotations
import logging
import os
import tempfile
from pathlib import Path
from typing import Optional
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
from fastapi import FastAPI, File, Form, UploadFile, HTTPException, Request
from fastapi.concurrency import run_in_threadpool
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from archlens.parsers.registry import detect_parser
from archlens.analyzers.security import SecurityAnalyzer
from archlens.analyzers.cost import CostAnalyzer
from archlens.analyzers.ai import AIAnalyzer
from archlens.analyzers.kubernetes_security import KubernetesSecurityAnalyzer
from archlens.analyzers.cicd_security import CicdSecurityAnalyzer
from archlens.models.findings import AnalysisReport
from archlens.models.architecture import ArchitectureModel, Component, Connection, ComponentType
from archlens.recommender import recommend as build_recommendation
app = FastAPI(title="ArchLens", description="Architecture security & cost analyzer")
_STATIC = Path(__file__).parent / "static"
app.mount("/static", StaticFiles(directory=str(_STATIC)), name="static")
MAX_FILE_BYTES = 5 * 1024 * 1024 # 5 MB
@app.get("/", response_class=HTMLResponse)
def index():
return (_STATIC / "index.html").read_text(encoding="utf-8")
@app.get("/health")
async def health():
"""Check app liveness and connectivity to each LLM provider."""
providers: dict[str, dict] = {}
gemini_key = os.getenv("GEMINI_API_KEY")
if not gemini_key:
providers["gemini"] = {"status": "missing_key"}
else:
try:
await run_in_threadpool(_ping_gemini, gemini_key)
providers["gemini"] = {"status": "ok"}
except Exception as exc:
providers["gemini"] = {"status": "error", "detail": str(exc)[:200]}
anthropic_key = os.getenv("ANTHROPIC_API_KEY")
if not anthropic_key:
providers["anthropic"] = {"status": "missing_key"}
else:
try:
await run_in_threadpool(_ping_anthropic, anthropic_key)
providers["anthropic"] = {"status": "ok"}
except Exception as exc:
providers["anthropic"] = {"status": "error", "detail": str(exc)[:200]}
# Degraded = a key is set but the provider can't be reached
any_error = any(v["status"] == "error" for v in providers.values())
overall = "degraded" if any_error else "healthy"
return {"status": overall, "providers": providers}
def _ping_gemini(api_key: str) -> None:
import google.generativeai as genai
# Strip whitespace/newlines a corrupted secret may carry (Windows echo footgun)
genai.configure(api_key=api_key.strip(), transport="rest")
# list_models is the lightest authenticated call available
next(iter(genai.list_models()), None)
def _ping_anthropic(api_key: str) -> None:
import socket
import anthropic
# Strip whitespace/newlines a corrupted secret may carry (Windows echo footgun)
api_key = api_key.strip()
# Step 1: raw TCP check — separates "can't reach host" from "SDK/auth issue"
try:
socket.setdefaulttimeout(8)
socket.getaddrinfo("api.anthropic.com", 443)
except OSError as exc:
raise RuntimeError(f"DNS/TCP failed for api.anthropic.com: {exc}") from exc
# Step 2: authenticated SDK call. The SDK masks real causes (e.g. a malformed
# auth header) behind a generic "Connection error." — unwrap __cause__ so the
# /health detail is actionable.
client = anthropic.Anthropic(api_key=api_key, timeout=10.0, max_retries=0)
try:
client.models.list(limit=1)
except Exception as exc:
cause = exc.__cause__
if cause is not None:
raise RuntimeError(f"{exc} (cause: {type(cause).__name__}: {cause})") from exc
raise
@app.post("/analyze")
async def analyze(
file: Optional[UploadFile] = File(default=None),
text: Optional[str] = Form(default=None),
format_hint: Optional[str] = Form(default=None),
):
if not file and not text:
raise HTTPException(status_code=400, detail="Provide either a file or a text description.")
try:
if file and file.filename:
model = await _parse_upload(file, format_hint)
else:
if not text or not text.strip():
raise HTTPException(status_code=400, detail="Text description cannot be empty.")
# threadpool: LLM-backed parse must not block the event loop
model = await run_in_threadpool(_parse_text, text, format_hint)
findings = SecurityAnalyzer().analyze(model) + KubernetesSecurityAnalyzer().analyze(model) + CicdSecurityAnalyzer().analyze(model) + CostAnalyzer().analyze(model) + AIAnalyzer().analyze(model)
report = AnalysisReport(architecture_name=model.name, findings=findings)
return _report_to_dict(report, len(model.components))
except HTTPException:
raise
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc))
except Exception as exc:
logger.exception("Unexpected error during analysis")
raise HTTPException(
status_code=500,
detail="Analysis failed. Please check your file format and try again."
)
@app.post("/analyze-interactive")
async def analyze_interactive(request: Request):
try:
data = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON body.")
components_raw = data.get("components", [])
if not components_raw:
raise HTTPException(
status_code=400,
detail="No components selected. Please add at least one service to your stack."
)
try:
model = ArchitectureModel(
name=data.get("name", "My Stack"),
source="interactive",
)
for c in components_raw:
try:
ctype = ComponentType[c.get("type", "OTHER")]
except KeyError:
ctype = ComponentType.OTHER
model.components.append(Component(
id=c.get("id", "unknown"),
name=c.get("name", "unknown"),
type=ctype,
provider=c.get("provider", "generic"),
service=c.get("service", ""),
))
for conn in data.get("connections", []):
try:
model.connections.append(Connection(
source_id=conn["source_id"],
target_id=conn["target_id"],
label=conn.get("label", ""),
))
except Exception:
continue
findings = SecurityAnalyzer().analyze(model) + KubernetesSecurityAnalyzer().analyze(model) + CicdSecurityAnalyzer().analyze(model) + CostAnalyzer().analyze(model) + AIAnalyzer().analyze(model)
report = AnalysisReport(architecture_name=model.name, findings=findings)
return _report_to_dict(report, len(model.components))
except HTTPException:
raise
except Exception as exc:
logger.exception("Unexpected error during interactive analysis")
raise HTTPException(
status_code=500,
detail="Analysis failed. Please try again."
)
@app.post("/recommend")
async def recommend(request: Request):
try:
data = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON body.")
try:
# threadpool: the AI-summary step may make a blocking network call
return await run_in_threadpool(build_recommendation, data)
except Exception:
logger.exception("Unexpected error during recommendation")
raise HTTPException(
status_code=500,
detail="Could not generate a recommendation. Please try again."
)
async def _parse_upload(file: UploadFile, format_hint: Optional[str]) -> ArchitectureModel:
content = await file.read()
if len(content) > MAX_FILE_BYTES:
raise ValueError(
f"File is too large ({len(content) // 1024} KB). "
f"Maximum allowed size is {MAX_FILE_BYTES // 1024 // 1024} MB."
)
suffix = Path(file.filename).suffix
with tempfile.TemporaryDirectory() as tmp:
dest = Path(tmp) / file.filename
dest.write_bytes(content)
source = str(tmp) if suffix == ".tf" else str(dest)
parser = detect_parser(source, format_hint)
# threadpool: text/LLM parsers must not block the event loop
return await run_in_threadpool(parser.parse, source)
def _parse_text(text: str, format_hint: Optional[str]) -> ArchitectureModel:
parser = detect_parser(text, format_hint or "text")
return parser.parse(text)
def _report_to_dict(report: AnalysisReport, components_found: int = 0) -> dict:
return {
"architecture": report.architecture_name,
"components_found": components_found,
"summary": {
"security_count": len(report.security_findings),
"cost_count": len(report.cost_findings),
"estimated_savings": report.total_estimated_savings,
},
"findings": [
{
"type": f.type.value,
"severity": f.severity.value,
"title": f.title,
"description": f.description,
"component": f.component_name,
"recommendation": f.recommendation,
"estimated_savings": f.estimated_savings,
"references": f.references,
}
for f in report.findings
],
}
|