File size: 1,489 Bytes
26e1c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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")