Archived
feat(db-async): 站点数据异步读写仓储 + FastAPI 生命周期
- core/repo.py:经由 core.db(aiosqlite) 读写 auth_system 的**同一份** site.db (settings/announcements/users 只读/ledger 只读),字段口径对齐 auth_system; 读并发、写串行、WAL 读写同时进行 - web/routers/site.py:/api/v1/site/announcements、/api/v1/site/stats(异步多表并发读) - web/app.py:lifespan 关闭时释放 aiohttp 连接池(含按 loop 隔离的 session) 写路径说明:迁移期写操作仍归 auth_system 的 _DB_LOCK 事务口径,避免两套写 路径互相踩踏;本层先只做读。 验证:pytest 22/22(新增 6 项含「异步读 + 同步写者并发 WAL」); /api/v1/site/* 实测读到共享库(3 用户/1 管理员);ruff/pyright 干净。
This commit is contained in:
1 parent
ce7ea66d6a
commit
0f2e72db44
4 files changed
+219
No files matched your search
@@ -0,0 +1,45 @@
|
||||
# -*- 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)
|
||||
Reference in new issue
Block a user