| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import os
- from fastapi import HTTPException
- from fastapi.responses import HTMLResponse, FileResponse
- from fastapi.staticfiles import StaticFiles
- from starlette.responses import Response
- from starlette.types import Scope, Receive, Send
- class NoCacheStaticFiles(StaticFiles):
- """StaticFiles que agrega headers para evitar cache"""
-
- async def get_response(self, path: str, scope: Scope) -> Response:
- """Override para agregar headers de no-cache"""
- response = await super().get_response(path, scope)
-
- # Agregar headers para evitar cache
- response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, max-age=0"
- response.headers["Pragma"] = "no-cache"
- response.headers["Expires"] = "0"
-
- return response
- async def serve_app_html():
- """Serve the main HTML file"""
- index_path = os.path.join("public","main", "index.html")
- if not os.path.exists(index_path):
- raise HTTPException(status_code=404, detail="public/index.html not found.")
-
- # Headers para evitar cache
- headers = {
- "Cache-Control": "no-cache, no-store, must-revalidate, max-age=0",
- "Pragma": "no-cache",
- "Expires": "0"
- }
- return FileResponse(index_path, headers=headers)
- async def serve_register_html():
- """Serve the register HTML file"""
- register_path = os.path.join("public", "register", "index.html")
- if not os.path.exists(register_path):
- raise HTTPException(status_code=404, detail="public/register/index.html not found.")
-
- # Headers para evitar cache
- headers = {
- "Cache-Control": "no-cache, no-store, must-revalidate, max-age=0",
- "Pragma": "no-cache",
- "Expires": "0"
- }
- return FileResponse(register_path, headers=headers)
- def mount_register_static_files(app):
- """Mount static files for the register page"""
- app.mount("/register/", NoCacheStaticFiles(directory="public/register", html=False), name="register_static")
- def mount_main_static_files(app):
- """Mount static files"""
- app.mount("/express/", NoCacheStaticFiles(directory="public/main", html=False), name="public_root_assets")
- def serve_image(image_path: str):
- """Serve images from the public/images directory"""
- image_full_path = os.path.join("public", "images", image_path)
- if not os.path.exists(image_full_path):
- raise HTTPException(status_code=404, detail=f"Image '{image_path}' not found.")
-
- # Headers para evitar cache en imágenes también
- headers = {
- "Cache-Control": "no-cache, no-store, must-revalidate, max-age=0",
- "Pragma": "no-cache",
- "Expires": "0"
- }
- return FileResponse(image_full_path, headers=headers)
|