feat(api): 新增免鉴权在线接口说明 /api/v1/docs,并让错误响应自带文档入口
- GET /api/v1/docs:返回 README 的调用方章节(text/markdown + gzip,mtime 缓存,截断运维/架构章节) - 401/403/404/429 等所有错误体新增 error.docs,新接入方首次调用即可自助查阅 - /health、/stats 新增 docs 字段 - README 补充入口说明与在线版调用示例
This commit is contained in:
+14
-1
@@ -31,6 +31,9 @@ curl "https://drawlogo.zhuig.com/api/v1/sources"
|
|||||||
# 4. 3D 位图图标(中文也能搜到英文命名的 3D 表情)
|
# 4. 3D 位图图标(中文也能搜到英文命名的 3D 表情)
|
||||||
curl -H "X-API-Key: <你的Key>" \
|
curl -H "X-API-Key: <你的Key>" \
|
||||||
"https://drawlogo.zhuig.com/api/v1/icons/search?q=爱心&source=fluent-emoji-3d&limit=5"
|
"https://drawlogo.zhuig.com/api/v1/icons/search?q=爱心&source=fluent-emoji-3d&limit=5"
|
||||||
|
|
||||||
|
# 5. 接口说明在线版(免鉴权,Markdown 原文,可直接丢给 AI 或调用方)
|
||||||
|
curl "https://drawlogo.zhuig.com/api/v1/docs"
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -63,9 +66,19 @@ Authorization: Bearer dl_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|||||||
所有失败响应:
|
所有失败响应:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{ "ok": false, "error": { "code": "UNAUTHORIZED", "message": "缺少 API Key" } }
|
{ "ok": false, "error": { "code": "UNAUTHORIZED", "message": "缺少 API Key", "docs": "https://drawlogo.zhuig.com/api/v1/docs" } }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> `error.docs` 恒指向在线接口说明(免鉴权),新接入方第一次调用报 401 时即可自助查阅。
|
||||||
|
|
||||||
|
### 0. 接口说明(免鉴权)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/v1/docs
|
||||||
|
```
|
||||||
|
|
||||||
|
返回本文档的 Markdown 原文(`text/markdown`,gzip),免鉴权、可直接 `curl` 后喂给 AI 或转给调用方。仅公开接口相关章节,运维/架构不外泄。`/health`、`/stats` 的 `docs` 字段与所有错误响应的 `error.docs` 都指向这里。
|
||||||
|
|
||||||
### 1. 健康检查
|
### 1. 健康检查
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
+52
-5
@@ -787,11 +787,53 @@ function ok(res, data, meta, extraHeaders) {
|
|||||||
send(res, 200, { ok: true, data, ...(meta ? { meta } : {}) }, extraHeaders);
|
send(res, 200, { ok: true, data, ...(meta ? { meta } : {}) }, extraHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fail(res, status, code, message, extraHeaders) {
|
/* ---------- 对接文档(自动可发现:无 Key 也能读,错误响应里也带入口) ---------- */
|
||||||
send(res, status, { ok: false, error: { code, message } }, { 'Cache-Control': 'no-store', ...(extraHeaders || {}) });
|
const DOCS_URL = 'https://drawlogo.zhuig.com/api/v1/docs';
|
||||||
|
const DOCS_FILE = path.join(ROOT, 'README.md');
|
||||||
|
const DOCS_CUT = '\n## 八、运维'; // 运维/架构章节含服务器路径与部署命令,不对外公开
|
||||||
|
let docsCache = { mtime: -1, buf: null, gz: null };
|
||||||
|
|
||||||
|
/** 只公开「一~七」章(接口、检索、限流、最佳实践、示例、协议合规) */
|
||||||
|
function buildDocs() {
|
||||||
|
let md;
|
||||||
|
try {
|
||||||
|
md = fs.readFileSync(DOCS_FILE, 'utf8');
|
||||||
|
} catch (e) {
|
||||||
|
return Buffer.from('# 接口说明暂不可用\n\n请联系管理员获取 api/README.md。\n', 'utf8');
|
||||||
|
}
|
||||||
|
const cut = md.indexOf(DOCS_CUT);
|
||||||
|
if (cut > 0) md = md.slice(0, cut) + '\n> 运维与架构章节不对外公开。\n';
|
||||||
|
return Buffer.from(md, 'utf8');
|
||||||
}
|
}
|
||||||
|
|
||||||
const PUBLIC_PATHS = new Set(['/api/v1/health', '/api/v1/stats', '/api/v1/sources']);
|
function sendDocs(res) {
|
||||||
|
let mtime = 0;
|
||||||
|
try { mtime = fs.statSync(DOCS_FILE).mtimeMs; } catch (e) { /* 缺失时按空文档处理 */ }
|
||||||
|
if (!docsCache.buf || docsCache.mtime !== mtime) {
|
||||||
|
const buf = buildDocs();
|
||||||
|
docsCache = { mtime, buf, gz: buf.length > 1024 ? zlib.gzipSync(buf, { level: 6 }) : null };
|
||||||
|
}
|
||||||
|
const h = {
|
||||||
|
'Content-Type': 'text/markdown; charset=utf-8',
|
||||||
|
'Access-Control-Allow-Origin': CONFIG.corsOrigin,
|
||||||
|
'X-Content-Type-Options': 'nosniff',
|
||||||
|
'Cache-Control': 'public, max-age=300'
|
||||||
|
};
|
||||||
|
const ae = String((res.req && res.req.headers['accept-encoding']) || '');
|
||||||
|
if (docsCache.gz && /\bgzip\b/.test(ae)) {
|
||||||
|
res.writeHead(200, { ...h, 'Content-Encoding': 'gzip', Vary: 'Accept-Encoding', 'Content-Length': docsCache.gz.length });
|
||||||
|
return res.end(docsCache.gz);
|
||||||
|
}
|
||||||
|
res.writeHead(200, { ...h, 'Content-Length': docsCache.buf.length });
|
||||||
|
return res.end(docsCache.buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fail(res, status, code, message, extraHeaders) {
|
||||||
|
// 错误响应统一带文档入口:新接入方第一次调用(401)就能读到对接说明
|
||||||
|
send(res, status, { ok: false, error: { code, message, docs: DOCS_URL } }, { 'Cache-Control': 'no-store', ...(extraHeaders || {}) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const PUBLIC_PATHS = new Set(['/api/v1/health', '/api/v1/stats', '/api/v1/sources', '/api/v1/docs']);
|
||||||
|
|
||||||
async function handle(req, res) {
|
async function handle(req, res) {
|
||||||
const u = new URL(req.url, 'http://localhost');
|
const u = new URL(req.url, 'http://localhost');
|
||||||
@@ -841,8 +883,12 @@ async function handle(req, res) {
|
|||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
/* ---------- 基础 ---------- */
|
/* ---------- 基础 ---------- */
|
||||||
|
if (pathname === '/api/v1/docs') {
|
||||||
|
return sendDocs(res);
|
||||||
|
}
|
||||||
|
|
||||||
if (pathname === '/api/v1/health') {
|
if (pathname === '/api/v1/health') {
|
||||||
return ok(res, { status: 'up', version: '1.0.0', builtAt: store.catalog.builtAt, uptime: Math.round(process.uptime()) });
|
return ok(res, { status: 'up', version: '1.0.0', builtAt: store.catalog.builtAt, uptime: Math.round(process.uptime()), docs: DOCS_URL });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pathname === '/api/v1/stats') {
|
if (pathname === '/api/v1/stats') {
|
||||||
@@ -854,7 +900,8 @@ async function handle(req, res) {
|
|||||||
skills: store.skills
|
skills: store.skills
|
||||||
? { shared: store.skills.shared.total, poster: store.skills.poster.total, sticker: store.skills.sticker.total }
|
? { shared: store.skills.shared.total, poster: store.skills.poster.total, sticker: store.skills.sticker.total }
|
||||||
: null,
|
: null,
|
||||||
runtime: { rssMB: Math.round(mem.rss / 1048576), uptime: Math.round(process.uptime()) }
|
runtime: { rssMB: Math.round(mem.rss / 1048576), uptime: Math.round(process.uptime()) },
|
||||||
|
docs: DOCS_URL
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user