Skip to content

Architecture

ZZzzswszzZZ edited this page Apr 16, 2026 · 1 revision

系统架构

技术栈

技术
后端 Fastify 5 + TypeScript
数据库 SQLite (better-sqlite3),自动迁移
前端 Vue 3 + shadcn-vue + Tailwind CSS
加密 AES-256-GCM(后端 API Key 存储)

请求流程

客户端请求
  → auth middleware (Bearer token 校验)
  → proxy handler
    → 查找后端服务
    → 模型映射 (client_model → server_model)
    → 解密 API Key
    → 转发请求到上游
  → 记录 request_logs(完整链路)
  → 响应客户端

核心模块

  • 入口 src/index.tsbuildApp() 组装所有插件,支持注入 db(测试用 in-memory)
  • 代理层 src/proxy/openai.ts / anthropic.ts — Fastify 插件,各含流式和非流式代理函数。流式使用 PassThrough + 空闲超时防泄漏
  • 认证 src/middleware/auth.ts — 全局 onRequest hook;admin-auth.ts 用 JWT + Cookie 做管理后台认证
  • 数据库 src/db/index.ts — 启动时自动执行迁移,所有 CRUD 集中管理
  • 管理 API src/admin/ — routes → services / mappings / logs / stats 分层
  • 加密 src/utils/crypto.ts — AES-256-GCM 加解密

关键设计决策

  • 代理用原生 http.request 而非 axios:需要直接操作 SSE 流
  • fastify-plugin 打破封装,使代理 hook 作用于全局
  • 测试通过 buildApp({ db: inMemoryDb }) 注入内存数据库,无需 mock

前端架构

  • 路由在 frontend/src/router/index.ts,管理页面在 /admin/*
  • API 客户端在 frontend/src/api/client.ts
  • 开发时 Vite 代理 /admin/api → 后端 :3000;生产时 @fastify/static 托管构建产物

Clone this wiki locally