Automated Action 07dc69217a Fix CORS policy for frontend integration
- Add specific Netlify frontend domain to allowed origins
- Replace wildcard origin with explicit list of allowed origins
- Add additional CORS configuration for better performance and security
- Expose headers for better API communication
2025-05-26 12:24:23 +00:00

59 lines
1.8 KiB
Python

import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.api import api_router
from app.core.config import settings
app = FastAPI(
title=settings.PROJECT_NAME,
version=settings.VERSION,
description="E-commerce API with FastAPI",
openapi_url="/openapi.json", # Make OpenAPI schema available at root path
docs_url="/docs",
redoc_url="/redoc",
root_path=settings.ROOT_PATH, # Support for deployments behind a proxy/subdirectory
)
# Set CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS, # Use origins from settings
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
max_age=600, # Cache preflight requests for 10 minutes
)
# Include API router
app.include_router(api_router, prefix=settings.API_V1_STR)
# Root route to return API information
@app.get("/", tags=["Root"])
async def root():
return {
"name": settings.PROJECT_NAME,
"version": settings.VERSION,
"description": "Welcome to the E-commerce API",
"docs": "/docs",
"redoc": "/redoc",
"api_prefix": settings.API_V1_STR,
"endpoints": {
"health": "/health",
"auth": f"{settings.API_V1_STR}/auth",
"users": f"{settings.API_V1_STR}/users",
"products": f"{settings.API_V1_STR}/products",
"categories": f"{settings.API_V1_STR}/categories",
"cart": f"{settings.API_V1_STR}/cart",
"orders": f"{settings.API_V1_STR}/orders"
}
}
# Health check endpoint
@app.get("/health", tags=["Health"])
async def health_check():
return {"status": "healthy"}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)