""" Murshid Backend — FastAPI entrypoint. Architecture: API Layer → app/api/routes/ Service Layer→ app/services/ ML Layer → app/ml/ Repository → app/repositories/ Database → app/db/ (SQLAlchemy + Alembic, MySQL) """ from __future__ import annotations from contextlib import asynccontextmanager from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from app.api.routes import db_viewer, health, queries, rules, stats from app.ml.pipeline import load_models, unload_models _FRONTEND_DIR = Path(__file__).resolve().parent.parent.parent / "murshid_frontend" @asynccontextmanager async def lifespan(app: FastAPI): load_models() yield unload_models() app = FastAPI( title="Murshid API", description=( "MITRE ATT&CK-Aligned Techniques Mapping for SOC Analysts. " "Transforms Wazuh IDS rules into actionable threat intelligence." ), version="1.0.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(health.router) app.include_router(stats.router) app.include_router(db_viewer.router) app.include_router(rules.router) app.include_router(queries.router) if _FRONTEND_DIR.is_dir(): app.mount("/", StaticFiles(directory=str(_FRONTEND_DIR), html=True), name="frontend")