# -*- coding: utf-8 -*- """站点数据端点(FastAPI 栈):走 core.db 异步 SQLite(读并发/写串行/WAL)。 这些是**新增**的 `/api/v1` 只读端点,用于验证异步读写路径与共享同一份站点库; 不替代、不影响 Flask 侧既有端点。鉴权端点(需登录/管理员)待会话互通层就绪后再迁移。 注意:本模块只做读(与 announcements/settings 这些低风险资源对应), 写操作仍归 auth_system 的事务口径,避免迁移期两套写路径互相踩踏。 """ from fastapi import APIRouter from core import repo from web.response import error, success router = APIRouter() @router.get('/site/announcements') async def list_announcements(limit: int = 20): """公告列表(异步读;对应 settings/announcements 表)。""" try: items = await repo.list_announcements(max(1, min(limit, 100))) return success(items, '公告获取成功') except Exception: return error('服务暂时不可用,请稍后再试', 500) @router.get('/site/stats') async def site_stats(): """站点规模概览(异步并发读多张表)。""" try: import asyncio total, admins, open_ledger = await asyncio.gather( repo.count_users(), repo.count_users('admin'), repo.ledger_open_count(), ) return success({ 'users': total, 'admins': admins, 'open_ledger': open_ledger, }, '站点统计获取成功') except Exception: return error('服务暂时不可用,请稍后再试', 500)