Fix OpenAPI documentation in production environment
- Move OpenAPI schema to root path for easier access - Add ROOT_PATH setting to support deployments behind proxies - Add root route that redirects to documentation
This commit is contained in:
parent
f942ce333e
commit
c1c91cc80f
@ -8,6 +8,7 @@ class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = "E-Commerce API"
|
||||
VERSION: str = "0.1.0"
|
||||
API_V1_STR: str = "/api/v1"
|
||||
ROOT_PATH: str = "" # For deployments behind proxies/subpaths, can be set via env var
|
||||
|
||||
# CORS Settings
|
||||
CORS_ORIGINS: List[str] = ["*"] # Allow all origins for development
|
||||
|
9
main.py
9
main.py
@ -9,9 +9,10 @@ app = FastAPI(
|
||||
title=settings.PROJECT_NAME,
|
||||
version=settings.VERSION,
|
||||
description="E-commerce API with FastAPI",
|
||||
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
||||
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
|
||||
@ -26,6 +27,12 @@ app.add_middleware(
|
||||
# Include API router
|
||||
app.include_router(api_router, prefix=settings.API_V1_STR)
|
||||
|
||||
# Root route to redirect to documentation
|
||||
@app.get("/", include_in_schema=False)
|
||||
async def root():
|
||||
from fastapi.responses import RedirectResponse
|
||||
return RedirectResponse(url="/docs")
|
||||
|
||||
# Health check endpoint
|
||||
@app.get("/health", tags=["Health"])
|
||||
async def health_check():
|
||||
|
Loading…
x
Reference in New Issue
Block a user