Rudraaaa76's picture
updated
c039a0e
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from dotenv import load_dotenv
import os
import logging
import time
load_dotenv()
from routers import analyze, stego
# Setup structured logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
app = FastAPI(
title="SENTINEL API",
description="Smart Explainable Network Threat Intelligence & Neutralization Engine Layer",
version="1.0.0"
)
# Global Exception Handler
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled exception: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={"message": "An internal server error occurred. Please try again later."},
)
# Middleware for request timing and logging
@app.middleware("http")
async def log_requests(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
logger.info(f"Path: {request.url.path} - Method: {request.method} - Status: {response.status_code} - Time: {process_time:.4f}s")
return response
# Strict CORS for production (Update origins as needed for frontend)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(analyze.router, prefix="/api", tags=["Analysis"])
app.include_router(stego.router, prefix="/api/stego", tags=["StegoScan"])
@app.get("/")
def root():
return {
"status": "SENTINEL operational",
"version": "1.0.0",
"endpoints": {
"analyze": "POST /api/analyze",
"stego_scan": "POST /api/stego/scan",
"stego_verify": "POST /api/stego/verify",
"demo_poisoned": "GET /api/stego/demo-text",
"health": "GET /api/health"
}
}
@app.get("/api/health")
def health():
return {"status": "ok", "groq": bool(os.getenv("GROQ_API_KEY")),
"hf": bool(os.getenv("HF_TOKEN"))}