mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-10-27 07:21:58 -06:00
Fixed Engine Flask Server Accessibility
This commit is contained in:
@@ -1,17 +1,86 @@
|
||||
"""WebUI service stubs for the Borealis Engine runtime.
|
||||
|
||||
The future WebUI migration will centralise static asset serving, template
|
||||
rendering, and dev-server proxying here. Stage 1 keeps the placeholder so the
|
||||
application factory can stub out registration calls.
|
||||
"""
|
||||
"""WebUI static asset handling for the Borealis Engine runtime."""
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import Flask
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from flask import Blueprint, Flask, request, send_from_directory
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
from ...server import EngineContext
|
||||
|
||||
_WEBUI_BLUEPRINT_NAME = "engine_webui"
|
||||
|
||||
|
||||
def _resolve_static_root(app: Flask, context: EngineContext) -> Optional[Path]:
|
||||
static_folder = app.static_folder
|
||||
if not static_folder:
|
||||
context.logger.error("Engine WebUI static folder is not configured.")
|
||||
return None
|
||||
|
||||
static_path = Path(static_folder).resolve()
|
||||
if not static_path.is_dir():
|
||||
context.logger.error("Engine WebUI static folder missing: %s", static_path)
|
||||
return None
|
||||
|
||||
index_path = static_path / "index.html"
|
||||
if not index_path.is_file():
|
||||
context.logger.error("Engine WebUI missing index.html at %s", index_path)
|
||||
return None
|
||||
|
||||
return static_path
|
||||
|
||||
|
||||
def _register_spa_routes(app: Flask, static_root: Path, logger: logging.Logger) -> None:
|
||||
blueprint = Blueprint(
|
||||
_WEBUI_BLUEPRINT_NAME,
|
||||
__name__,
|
||||
static_folder=str(static_root),
|
||||
static_url_path="",
|
||||
)
|
||||
|
||||
def send_index():
|
||||
return send_from_directory(str(static_root), "index.html")
|
||||
|
||||
@blueprint.route("/", defaults={"requested_path": ""})
|
||||
@blueprint.route("/<path:requested_path>")
|
||||
def spa_entry(requested_path: str) -> object:
|
||||
if requested_path:
|
||||
try:
|
||||
return send_from_directory(str(static_root), requested_path)
|
||||
except NotFound:
|
||||
logger.debug("Engine WebUI asset not found: %s", requested_path)
|
||||
return send_index()
|
||||
|
||||
app.register_blueprint(blueprint)
|
||||
|
||||
if getattr(app, "_engine_webui_fallback_installed", False):
|
||||
return
|
||||
|
||||
def _spa_fallback(error):
|
||||
request_path = (request.path or "").strip()
|
||||
if request_path.startswith("/api") or request_path.startswith("/socket.io"):
|
||||
return error
|
||||
if "." in Path(request_path).name:
|
||||
return error
|
||||
if request.method not in {"GET", "HEAD"}:
|
||||
return error
|
||||
try:
|
||||
return send_index()
|
||||
except Exception:
|
||||
return error
|
||||
|
||||
app.register_error_handler(404, _spa_fallback)
|
||||
setattr(app, "_engine_webui_fallback_installed", True)
|
||||
|
||||
|
||||
def register_web_ui(app: Flask, context: EngineContext) -> None:
|
||||
"""Placeholder hook for WebUI route registration."""
|
||||
"""Register static asset routes for the Engine WebUI."""
|
||||
|
||||
context.logger.debug("Engine WebUI services are not yet implemented.")
|
||||
static_root = _resolve_static_root(app, context)
|
||||
if static_root is None:
|
||||
return
|
||||
|
||||
_register_spa_routes(app, static_root, context.logger)
|
||||
context.logger.info("Engine WebUI registered static assets from %s", static_root)
|
||||
|
||||
Reference in New Issue
Block a user