shared/README.md

71 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# shared — TLYQ 共享标准库
tlyq.ai 各站点共享的标准库,消除代码重复,统一基础设施实现。
## 目录结构
```
shared/
├── lib/
│ ├── auth/ # 认证标准库
│ │ ├── config.ts # config 工厂createAuthConfig
│ │ ├── jwt.ts # JWT 签发/验证
│ │ ├── ldap.ts # LDAP 认证
│ │ ├── middleware.ts # middleware 工厂createMiddleware
│ │ ├── oidc.ts # OIDC 客户端
│ │ ├── types.ts # 类型定义
│ │ └── user-sync.ts # OIDC 用户同步
│ ├── audit/ # 审计日志标准库
│ │ ├── audit-schema.ts # 表结构常量
│ │ └── write-audit-log.ts # writeAuditLog + diffObjects + getClientIP
│ ├── wechat/ # 企业微信推送
│ │ ├── wechat-pusher.ts # WeChatPusher 类
│ │ └── message-formatter.ts # 消息格式化工具
│ ├── alert/ # 告警管理
│ │ ├── alert-manager.ts # AlertManager去重/冷却/免打扰)
│ │ ├── health-checker.ts # HealthCheckerHTTP/Docker
│ │ └── types.ts # 类型定义
│ └── db/ # 数据库 schema
│ └── alert-schema.ts # CREATE TABLE 常量
└── ui/ # 共享 UI 组件
└── login-page.tsx # 统一登录页
```
## 使用方式
### 1. 创建 symlink
```bash
ln -sf ../shared {站点目录}/shared
```
### 2. 配置 tsconfig
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@shared/*": ["./shared/*"]
}
}
}
```
### 3. 引用
```typescript
import { createAuthConfig } from '@shared/lib/auth/config'
import { createMiddleware } from '@shared/lib/auth/middleware'
import { writeAuditLog } from '@shared/lib/audit/write-audit-log'
import { WeChatPusher } from '@shared/lib/wechat/wechat-pusher'
```
## 设计原则
- 零业务依赖:纯基础设施,不含站点特有逻辑
- 配置驱动:所有环境变量通过参数传入,不硬编码
- 接口稳定:包装器保持各站点原有调用方式不变
- Edge Runtime 兼容middleware 仅用 atob不用 Node.js crypto