refactor: move supporters loading to separate API endpoint

- Add SupportersHandler in misc_handlers.py to serve /api/lm/supporters
- Register new endpoint in misc_route_registrar.py
- Remove supporters from page load template context in model_handlers.py
- Create supportersService.js for frontend data fetching
- Update Header.js to fetch supporters when support modal opens
- Modify support_modal.html to use client-side rendering

This change improves page load performance by loading supporters data
on-demand instead of during initial page render.
This commit is contained in:
Will Miao
2026-02-28 20:14:20 +08:00
parent 77a2215e62
commit 78b55d10ba
20 changed files with 1206 additions and 89 deletions

View File

@@ -9,6 +9,7 @@ objects that can be composed by the route controller.
from __future__ import annotations
import asyncio
import json
import logging
import os
import subprocess
@@ -218,6 +219,45 @@ class HealthCheckHandler:
return web.json_response({"status": "ok"})
class SupportersHandler:
"""Handler for supporters data."""
def __init__(self, logger: logging.Logger | None = None) -> None:
self._logger = logger or logging.getLogger(__name__)
def _load_supporters(self) -> dict:
"""Load supporters data from JSON file."""
try:
current_file = os.path.abspath(__file__)
root_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(current_file)))
)
supporters_path = os.path.join(root_dir, "data", "supporters.json")
if os.path.exists(supporters_path):
with open(supporters_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
self._logger.debug(f"Failed to load supporters data: {e}")
return {
"specialThanks": [],
"allSupporters": [],
"totalCount": 0
}
async def get_supporters(self, request: web.Request) -> web.Response:
"""Return supporters data as JSON."""
try:
supporters = self._load_supporters()
return web.json_response({"success": True, "supporters": supporters})
except Exception as exc:
self._logger.error("Error loading supporters: %s", exc, exc_info=True)
return web.json_response(
{"success": False, "error": str(exc)}, status=500
)
class SettingsHandler:
"""Sync settings between backend and frontend."""
@@ -1482,6 +1522,7 @@ class MiscHandlerSet:
metadata_archive: MetadataArchiveHandler,
filesystem: FileSystemHandler,
custom_words: CustomWordsHandler,
supporters: SupportersHandler,
) -> None:
self.health = health
self.settings = settings
@@ -1494,6 +1535,7 @@ class MiscHandlerSet:
self.metadata_archive = metadata_archive
self.filesystem = filesystem
self.custom_words = custom_words
self.supporters = supporters
def to_route_mapping(
self,
@@ -1522,6 +1564,7 @@ class MiscHandlerSet:
"open_file_location": self.filesystem.open_file_location,
"open_settings_location": self.filesystem.open_settings_location,
"search_custom_words": self.custom_words.search_custom_words,
"get_supporters": self.supporters.get_supporters,
}

View File

@@ -66,6 +66,27 @@ class ModelPageView:
self._logger = logger
self._app_version = self._get_app_version()
def _load_supporters(self) -> dict:
"""Load supporters data from JSON file."""
try:
current_file = os.path.abspath(__file__)
root_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(current_file)))
)
supporters_path = os.path.join(root_dir, "data", "supporters.json")
if os.path.exists(supporters_path):
with open(supporters_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
self._logger.debug(f"Failed to load supporters data: {e}")
return {
"specialThanks": [],
"allSupporters": [],
"totalCount": 0
}
def _get_app_version(self) -> str:
version = "1.0.0"
short_hash = "stable"

View File

@@ -26,6 +26,7 @@ MISC_ROUTE_DEFINITIONS: tuple[RouteDefinition, ...] = (
RouteDefinition("GET", "/api/lm/settings/libraries", "get_settings_libraries"),
RouteDefinition("POST", "/api/lm/settings/libraries/activate", "activate_library"),
RouteDefinition("GET", "/api/lm/health-check", "health_check"),
RouteDefinition("GET", "/api/lm/supporters", "get_supporters"),
RouteDefinition("POST", "/api/lm/open-file-location", "open_file_location"),
RouteDefinition("POST", "/api/lm/update-usage-stats", "update_usage_stats"),
RouteDefinition("GET", "/api/lm/get-usage-stats", "get_usage_stats"),

View File

@@ -29,6 +29,7 @@ from .handlers.misc_handlers import (
NodeRegistry,
NodeRegistryHandler,
SettingsHandler,
SupportersHandler,
TrainedWordsHandler,
UsageStatsHandler,
build_service_registry_adapter,
@@ -119,6 +120,7 @@ class MiscRoutes:
metadata_provider_factory=self._metadata_provider_factory,
)
custom_words = CustomWordsHandler()
supporters = SupportersHandler()
return self._handler_set_factory(
health=health,
@@ -132,6 +134,7 @@ class MiscRoutes:
metadata_archive=metadata_archive,
filesystem=filesystem,
custom_words=custom_words,
supporters=supporters,
)