# -*- coding: utf-8 -*- """站点数据端点:走 core.db 异步 SQLite(读并发/写串行/WAL)。 这些是 `/api/v1` 下的只读端点,走异步读写路径并共享同一份站点库; 鉴权端点(需登录/管理员)由 web.deps 统一把关。 注意:本模块只做读(与 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)