Better static files handling on backend, when in dev mode: fetch from vite.

This commit is contained in:
2025-12-04 02:30:02 +00:00
parent b9b1c995f9
commit a6591a1fbb
8 changed files with 65 additions and 20 deletions
+2 -3
View File
@@ -244,9 +244,8 @@ def main():
run_kwargs["port"] = port
if devmode:
if os.environ.get("PASSKEY_BUN_PARENT") != "1":
os.environ["PASSKEY_BUN_PARENT"] = "1"
frontend.run_dev()
os.environ["PASSKEY_DEVMODE"] = "1"
frontend.run_dev()
if all_ifaces and not uds:
if devmode:
+4 -4
View File
@@ -2,8 +2,8 @@ import logging
from datetime import timezone
from uuid import UUID, uuid4
from fastapi import Body, FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, JSONResponse
from fastapi import Body, FastAPI, HTTPException, Request, Response
from fastapi.responses import JSONResponse
from ..authsession import reset_expires
from ..globals import db
@@ -33,7 +33,7 @@ async def auth_exception_handler(_request, exc: authz.AuthException):
"""Handle AuthException with auth info for UI."""
return JSONResponse(
status_code=exc.status_code,
content=authz.auth_error_content(exc),
content=await authz.auth_error_content(exc),
)
@@ -45,7 +45,7 @@ async def general_exception_handler(_request, exc: Exception):
@app.get("/")
async def adminapp(request: Request, auth=AUTH_COOKIE):
return FileResponse(frontend.file("auth", "admin", "index.html"))
return Response(*await frontend.read("/auth/admin/index.html"))
# -------------------- Organizations --------------------
+3 -3
View File
@@ -62,7 +62,7 @@ async def auth_exception_handler(_request: Request, exc: authz.AuthException):
"""Handle AuthException with auth info for UI."""
return JSONResponse(
status_code=exc.status_code,
content=authz.auth_error_content(exc),
content=await authz.auth_error_content(exc),
)
@@ -179,7 +179,7 @@ async def forward_authentication(
if wants_html:
# Browser request - return full-page HTML with metadata
data_attrs = {"mode": e.mode, **e.metadata}
html = frontend.file("int", "forward", "index.html").read_bytes()
html = (await frontend.read("/int/forward/index.html"))[0]
html = htmlutil.patch_html_data_attrs(html, **data_attrs)
return Response(
html, status_code=e.status_code, media_type="text/html; charset=UTF-8"
@@ -188,7 +188,7 @@ async def forward_authentication(
# API request - return JSON with iframe srcdoc HTML
return JSONResponse(
status_code=e.status_code,
content=authz.auth_error_content(e),
content=await authz.auth_error_content(e),
)
+2 -2
View File
@@ -32,13 +32,13 @@ class AuthException(HTTPException):
self.metadata = metadata
def auth_error_content(exc: AuthException) -> dict:
async def auth_error_content(exc: AuthException) -> dict:
"""Generate JSON response content for an AuthException.
Returns a dict with detail, mode, and iframe HTML for srcdoc embedding.
"""
data_attrs = {"mode": exc.mode, **exc.metadata}
iframe_html = frontend.file("auth", "restricted", "index.html").read_bytes()
iframe_html = (await frontend.read("/auth/restricted/index.html"))[0]
iframe_html = htmlutil.patch_html_data_attrs(iframe_html, **data_attrs)
return {
"detail": exc.detail,
+5 -5
View File
@@ -3,7 +3,7 @@ import os
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from passkey.util import frontend, hostutil, passphrase
@@ -64,7 +64,7 @@ app.mount(
@app.get("/auth/restricted/")
async def restricted_view():
"""Serve the restricted/authentication UI for iframe embedding."""
return FileResponse(frontend.file("auth", "restricted", "index.html"))
return Response(*await frontend.read("/auth/restricted/index.html"))
# Navigable URLs are defined here. We support both / and /auth/ as the base path
@@ -83,8 +83,8 @@ async def frontapp(request: Request, response: Response, auth=AUTH_COOKIE):
cur_host = hostutil.normalize_host(request.headers.get("host"))
cfg_normalized = hostutil.normalize_host(cfg_host)
if cur_host and cfg_normalized and cur_host != cfg_normalized:
return FileResponse(frontend.file("int", "host", "index.html"))
return FileResponse(frontend.file("auth", "index.html"))
return Response(*await frontend.read("/int/host/index.html"))
return Response(*await frontend.read("/auth/index.html"))
@app.get("/admin", include_in_schema=False)
@@ -105,4 +105,4 @@ async def reset_link(reset: str):
"""Serve the reset app directly with an injected reset token."""
if not passphrase.is_well_formed(reset):
raise HTTPException(status_code=404)
return FileResponse(frontend.file("int", "reset", "index.html"))
return Response(*await frontend.read("/int/reset/index.html"))
+1 -1
View File
@@ -29,7 +29,7 @@ async def auth_exception_handler(_request, exc: authz.AuthException):
"""Handle AuthException with auth info for UI."""
return JSONResponse(
status_code=exc.status_code,
content=authz.auth_error_content(exc),
content=await authz.auth_error_content(exc),
)
+1 -1
View File
@@ -26,7 +26,7 @@ def websocket_error_handler(func):
await ws.send_json(
{
"status": e.status_code,
**authz.auth_error_content(e),
**(await authz.auth_error_content(e)),
}
)
except (ValueError, InvalidAuthenticationResponse) as e: