allow all origins for cors
This commit is contained in:
parent
1461373ec3
commit
8db0013f26
@ -1,3 +1,4 @@
|
||||
|
||||
from pydantic import AnyHttpUrl, validator
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
@ -8,7 +9,7 @@ class Settings(BaseSettings):
|
||||
VERSION: str = "0.1.0"
|
||||
|
||||
# CORS Configuration
|
||||
BACKEND_CORS_ORIGINS: list[str | AnyHttpUrl] = ['*']
|
||||
BACKEND_CORS_ORIGINS: list[str | AnyHttpUrl] = ["*"]
|
||||
|
||||
@validator("BACKEND_CORS_ORIGINS", pre=True)
|
||||
def assemble_cors_origins(self, v: str | list[str]) -> list[str] | str:
|
||||
|
26
main.py
26
main.py
@ -1,4 +1,6 @@
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
@ -19,13 +21,21 @@ async def lifespan(app: FastAPI):
|
||||
Startup and shutdown events run on application startup and shutdown.
|
||||
"""
|
||||
# Setup logging on startup
|
||||
try:
|
||||
setup_logging()
|
||||
logger.info(f"Starting up {settings.PROJECT_NAME}")
|
||||
|
||||
# Verify imports and application setup
|
||||
logger.info("Application initialized successfully")
|
||||
|
||||
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(
|
||||
@ -33,6 +43,8 @@ app = FastAPI(
|
||||
openapi_url="/openapi.json",
|
||||
version=settings.VERSION,
|
||||
lifespan=lifespan,
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
)
|
||||
|
||||
|
||||
@ -58,12 +70,12 @@ async def add_process_time_header(request: Request, call_next):
|
||||
|
||||
|
||||
# Set all CORS enabled origins
|
||||
if settings.BACKEND_CORS_ORIGINS:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
allow_origins=["*"], # Allow all origins
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # Allow all methods
|
||||
allow_headers=["*"], # Allow all headers
|
||||
)
|
||||
|
||||
app.include_router(api_router, prefix=settings.API_V1_STR)
|
||||
@ -79,4 +91,10 @@ async def health_check():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user