import asyncio import logging from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from config.settings import settings from config.database import engine from models import Base from models.rule_template import RuleTemplate from routers.cves import router as cve_router from routers.sigma_rules import router as sigma_rule_router from routers.bulk_operations import router as bulk_router from routers.llm_operations import router as llm_router # Setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Global job tracking (TODO: Move to job service) running_jobs = {} job_cancellation_flags = {} @asynccontextmanager async def lifespan(app: FastAPI): """Application lifespan manager""" # Initialize database Base.metadata.create_all(bind=engine) # Initialize rule templates from config.database import SessionLocal db = SessionLocal() try: existing_templates = db.query(RuleTemplate).count() if existing_templates == 0: logger.info("No rule templates found. Database initialization will handle template creation.") except Exception as e: logger.error(f"Error checking rule templates: {e}") finally: db.close() # Initialize and start the job scheduler try: from job_scheduler import initialize_scheduler from job_executors import register_all_executors # Initialize scheduler scheduler = initialize_scheduler() scheduler.set_db_session_factory(SessionLocal) # Register all job executors register_all_executors(scheduler) # Start the scheduler scheduler.start() logger.info("Job scheduler initialized and started") except Exception as e: logger.error(f"Error initializing job scheduler: {e}") yield # Shutdown try: from job_scheduler import get_scheduler scheduler = get_scheduler() scheduler.stop() logger.info("Job scheduler stopped") except Exception as e: logger.error(f"Error stopping job scheduler: {e}") # FastAPI app app = FastAPI(title="CVE-SIGMA Auto Generator", lifespan=lifespan) # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=settings.CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include routers app.include_router(cve_router) app.include_router(sigma_rule_router) app.include_router(bulk_router) app.include_router(llm_router) @app.get("/api/stats") async def get_stats(): """Get application statistics""" from config.database import SessionLocal from services import CVEService, SigmaRuleService db = SessionLocal() try: cve_service = CVEService(db) sigma_service = SigmaRuleService(db) cve_stats = cve_service.get_cve_stats() rule_stats = sigma_service.get_rule_stats() return { **cve_stats, **rule_stats, "api_status": "operational" } finally: db.close() @app.get("/api/health") async def health_check(): """Health check endpoint""" return { "status": "healthy", "service": "CVE-SIGMA Auto Generator", "version": "2.0.0" } # TODO: Add remaining endpoints from original main.py: # - Bulk processing endpoints # - LLM endpoints # - Scheduler endpoints # - GitHub analysis endpoints if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)