Spaces:
Runtime error
Runtime error
chih.yikuan commited on
Commit ·
6884131
1
Parent(s): 9fdcfbc
Fix: Add explicit CORS headers and improve file serving for report-demo route
Browse files- chatkit/backend/app/main.py +26 -8
chatkit/backend/app/main.py
CHANGED
|
@@ -271,9 +271,9 @@ async def serve_report_template():
|
|
| 271 |
# Report Demo Route
|
| 272 |
# =============================================================================
|
| 273 |
|
| 274 |
-
@app.get("/report-demo")
|
| 275 |
async def serve_report_demo():
|
| 276 |
-
"""Serve the HTML report template demo."""
|
| 277 |
# Try multiple possible locations (in order of preference)
|
| 278 |
base_dir = Path(__file__).parent.parent # /home/user/app in Docker
|
| 279 |
possible_paths = [
|
|
@@ -288,11 +288,28 @@ async def serve_report_demo():
|
|
| 288 |
|
| 289 |
for template_path in possible_paths:
|
| 290 |
if template_path and template_path.exists():
|
| 291 |
-
return
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
# If not found, return helpful error message with debug info
|
| 298 |
current_dir = base_dir
|
|
@@ -308,7 +325,8 @@ async def serve_report_demo():
|
|
| 308 |
return Response(
|
| 309 |
content=f"Report template not found.\nDebug info:\n{json.dumps(debug_info, indent=2)}",
|
| 310 |
status_code=404,
|
| 311 |
-
media_type="text/plain"
|
|
|
|
| 312 |
)
|
| 313 |
|
| 314 |
|
|
|
|
| 271 |
# Report Demo Route
|
| 272 |
# =============================================================================
|
| 273 |
|
| 274 |
+
@app.get("/report-demo", response_class=Response)
|
| 275 |
async def serve_report_demo():
|
| 276 |
+
"""Serve the HTML report template demo - publicly accessible."""
|
| 277 |
# Try multiple possible locations (in order of preference)
|
| 278 |
base_dir = Path(__file__).parent.parent # /home/user/app in Docker
|
| 279 |
possible_paths = [
|
|
|
|
| 288 |
|
| 289 |
for template_path in possible_paths:
|
| 290 |
if template_path and template_path.exists():
|
| 291 |
+
# Read file content and return with explicit headers
|
| 292 |
+
try:
|
| 293 |
+
content = template_path.read_text(encoding='utf-8')
|
| 294 |
+
return Response(
|
| 295 |
+
content=content,
|
| 296 |
+
media_type="text/html; charset=utf-8",
|
| 297 |
+
headers={
|
| 298 |
+
"Cache-Control": "public, max-age=3600",
|
| 299 |
+
"X-Content-Type-Options": "nosniff",
|
| 300 |
+
"Access-Control-Allow-Origin": "*",
|
| 301 |
+
}
|
| 302 |
+
)
|
| 303 |
+
except Exception as e:
|
| 304 |
+
# If reading fails, try FileResponse as fallback
|
| 305 |
+
return FileResponse(
|
| 306 |
+
template_path,
|
| 307 |
+
media_type="text/html; charset=utf-8",
|
| 308 |
+
headers={
|
| 309 |
+
"Cache-Control": "public, max-age=3600",
|
| 310 |
+
"Access-Control-Allow-Origin": "*",
|
| 311 |
+
}
|
| 312 |
+
)
|
| 313 |
|
| 314 |
# If not found, return helpful error message with debug info
|
| 315 |
current_dir = base_dir
|
|
|
|
| 325 |
return Response(
|
| 326 |
content=f"Report template not found.\nDebug info:\n{json.dumps(debug_info, indent=2)}",
|
| 327 |
status_code=404,
|
| 328 |
+
media_type="text/plain",
|
| 329 |
+
headers={"Access-Control-Allow-Origin": "*"}
|
| 330 |
)
|
| 331 |
|
| 332 |
|