feat: initial MVP - FastAPI backend + Vue3 frontend + docker-compose
- backend: FastAPI + SQLAlchemy 2.0(async) + asyncpg + Alembic - 7 API routes: auth/me/articles/sources/bookmarks/subscriptions/admin - models: User/Source/Article/Bookmark/Subscription/ApiToken - services: RSS fetcher (feedparser) + Tencent TMT translator with quota + cache + local NLLB fallback - workers: APScheduler + asyncio pipeline (fetch -> dedupe -> insert -> translate) - seed scripts: create_user, seed_sources (5 RSS: Reuters/BBC/Al Jazeera/NHK/DW) - frontend: Vue 3 + Vite + Naive UI + Pinia + vue-router - pages: Login, Feed (24h), ArticleDetail, Sources, Bookmarks, AdminSources - deploy: docker-compose (postgres/redis/api/worker/frontend/caddy) - docs: README, DEPLOY, architecture, acceptance
This commit is contained in:
68
backend/app/api/me.py
Normal file
68
backend/app/api/me.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""/me 当前用户信息 + 翻译配额。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import settings
|
||||
from app.core.deps import get_current_user
|
||||
from app.database import get_session
|
||||
from app.models.user import User
|
||||
from app.redis_client import get_redis
|
||||
|
||||
router = APIRouter(prefix="/me", tags=["me"])
|
||||
|
||||
|
||||
class MeOut(BaseModel):
|
||||
id: int
|
||||
username: str
|
||||
email: str | None
|
||||
role: str
|
||||
display_name: str | None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class UsageOut(BaseModel):
|
||||
month: str
|
||||
used_chars: int
|
||||
quota_chars: int
|
||||
remaining_chars: int
|
||||
buffered_quota: int
|
||||
pct_used: float
|
||||
|
||||
|
||||
@router.get("", response_model=MeOut)
|
||||
async def me(user: User = Depends(get_current_user)):
|
||||
return MeOut(
|
||||
id=user.id,
|
||||
username=user.username,
|
||||
email=user.email,
|
||||
role=user.role.value,
|
||||
display_name=user.display_name,
|
||||
created_at=user.created_at,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/usage", response_model=UsageOut)
|
||||
async def usage(
|
||||
user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_session), # noqa: ARG001
|
||||
):
|
||||
r = get_redis()
|
||||
now = datetime.now(timezone.utc)
|
||||
key = f"translation:month:{now:%Y%m}"
|
||||
used = int(await r.get(key) or 0)
|
||||
quota = settings.tencent_tmt_quota_month
|
||||
buffered = int(quota * (1 - settings.tencent_tmt_quota_buffer))
|
||||
remaining = max(0, quota - used)
|
||||
return UsageOut(
|
||||
month=f"{now:%Y%m}",
|
||||
used_chars=used,
|
||||
quota_chars=quota,
|
||||
remaining_chars=remaining,
|
||||
buffered_quota=buffered,
|
||||
pct_used=round(used / quota * 100, 2) if quota else 0.0,
|
||||
)
|
||||
Reference in New Issue
Block a user