allow all origins for cors

This commit is contained in:
Automated Action 2025-05-31 13:41:11 +00:00
parent 1461373ec3
commit 8db0013f26
2 changed files with 35 additions and 16 deletions

View File

@ -1,3 +1,4 @@
from pydantic import AnyHttpUrl, validator from pydantic import AnyHttpUrl, validator
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
@ -8,7 +9,7 @@ class Settings(BaseSettings):
VERSION: str = "0.1.0" VERSION: str = "0.1.0"
# CORS Configuration # CORS Configuration
BACKEND_CORS_ORIGINS: list[str | AnyHttpUrl] = ['*'] BACKEND_CORS_ORIGINS: list[str | AnyHttpUrl] = ["*"]
@validator("BACKEND_CORS_ORIGINS", pre=True) @validator("BACKEND_CORS_ORIGINS", pre=True)
def assemble_cors_origins(self, v: str | list[str]) -> list[str] | str: def assemble_cors_origins(self, v: str | list[str]) -> list[str] | str:

48
main.py
View File

@ -1,4 +1,6 @@
import sys
import time import time
import traceback
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import uvicorn import uvicorn
@ -19,13 +21,21 @@ async def lifespan(app: FastAPI):
Startup and shutdown events run on application startup and shutdown. Startup and shutdown events run on application startup and shutdown.
""" """
# Setup logging on startup # Setup logging on startup
setup_logging() try:
logger.info(f"Starting up {settings.PROJECT_NAME}") setup_logging()
logger.info(f"Starting up {settings.PROJECT_NAME}")
yield # Run the application
# Verify imports and application setup
# Clean up resources on shutdown logger.info("Application initialized successfully")
logger.info(f"Shutting down {settings.PROJECT_NAME}")
yield # Run the application
# Clean up resources on shutdown
logger.info(f"Shutting down {settings.PROJECT_NAME}")
except Exception as e:
logger.error(f"Error during application startup: {e}")
traceback.print_exc()
sys.exit(1)
app = FastAPI( app = FastAPI(
@ -33,6 +43,8 @@ app = FastAPI(
openapi_url="/openapi.json", openapi_url="/openapi.json",
version=settings.VERSION, version=settings.VERSION,
lifespan=lifespan, lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
) )
@ -58,13 +70,13 @@ async def add_process_time_header(request: Request, call_next):
# Set all CORS enabled origins # Set all CORS enabled origins
if settings.BACKEND_CORS_ORIGINS: app.add_middleware(
app.add_middleware( CORSMiddleware,
CORSMiddleware, allow_origins=["*"], # Allow all origins
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], allow_credentials=True,
allow_methods=["*"], allow_methods=["*"], # Allow all methods
allow_headers=["*"], allow_headers=["*"], # Allow all headers
) )
app.include_router(api_router, prefix=settings.API_V1_STR) app.include_router(api_router, prefix=settings.API_V1_STR)
@ -79,4 +91,10 @@ async def health_check():
if __name__ == "__main__": if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) try:
logger.info("Starting server with uvicorn...")
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
except Exception as e:
logger.error(f"Failed to start server: {e}")
traceback.print_exc()
sys.exit(1)