- 搜索:源级剪枝(ASCII 名字+别名 / 中文分类+别名+源信息)、负面变体位图化、MAX_CANDIDATES 保持 5000 以兼容结果集 - 缓存:查询级响应缓存(序列化+gzip+ETag,LRU 400)、/sources 与 /skills 预序列化预压缩、readItem LRU、findByName Map - 传输:>1KB gzip、弱 ETag + 304、成功响应 max-age=30、错误 no-store - 新增 /icons/batch、/posters/batch(单次 50 条)、/sources?compact=1 - 验证:新旧版本 44/44 端点 data+meta 逐字节一致,30 项功能回归全通过,并发 100 零错误 - 附优化前后对比报告 api/PERF-REPORT-20261108.md
397 lines
14 KiB
Markdown
397 lines
14 KiB
Markdown
# 画个标素材 API
|
||
|
||
面向公司内部项目的**素材检索服务**:按关键词查图标/海报素材,直接拿到可嵌入的 SVG。
|
||
|
||
- 线上地址:`https://drawlogo.zhuig.com/api/v1`
|
||
- 鉴权:API Key(`X-API-Key` 请求头)
|
||
- 数据规模:图标 **501,584** 个 / 268 源;海报 **11,598** 个 / 71 源;技能元数据 **139** 个(共享 55 + 海报 64 + 表情 20)
|
||
- 性能:静态端点(源清单/技能/热门查询)走响应缓存 + gzip + ETag,实测单机 1w+ RPS、p50 < 2ms(详见 [PERF-REPORT-20261108.md](./PERF-REPORT-20261108.md))
|
||
- 特性:支持**中文语义检索**(搜「学校」命中 school / graduation-cap / book…,词表 619 条中文 → 2,427 英文词根)、按协议过滤商用可用素材、默认排除 `-off`/`-error` 等负面变体
|
||
- 含 **3D 位图图标**:微软 Fluent Emoji 3D(1,586 个)+ Google Noto Emoji 3D(3,797 个):`svg` 字段内是 `<image>` 引用的 PNG,服务端已自动内联为 data URI,调用方无需再取静态资源
|
||
- 含 **字体素材**:Google Fonts latin 六档字重(300–800)+ 扩展字符集(latin-ext/cyrillic/greek/vietnamese),woff2 可直接 `@font-face` 引用
|
||
|
||
> 本服务只提供**素材与技能元数据**,不提供 AI 生成 LOGO(当前 AI 生成质量不足以对外提供)。
|
||
|
||
---
|
||
|
||
## 一、快速开始
|
||
|
||
```bash
|
||
# 1. 搜索(中文或英文)
|
||
curl -H "X-API-Key: <你的Key>" \
|
||
"https://drawlogo.zhuig.com/api/v1/icons/search?q=学校&limit=5"
|
||
|
||
# 2. 直接拿 SVG(可直接放进 <img src> 或 fetch)
|
||
curl -H "X-API-Key: <你的Key>" \
|
||
"https://drawlogo.zhuig.com/api/v1/icons/lucide/school.svg"
|
||
|
||
# 3. 查看有哪些源(免鉴权)
|
||
curl "https://drawlogo.zhuig.com/api/v1/sources"
|
||
|
||
# 4. 3D 位图图标(中文也能搜到英文命名的 3D 表情)
|
||
curl -H "X-API-Key: <你的Key>" \
|
||
"https://drawlogo.zhuig.com/api/v1/icons/search?q=爱心&source=fluent-emoji-3d&limit=5"
|
||
```
|
||
|
||
---
|
||
|
||
## 二、鉴权
|
||
|
||
请求头二选一:
|
||
|
||
```
|
||
X-API-Key: dl_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||
Authorization: Bearer dl_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||
```
|
||
|
||
- Key 由管理员用 `node admin.js create --name "调用方"` 生成,**明文只显示一次**
|
||
- 服务端只存 SHA-256 摘要,无法反查明文
|
||
- 每个 Key 独立配置:分钟限流、每日配额、启用/禁用
|
||
|
||
**免鉴权端点**(仅基础信息):`/health`、`/stats`、`/sources`
|
||
|
||
---
|
||
|
||
## 三、端点
|
||
|
||
所有成功响应:
|
||
|
||
```json
|
||
{ "ok": true, "data": ..., "meta": { "total": 1059, "limit": 5, "offset": 0 } }
|
||
```
|
||
|
||
所有失败响应:
|
||
|
||
```json
|
||
{ "ok": false, "error": { "code": "UNAUTHORIZED", "message": "缺少 API Key" } }
|
||
```
|
||
|
||
### 1. 健康检查
|
||
|
||
```
|
||
GET /api/v1/health # 免鉴权
|
||
```
|
||
```json
|
||
{ "ok": true, "data": { "status": "up", "version": "1.0.0", "builtAt": "...", "uptime": 3600 } }
|
||
```
|
||
|
||
### 2. 运行统计
|
||
|
||
```
|
||
GET /api/v1/stats # 免鉴权
|
||
```
|
||
|
||
### 3. 源清单
|
||
|
||
```
|
||
GET /api/v1/sources?kind=icons|posters&compact=1 # 免鉴权
|
||
```
|
||
返回每个源的 `id / name / nameCn / desc / type / license / commercial / count / workspaces`。
|
||
**接入前建议先读这个**,确认目标源协议是否允许商用。
|
||
|
||
`compact=1` 只返回 `id / name / nameCn / type / license / commercial / count`(去掉 `desc`/`licenseNote`/`style`/`workspaces`),体积约为完整版的 1/3,适合只需要源 id 列表的场景。
|
||
|
||
### 4. 图标搜索
|
||
|
||
```
|
||
GET /api/v1/icons/search
|
||
```
|
||
|
||
| 参数 | 说明 | 默认 |
|
||
|---|---|---|
|
||
| `q` | 关键词(英文名 / 中文分类 / 中文语义) | 空=全部 |
|
||
| `source` | 限定源 id,如 `lucide` | 全部 |
|
||
| `type` | `icon`(单色)/ `emoji`(多色)/ `brand`(品牌) | 全部 |
|
||
| `commercial` | `1`=仅商用可用(排除 BY-NC/BY-SA/BY-ND/GPL) | 不过滤 |
|
||
| `dedupe` | `1`=按图标名去重(多源同名只留最优一个) | `0` |
|
||
| `include_negative` | `1`=包含 `-off`/`-error` 等负面变体 | `0` |
|
||
| `limit` | 1–100 | `20` |
|
||
| `offset` | 分页偏移 | `0` |
|
||
|
||
响应项:
|
||
|
||
```json
|
||
{
|
||
"id": "school",
|
||
"name": "school",
|
||
"categoryCn": "教育学习",
|
||
"type": "icon",
|
||
"source": "lucide",
|
||
"sourceNameCn": "Lucide 图标",
|
||
"license": "ISC",
|
||
"commercial": true,
|
||
"index": 1321
|
||
}
|
||
```
|
||
|
||
> `index` 是该素材在源内的序号,用于内部定位;外部调用请用 `source` + `name`。
|
||
|
||
排序规则:名称精确 > 名称前缀 > 名称包含 > 中文别名精确 > 分类匹配 > 别名包含;同分时主流库(lucide/tabler/heroicons/feather…)优先、短名优先。
|
||
|
||
### 5. 图标详情 / SVG 直链
|
||
|
||
```
|
||
GET /api/v1/icons/{source}/{name} # JSON(含 svg 原文)
|
||
GET /api/v1/icons/{source}/{name}.svg # image/svg+xml
|
||
```
|
||
|
||
SVG 直链返回:
|
||
|
||
```html
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
|
||
<path d="..." />
|
||
</svg>
|
||
```
|
||
|
||
- 单色图标(`type=icon`)带 `fill="currentColor"`,颜色由 CSS 的 `color` 控制
|
||
- 多色图标(`type=emoji/brand`)保留原色,不加 `fill`
|
||
- 建议在 `<img>` 中用时手动加 `style="color:#1B4F72"`
|
||
- **3D 位图图标**(源 `fluent-emoji-3d`)的 `svg` 是一层 `<image>`,`href` 已由服务端内联为 `data:image/png;base64,...`(原图 256×256),`/icons/fluent-emoji-3d/red-heart.svg` 可直接当图片用;JSON 与 `.svg` 两种响应都是自包含的
|
||
|
||
### 6. 批量取素材(合并请求)
|
||
|
||
```
|
||
GET /api/v1/icons/batch?items=lucide:house,lucide:star,tabler:home
|
||
GET /api/v1/posters/batch?items=<源id>:<素材名>,...
|
||
```
|
||
|
||
一次请求最多 50 条,**替代「循环调详情接口」**(把 N 次 RTT + N 次鉴权/限流开销压成 1 次)。逐条返回独立结果,单条失败不影响其他条目:
|
||
|
||
```json
|
||
{ "ok": true, "data": [
|
||
{ "ref": "lucide:house", "item": { "id": "lucide:house", "svg": "...", "source": "lucide", "license": "ISC", "commercial": true } },
|
||
{ "ref": "bogus:xxx", "error": "source_not_found" }
|
||
], "meta": { "count": 2 } }
|
||
```
|
||
|
||
错误值为 `source_not_found` / `not_found` / `read_failed`。
|
||
|
||
### 7. 随机图标
|
||
|
||
```
|
||
GET /api/v1/icons/random?count=10&source=lucide
|
||
```
|
||
适合做占位图、灵感墙、测试数据。
|
||
|
||
### 8. 海报素材
|
||
|
||
```
|
||
GET /api/v1/posters/search?q=&type=&source=&limit=
|
||
GET /api/v1/posters/{source}/{name}
|
||
```
|
||
`type` 为 `palette`(配色)/ `font`(字体)/ `bg`(背景)/ `decoration`(装饰)。
|
||
配色类素材的 `data` 字段内联了色值数组;字体类给出 `file` 路径(相对静态站根目录)。
|
||
|
||
### 9. 技能元数据
|
||
|
||
```
|
||
GET /api/v1/skills?workspace=logo&step=3&category=color
|
||
GET /api/v1/skills/{id}
|
||
```
|
||
返回技能的 `id / name / category / scene / summary / params / step / kind / workspaces`。
|
||
**仅元数据**(可直接当设计知识/评审规则用),不执行 `run`。
|
||
|
||
---
|
||
|
||
## 四、中文检索说明
|
||
|
||
图标名是英文(`graduation-cap`),分类是中文宽类目(`教育学习`)。为了支持「学校」「医院」「餐厅」这类词,服务内置了一张**中文语义别名表**(425 个高频词 → 英文词根,含表情/3D 图标场景词),构建索引时为每个图标预计算别名。
|
||
|
||
- 搜「学校」→ school / graduation-cap / book / backpack…
|
||
- 搜「医院」→ hospital / ambulance / stethoscope / pill…
|
||
- 搜「工厂」→ factory / building-factory…
|
||
- 搜「飞机」→ plane / plane-landing / plane-takeoff…
|
||
- 搜「爱心」→ heart / red-heart / two-hearts…(3D 表情等英文命名素材同样命中)
|
||
|
||
词表产物是 [js/data/cn-tags.js](../js/data/cn-tags.js),由 [scripts/gen-cn-tags.js](../scripts/gen-cn-tags.js) 生成(种子 = [build-index.js](./build-index.js) 的 `CN_ALIAS` + 生成器内 `EXTRA` / `EMOJI_EXTRA` 场景词)。加词请改生成器后重跑 `node scripts/gen-cn-tags.js`,再重建索引。
|
||
|
||
---
|
||
|
||
## 五、限流与配额
|
||
|
||
| 项 | 默认 | 说明 |
|
||
|---|---|---|
|
||
| 分钟限流 | 60 次/分钟/Key | 创建时可调,如 `--rate 120` |
|
||
| 每日配额 | 20,000 次/天/Key | 创建时可调,如 `--quota 30000` |
|
||
|
||
响应头:
|
||
|
||
```
|
||
X-RateLimit-Limit: 120
|
||
X-RateLimit-Remaining: 118
|
||
```
|
||
|
||
超限返回 `429`,`Retry-After: 60`。
|
||
|
||
### 错误码
|
||
|
||
| HTTP | code | 含义 |
|
||
|---|---|---|
|
||
| 400 | `BAD_REQUEST` | 参数非法 |
|
||
| 401 | `UNAUTHORIZED` | 未带 Key |
|
||
| 403 | `INVALID_KEY` | Key 无效或已禁用 |
|
||
| 404 | `NOT_FOUND` | 素材/源/技能不存在 |
|
||
| 405 | `METHOD_NOT_ALLOWED` | 仅支持 GET |
|
||
| 429 | `RATE_LIMITED` | 分钟限流 |
|
||
| 429 | `DAILY_QUOTA_EXCEEDED` | 日配额用尽 |
|
||
| 500 | `INTERNAL` | 服务内部错误 |
|
||
| 503 | `NOT_READY` | 索引未构建 |
|
||
|
||
### 传输优化(gzip / ETag / 缓存)
|
||
|
||
服务端默认对 `> 1KB` 的 JSON 响应做 gzip,并返回 `ETag`:
|
||
|
||
```
|
||
Content-Encoding: gzip
|
||
Vary: Accept-Encoding
|
||
ETag: W/"xxxxxxxxxxxxxxxxxxxxxx"
|
||
Cache-Control: public, max-age=30, stale-while-revalidate=60
|
||
```
|
||
|
||
- 客户端带 `Accept-Encoding: gzip` 即可拿到压缩响应(`/sources` 从 108.6KB → 20.9KB,`/skills` 从 12KB → 4KB)
|
||
- 重复请求把上次的 `ETag` 放进 `If-None-Match`,服务端返回 `304 Not Modified`(**零响应体**),适合客户端/CDN 做条件请求
|
||
- 错误响应统一 `Cache-Control: no-store`,不会被中间层缓存
|
||
|
||
---
|
||
|
||
## 六、接入示例
|
||
|
||
### JavaScript / 前端
|
||
|
||
```js
|
||
const KEY = '...'; // 注意:放前端会暴露给访客,建议走后端代理
|
||
const r = await fetch('/api/v1/icons/search?q=学校&limit=8', {
|
||
headers: { 'X-API-Key': KEY }
|
||
});
|
||
const { data } = await r.json();
|
||
data.forEach(ic => {
|
||
const img = new Image();
|
||
img.src = `/api/v1/icons/${ic.source}/${ic.name}.svg`;
|
||
});
|
||
```
|
||
|
||
### Node / 后端
|
||
|
||
```js
|
||
const res = await fetch('https://drawlogo.zhuig.com/api/v1/icons/search?q=hospital&limit=5', {
|
||
headers: { 'X-API-Key': process.env.DRAWLOGO_API_KEY }
|
||
});
|
||
const { data } = await res.json();
|
||
```
|
||
|
||
### Java(Spring RestTemplate)
|
||
|
||
```java
|
||
HttpHeaders h = new HttpHeaders();
|
||
h.set("X-API-Key", System.getenv("DRAWLOGO_API_KEY"));
|
||
String url = "https://drawlogo.zhuig.com/api/v1/icons/search?q=%E5%AD%A6%E6%A0%A1&limit=5";
|
||
ResponseEntity<String> resp = new RestTemplate().exchange(url, HttpMethod.GET, new HttpEntity<>(h), String.class);
|
||
```
|
||
|
||
### Python
|
||
|
||
```python
|
||
import os, requests
|
||
r = requests.get(
|
||
"https://drawlogo.zhuig.com/api/v1/icons/search",
|
||
params={"q": "学校", "limit": 5},
|
||
headers={"X-API-Key": os.environ["DRAWLOGO_API_KEY"]},
|
||
)
|
||
print(r.json()["data"])
|
||
```
|
||
|
||
---
|
||
|
||
## 七、协议合规(重要)
|
||
|
||
- 调用 `/api/v1/sources` 可看到每个源的 `license` 与 `commercial`
|
||
- 商业项目请加 `commercial=1`,或自行校验 `commercial: true`
|
||
- 以下协议**不可商用**:`BY-NC`、`BY-SA`、`BY-ND`、`GPL`
|
||
- 品牌类图标(`type=brand`,如 Instagram / 微信 logo)有商标风险,做 LOGO 时避免直接使用
|
||
- 建议在产物中标注素材来源与协议(源 `name` + `license`)
|
||
|
||
---
|
||
|
||
## 八、运维
|
||
|
||
### 重建索引(素材库更新后)
|
||
|
||
```bash
|
||
cd /opt/drawlogo-api
|
||
DRAWLOGO_ROOT=/opt/drawlogo node --max-old-space-size=1536 build-index.js
|
||
sudo systemctl restart drawlogo-api
|
||
```
|
||
|
||
索引产物在 `data/`(约 530 MB),**不纳入 git**。
|
||
|
||
### Key 管理
|
||
|
||
**Web 管理后台**(推荐):`https://drawlogo.zhuig.com/api-admin.html`
|
||
|
||
- 输入管理口令进入(口令 = 服务器 `config.json` 的 `adminToken`)
|
||
- 可视化签发 Key:填写**使用者**、**备注**、**每分钟限流**、**每日配额**、**有效期**
|
||
- 列表可查看今日用量、最近使用时间,支持**启用/禁用/编辑/删除**
|
||
- **内部使用模式**:Key 明文保存在 `data/apikeys.json`(权限 600),后台可随时查看掩码、点「复制」一键复制 **地址 + 使用者 + 口令**,直接发给调用方
|
||
- 早期只存哈希的旧 Key 无法查看明文,重新生成一个即可(旧 Key 可删除)
|
||
- 若未来商业化,建议改回「仅存哈希 + 明文只显示一次」
|
||
|
||
> 管理口令与普通 API Key 完全分离;普通 Key 无法访问管理接口。口令为空时后台接口整体关闭。
|
||
|
||
**命令行**(服务器 `/opt/drawlogo-api`):
|
||
|
||
```bash
|
||
cd /opt/drawlogo-api
|
||
node admin.js create --name "项目A" --rate 120 --quota 30000
|
||
node admin.js list
|
||
node admin.js disable <key_id>
|
||
node admin.js enable <key_id>
|
||
node admin.js revoke <key_id>
|
||
```
|
||
|
||
Key 文件 `data/apikeys.json` 权限 600,服务每 5 秒热加载,**改 Key 无需重启**。
|
||
|
||
### 服务与反向代理
|
||
|
||
```bash
|
||
sudo systemctl status drawlogo-api
|
||
sudo journalctl -u drawlogo-api -f
|
||
```
|
||
|
||
- systemd 单元:[deploy/drawlogo-api.service](./deploy/drawlogo-api.service)
|
||
- nginx 片段:[deploy/nginx-location.conf](./deploy/nginx-location.conf)(必须 `location ^~ /api/`)
|
||
- 幂等写入脚本:`sudo python3 deploy/patch-nginx.py`
|
||
|
||
### 更新 API 代码
|
||
|
||
```bash
|
||
# 本地
|
||
scp api/server.js api/build-index.js api/admin.js ubuntu@119.45.242.239:/opt/drawlogo-api/
|
||
scp api/deploy/* ubuntu@119.45.242.239:/opt/drawlogo-api/deploy/
|
||
# 服务器
|
||
sudo systemctl restart drawlogo-api
|
||
```
|
||
|
||
> 注意:静态站的 Gitea 自动部署只更新 `/opt/drawlogo`,**不会**更新 `/opt/drawlogo-api`,API 代码需按上面的步骤单独发布。
|
||
|
||
---
|
||
|
||
## 九、架构
|
||
|
||
```
|
||
公司各项目
|
||
│ HTTPS + X-API-Key
|
||
▼
|
||
nginx (drawlogo.zhuig.com)
|
||
├── / → /opt/drawlogo(静态站)
|
||
└── ^~ /api/ → 127.0.0.1:7891(drawlogo-api)
|
||
├── data/*.idx.json 常驻内存(检索索引)
|
||
├── data/*.jsonl 按字节偏移随机读取(素材正文)
|
||
├── data/apikeys.json Key(SHA-256)
|
||
└── data/skills.json 技能元数据
|
||
```
|
||
|
||
- 零第三方依赖(Node 原生 `http`/`fs`)
|
||
- 索引常驻约 130–170 MB;素材正文按需 seek,单次读几百字节
|
||
- 服务只监听 `127.0.0.1`,公网仅经 nginx HTTPS 进入
|
||
- 内存上限 900 MB(systemd `MemoryMax`),避免影响同机其他项目
|