| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| from contextlib import asynccontextmanager | |
| from api.routes import scan, health | |
| from api import dependencies | |
| async def lifespan(app: FastAPI): | |
| """Load models on startup, cleanup on shutdown""" | |
| await dependencies.initialize_models() | |
| yield | |
| dependencies.cleanup_models() | |
| app = FastAPI( | |
| title="XSS Detection API", | |
| description="CodeBERT-based XSS vulnerability detection for PHP and JavaScript", | |
| version="1.0.0", | |
| lifespan=lifespan | |
| ) | |
| # CORS configuration | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # In production, replace with your frontend URL | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routers | |
| app.include_router(scan.router, prefix="/api/v1", tags=["scan"]) | |
| app.include_router(health.router, prefix="/api/v1", tags=["health"]) | |
| async def root(): | |
| return { | |
| "service": "XSS Detection API", | |
| "version": "1.0.0", | |
| "status": "running", | |
| "docs": "/docs" | |
| } | |
| if __name__ == "__main__": | |
| uvicorn.run( | |
| "api.main:app", | |
| host="0.0.0.0", | |
| port=8080, | |
| reload=True, | |
| log_level="info" | |
| ) | |