Automated Action 2a70291924 Fix CORS configuration for frontend access
- Set allow_credentials=False when using wildcard origins
- Added explicit OPTIONS handler for preflight requests
- Simplified CORS configuration to be more permissive
- This should resolve CORS blocking from frontend applications
2025-06-20 11:49:31 +00:00

76 lines
1.8 KiB
Python

import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.db.session import engine
from app.db.base import Base
from app.routers import monitors, checks, scheduler
from app.services.scheduler import uptime_scheduler
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create database tables
Base.metadata.create_all(bind=engine)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
logger.info("Starting Uptime Monitoring API...")
uptime_scheduler.start()
yield
# Shutdown
logger.info("Shutting down Uptime Monitoring API...")
uptime_scheduler.stop()
app = FastAPI(
title="Uptime Monitoring API",
description="API for monitoring website/endpoint uptime and performance",
version="1.0.0",
openapi_url="/openapi.json",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False, # Must be False when using wildcard origins
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
expose_headers=["*"],
)
# Include routers
app.include_router(monitors.router, prefix="/api/v1")
app.include_router(checks.router, prefix="/api/v1")
app.include_router(scheduler.router, prefix="/api/v1")
@app.get("/")
async def root():
return {
"title": "Uptime Monitoring API",
"documentation": "/docs",
"health_check": "/health",
}
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "uptime-monitoring-api"}
@app.options("/{path:path}")
async def options_handler():
"""Handle preflight OPTIONS requests"""
return {"message": "OK"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)