Update app/main.py
Browse files- app/main.py +62 -12
app/main.py
CHANGED
|
@@ -1,17 +1,67 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
from security.api_keys import verify_api_key
|
| 5 |
|
| 6 |
app = FastAPI(
|
| 7 |
title="Codex ReflexGuard Enterprise API",
|
| 8 |
-
version="
|
| 9 |
-
description="Enterprise Reflex
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Header, HTTPException
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import hashlib
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI(
|
| 6 |
title="Codex ReflexGuard Enterprise API",
|
| 7 |
+
version="1.0.0",
|
| 8 |
+
description="Enterprise Reflex Intelligence & Control API"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# -------------------------
|
| 12 |
+
# SECURITY
|
| 13 |
+
# -------------------------
|
| 14 |
+
VALID_KEYS = {
|
| 15 |
+
"codex-enterprise-demo-key",
|
| 16 |
+
"codex-internal-key"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def verify_key(x_api_key: str | None):
|
| 20 |
+
if x_api_key not in VALID_KEYS:
|
| 21 |
+
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 22 |
+
|
| 23 |
+
# -------------------------
|
| 24 |
+
# ROOT (THIS FIXES YOUR ISSUE)
|
| 25 |
+
# -------------------------
|
| 26 |
+
@app.get("/")
|
| 27 |
+
def root():
|
| 28 |
+
return {
|
| 29 |
+
"service": "Codex ReflexGuard Enterprise",
|
| 30 |
+
"status": "RUNNING",
|
| 31 |
+
"docs": "/docs",
|
| 32 |
+
"health": "/health",
|
| 33 |
+
"endpoint": "/v1/reflex/check"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# -------------------------
|
| 37 |
+
# HEALTH CHECK
|
| 38 |
+
# -------------------------
|
| 39 |
+
@app.get("/health")
|
| 40 |
+
def health():
|
| 41 |
+
return {
|
| 42 |
+
"status": "ok",
|
| 43 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# -------------------------
|
| 47 |
+
# CORE ENTERPRISE API
|
| 48 |
+
# -------------------------
|
| 49 |
+
@app.post("/v1/reflex/check")
|
| 50 |
+
def reflex_check(payload: dict, x_api_key: str = Header(None)):
|
| 51 |
+
verify_key(x_api_key)
|
| 52 |
+
|
| 53 |
+
scenario = payload.get("scenario", "")
|
| 54 |
+
source = payload.get("source", "unknown")
|
| 55 |
+
|
| 56 |
+
score = min(len(scenario) / 100.0, 1.0)
|
| 57 |
+
state = "BLOCK" if score > 0.6 else "ALLOW"
|
| 58 |
+
|
| 59 |
+
scenario_hash = hashlib.sha256(scenario.encode()).hexdigest()
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 63 |
+
"scenario_hash": scenario_hash,
|
| 64 |
+
"score": score,
|
| 65 |
+
"state": state,
|
| 66 |
+
"source": source
|
| 67 |
+
}
|