Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
|
@@ -20,6 +20,7 @@ from fastapi.security.api_key import APIKeyHeader, APIKey
|
|
| 20 |
from fastapi.staticfiles import StaticFiles
|
| 21 |
from fastapi.openapi.docs import get_swagger_ui_html
|
| 22 |
from fastapi.responses import HTMLResponse
|
|
|
|
| 23 |
|
| 24 |
# Load environment variables
|
| 25 |
load_dotenv()
|
|
@@ -321,15 +322,6 @@ app.add_middleware(
|
|
| 321 |
allow_headers=["*"],
|
| 322 |
)
|
| 323 |
|
| 324 |
-
# Add security scheme
|
| 325 |
-
app.add_middleware(
|
| 326 |
-
CORSMiddleware,
|
| 327 |
-
allow_origins=["*"],
|
| 328 |
-
allow_credentials=True,
|
| 329 |
-
allow_methods=["*"],
|
| 330 |
-
allow_headers=["*"],
|
| 331 |
-
)
|
| 332 |
-
|
| 333 |
# Add security scheme
|
| 334 |
app.add_security_requirement({"ApiKeyAuth": []})
|
| 335 |
app.openapi_schema = None # Reset OpenAPI schema
|
|
@@ -554,4 +546,36 @@ async def custom_swagger_ui_html():
|
|
| 554 |
}
|
| 555 |
</style>
|
| 556 |
"""
|
| 557 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from fastapi.staticfiles import StaticFiles
|
| 21 |
from fastapi.openapi.docs import get_swagger_ui_html
|
| 22 |
from fastapi.responses import HTMLResponse
|
| 23 |
+
from fastapi.openapi.utils import get_openapi
|
| 24 |
|
| 25 |
# Load environment variables
|
| 26 |
load_dotenv()
|
|
|
|
| 322 |
allow_headers=["*"],
|
| 323 |
)
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
# Add security scheme
|
| 326 |
app.add_security_requirement({"ApiKeyAuth": []})
|
| 327 |
app.openapi_schema = None # Reset OpenAPI schema
|
|
|
|
| 546 |
}
|
| 547 |
</style>
|
| 548 |
"""
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
def custom_openapi():
|
| 552 |
+
if app.openapi_schema:
|
| 553 |
+
return app.openapi_schema
|
| 554 |
+
|
| 555 |
+
openapi_schema = get_openapi(
|
| 556 |
+
title=app.title,
|
| 557 |
+
version=app.version,
|
| 558 |
+
description=app.description,
|
| 559 |
+
routes=app.routes,
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
+
# Add security scheme to components
|
| 563 |
+
openapi_schema["components"] = {
|
| 564 |
+
"securitySchemes": {
|
| 565 |
+
"ApiKeyAuth": {
|
| 566 |
+
"type": "apiKey",
|
| 567 |
+
"in": "header",
|
| 568 |
+
"name": "X-API-Key",
|
| 569 |
+
"description": "API key required for authentication"
|
| 570 |
+
}
|
| 571 |
+
}
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
# Add global security requirement
|
| 575 |
+
openapi_schema["security"] = [{"ApiKeyAuth": []}]
|
| 576 |
+
|
| 577 |
+
app.openapi_schema = openapi_schema
|
| 578 |
+
return app.openapi_schema
|
| 579 |
+
|
| 580 |
+
# Set the custom OpenAPI schema
|
| 581 |
+
app.openapi = custom_openapi
|