feat/fix: implement chatbot session persistence, resolve auth redirect loops, and polish UI
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.responses import FileResponse, PlainTextResponse
|
||||
from slowapi import _rate_limit_exceeded_handler
|
||||
from slowapi.errors import RateLimitExceeded
|
||||
from app.limiter import limiter
|
||||
@@ -85,15 +86,34 @@ DIST_DIR = os.path.join(FRONTEND_DIR, "dist")
|
||||
SPA_INDEX = os.path.join(DIST_DIR, "index.html")
|
||||
|
||||
|
||||
def _mount_if_exists(route: str, path: str, name: str) -> None:
|
||||
class CachedStaticFiles(StaticFiles):
|
||||
"""StaticFiles subclass that injects Cache-Control headers."""
|
||||
|
||||
def __init__(self, *args, max_age: int = 86400, immutable: bool = False, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
parts = [f"public, max-age={max_age}"]
|
||||
if immutable:
|
||||
parts.append("immutable")
|
||||
self._cache_control = ", ".join(parts)
|
||||
|
||||
async def get_response(self, path, scope): # type: ignore[override]
|
||||
resp = await super().get_response(path, scope)
|
||||
resp.headers["Cache-Control"] = self._cache_control
|
||||
return resp
|
||||
|
||||
|
||||
def _mount_cached(route: str, path: str, name: str, **kwargs) -> None:
|
||||
if os.path.isdir(path):
|
||||
app.mount(route, StaticFiles(directory=path), name=name)
|
||||
app.mount(route, CachedStaticFiles(directory=path, **kwargs), name=name)
|
||||
log.info("Mounted static %s → %s", route, path)
|
||||
|
||||
|
||||
_mount_if_exists("/css", os.path.join(DIST_DIR, "css"), "css")
|
||||
_mount_if_exists("/assets", os.path.join(DIST_DIR, "assets"), "assets")
|
||||
_mount_if_exists("/images", os.path.join(DIST_DIR, "images"), "images")
|
||||
# Hashed filenames — cache forever
|
||||
_mount_cached("/assets", os.path.join(DIST_DIR, "assets"), "assets",
|
||||
max_age=31_536_000, immutable=True)
|
||||
# Images & CSS — cache for 1 day
|
||||
_mount_cached("/css", os.path.join(DIST_DIR, "css"), "css", max_age=86_400)
|
||||
_mount_cached("/images", os.path.join(DIST_DIR, "images"), "images", max_age=86_400)
|
||||
|
||||
# Ensure uploads directory exists
|
||||
UPLOAD_DIR = os.path.abspath(os.path.join(FRONTEND_DIR, "..", "uploads"))
|
||||
@@ -113,6 +133,26 @@ def logout():
|
||||
return {"message": "Logged out"}
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# Crawler files — must be served BEFORE the SPA catch-all
|
||||
# ─────────────────────────────────────────────
|
||||
|
||||
@app.get("/robots.txt", tags=["seo"])
|
||||
def robots_txt():
|
||||
robots_path = os.path.join(DIST_DIR, "robots.txt")
|
||||
if os.path.isfile(robots_path):
|
||||
return FileResponse(robots_path, media_type="text/plain")
|
||||
return PlainTextResponse("User-agent: *\nAllow: /\n")
|
||||
|
||||
|
||||
@app.get("/llms.txt", tags=["seo"])
|
||||
def llms_txt():
|
||||
llms_path = os.path.join(DIST_DIR, "llms.txt")
|
||||
if os.path.isfile(llms_path):
|
||||
return FileResponse(llms_path, media_type="text/plain")
|
||||
return PlainTextResponse("# BookKeepPro\n")
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# SPA catch-all — serve index.html for all non-API routes
|
||||
# React Router handles client-side navigation
|
||||
|
||||
Reference in New Issue
Block a user