Fixed Engine Flask Server Accessibility

This commit is contained in:
2025-10-26 21:46:23 -06:00
parent d3e728c127
commit 95b3e55bc7
10 changed files with 392 additions and 57 deletions

View File

@@ -17,7 +17,7 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Iterable, Mapping, Optional, Sequence
from flask import Flask
from flask import Blueprint, Flask, jsonify
from Modules.auth import jwt_service as jwt_service_module
from Modules.auth.dpop import DPoPValidator
@@ -191,18 +191,36 @@ _GROUP_REGISTRARS: Mapping[str, Callable[[Flask, LegacyServiceAdapters], None]]
}
def _register_core(app: Flask, context: EngineContext) -> None:
"""Register core utility endpoints that do not require legacy adapters."""
blueprint = Blueprint("engine_core", __name__)
@blueprint.route("/health", methods=["GET"])
def health() -> Any:
return jsonify({"status": "ok"})
app.register_blueprint(blueprint)
context.logger.info("Engine registered API group 'core'.")
def register_api(app: Flask, context: EngineContext) -> None:
"""Register Engine API blueprints based on the enabled groups."""
enabled_groups: Iterable[str] = context.api_groups or DEFAULT_API_GROUPS
normalized = [group.strip().lower() for group in enabled_groups if group]
adapters = LegacyServiceAdapters(context)
adapters: Optional[LegacyServiceAdapters] = None
for group in normalized:
if group == "core":
_register_core(app, context)
continue
if adapters is None:
adapters = LegacyServiceAdapters(context)
registrar = _GROUP_REGISTRARS.get(group)
if registrar is None:
context.logger.info("Engine API group '%s' is not implemented; skipping.", group)
continue
registrar(app, adapters)
context.logger.info("Engine registered API group '%s'.", group)

View File

@@ -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)