Automated Action 215640c310 Modify root endpoint to return API information
- Change root endpoint to return API information in JSON format
- Include links to documentation and available endpoints
- Keep the endpoint in the OpenAPI schema
2025-05-26 11:56:27 +00:00

57 lines
1.7 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=["*"], # Allow all origins for development
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 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)