@kne/fastify-app-manager 0.1.28 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,77 @@ const yazl = require('yazl');
5
5
  const { createSseReply } = require('../utils/logStream');
6
6
  const { DEPLOY_STEPS } = require('../utils/deployProgress');
7
7
  const { formatInTz } = require('../utils/logTime');
8
+ const {
9
+ str,
10
+ num,
11
+ bool,
12
+ any,
13
+ strArray,
14
+ list,
15
+ obj,
16
+ success,
17
+ empty,
18
+ raw,
19
+ pageResult,
20
+ deployEvent,
21
+ deployRecordProps,
22
+ deployRecord,
23
+ app,
24
+ appDetail,
25
+ appListItem,
26
+ centerApp,
27
+ version,
28
+ logLine,
29
+ logFile,
30
+ loadSample,
31
+ cleanupResult,
32
+ exportResult
33
+ } = require('../schema/response');
34
+
35
+ const TAG_CENTER = '应用中心-前台';
36
+ const TAG_APP = '应用中心管理后台-应用';
37
+ const TAG_ENV = '应用中心管理后台-环境变量';
38
+ const TAG_OPEN_API = '应用中心管理后台-接口调试';
39
+ const TAG_VERSION = '应用中心管理后台-版本';
40
+ const TAG_DEPLOY = '应用中心管理后台-部署';
41
+ const TAG_PROCESS = '应用中心管理后台-进程';
42
+ const TAG_LOG = '应用中心管理后台-日志';
43
+ const TAG_LOAD = '应用中心管理后台-负载';
44
+
45
+ const APP_NAME = '应用 name';
46
+ const QUERY_TOKEN = '访问令牌;SSE、原生下载无法携带请求头时用它代替 Authorization: Bearer';
47
+ const RECORD_TYPE = '记录类型:history 调用记录,case 接口用例';
48
+
49
+ const entriesSchema = {
50
+ type: ['array', 'null'],
51
+ description: '应用入口;去掉 label 或 path 为空的项,path 补前导 / 并按 path 去重;传 null 清空',
52
+ items: {
53
+ type: 'object',
54
+ properties: {
55
+ label: { type: 'string', description: '入口名称' },
56
+ path: { type: 'string', description: '相对应用根的路径' },
57
+ showInCenter: { type: 'boolean', description: '是否在应用中心前台展示,不传视为展示' }
58
+ }
59
+ }
60
+ };
61
+
62
+ const appBodyProps = {
63
+ label: { type: 'string', description: '应用显示名称' },
64
+ domain: { type: ['string', 'null'], description: '绑定域名(Host),访问该域名时整站转发到应用;空字符串或 null 表示不绑定,不能与其他应用重复' },
65
+ icon: { type: 'string', description: '应用图标(文件 ID 或地址)' },
66
+ description: { type: 'string', description: '应用描述' },
67
+ category: { type: ['object', 'string', 'null'], description: '应用分类:分组记录(只保留 code、name)或分类编码;null 清空' },
68
+ isPublic: { type: 'boolean', description: '是否公开,公开的应用未登录也能在应用中心看到;不传视为公开' },
69
+ entries: entriesSchema,
70
+ env: { type: 'object', description: '应用环境变量;值为 ******** 的密钥变量保持原值,PORT 与宿主维护的系统变量会被忽略' },
71
+ pm2Config: { type: 'object', description: 'PM2 覆盖配置,与插件级 pm2Defaults 合并' },
72
+ options: { type: 'object', description: '扩展配置(如 secretEnvKeys 指定按密钥处理的变量名)' }
73
+ };
74
+
75
+ const pageQuery = {
76
+ perPage: { type: 'number', default: 20, description: '每页条数' },
77
+ currentPage: { type: 'number', default: 1, description: '页码,从 1 开始' }
78
+ };
8
79
 
9
80
  module.exports = fp(async (fastify, options) => {
10
81
  const { services } = fastify[options.name];
@@ -16,27 +87,15 @@ module.exports = fp(async (fastify, options) => {
16
87
  {
17
88
  onRequest: auth(),
18
89
  schema: {
90
+ tags: [TAG_APP],
19
91
  summary: '创建应用',
92
+ description: '登记应用并创建应用目录、分配端口,状态为 idle;之后上传版本并部署才能访问',
20
93
  body: {
21
94
  type: 'object',
22
95
  required: ['name', 'label'],
23
- properties: {
24
- name: { type: 'string' },
25
- label: { type: 'string' },
26
- domain: { type: ['string', 'null'] },
27
- icon: { type: 'string' },
28
- description: { type: 'string' },
29
- category: { type: ['object', 'string', 'null'] },
30
- isPublic: { type: 'boolean' },
31
- entries: {
32
- type: ['array', 'null'],
33
- items: { type: 'object', properties: { label: { type: 'string' }, path: { type: 'string' }, showInCenter: { type: 'boolean' } } }
34
- },
35
- env: { type: 'object' },
36
- pm2Config: { type: 'object' },
37
- options: { type: 'object' }
38
- }
39
- }
96
+ properties: Object.assign({ name: { type: 'string', description: '应用 name:小写字母、数字和 -,作为访问路径 /app/{name},创建后不可修改' } }, appBodyProps)
97
+ },
98
+ response: success('创建的应用', app())
40
99
  }
41
100
  },
42
101
  async request => services.app.create(request.body)
@@ -47,27 +106,15 @@ module.exports = fp(async (fastify, options) => {
47
106
  {
48
107
  onRequest: auth(),
49
108
  schema: {
109
+ tags: [TAG_APP],
50
110
  summary: '更新应用元信息',
111
+ description: '只更新传入的字段;name、端口、目录、状态等不可通过此接口修改。修改环境变量后需重启应用才生效',
51
112
  body: {
52
113
  type: 'object',
53
114
  required: ['name'],
54
- properties: {
55
- name: { type: 'string' },
56
- label: { type: 'string' },
57
- domain: { type: ['string', 'null'] },
58
- icon: { type: 'string' },
59
- description: { type: 'string' },
60
- category: { type: ['object', 'string', 'null'] },
61
- isPublic: { type: 'boolean' },
62
- entries: {
63
- type: ['array', 'null'],
64
- items: { type: 'object', properties: { label: { type: 'string' }, path: { type: 'string' }, showInCenter: { type: 'boolean' } } }
65
- },
66
- env: { type: 'object' },
67
- pm2Config: { type: 'object' },
68
- options: { type: 'object' }
69
- }
70
- }
115
+ properties: Object.assign({ name: { type: 'string', description: '要更新的应用 name' } }, appBodyProps)
116
+ },
117
+ response: success('更新后的应用', app())
71
118
  }
72
119
  },
73
120
  async request => services.app.save(request.body)
@@ -78,7 +125,10 @@ module.exports = fp(async (fastify, options) => {
78
125
  {
79
126
  onRequest: userAuth(),
80
127
  schema: {
81
- summary: '应用中心:运行中应用的公开信息(普通登录用户可访问)'
128
+ tags: [TAG_CENTER],
129
+ summary: '应用中心:运行中应用的公开信息(普通登录用户可访问)',
130
+ description: '返回所有运行中的应用(含未公开的),只包含展示用字段',
131
+ response: success('运行中的应用,按创建时间倒序', pageResult('应用', centerApp))
82
132
  }
83
133
  },
84
134
  async () => services.app.centerList()
@@ -88,7 +138,9 @@ module.exports = fp(async (fastify, options) => {
88
138
  `${options.prefix}/app/center/public-list`,
89
139
  {
90
140
  schema: {
91
- summary: '应用中心:运行中且公开的应用(无需登录)'
141
+ tags: [TAG_CENTER],
142
+ summary: '应用中心:运行中且公开的应用(无需登录)',
143
+ response: success('运行中且公开的应用,按创建时间倒序', pageResult('应用', centerApp))
92
144
  }
93
145
  },
94
146
  async () => services.app.centerList({ publicOnly: true })
@@ -98,15 +150,33 @@ module.exports = fp(async (fastify, options) => {
98
150
  `${options.prefix}/app/center/webmanifest`,
99
151
  {
100
152
  schema: {
153
+ tags: [TAG_CENTER],
101
154
  summary: '应用中心:公开应用的 PWA manifest(无需登录)',
155
+ description: '应用不存在、未运行或未公开时返回 404;Content-Type 为 application/manifest+json',
102
156
  querystring: {
103
157
  type: 'object',
104
158
  required: ['name'],
105
159
  properties: {
106
- name: { type: 'string' },
107
- path: { type: 'string' }
160
+ name: { type: 'string', description: APP_NAME },
161
+ path: { type: 'string', description: '应用入口地址;不传或不匹配任何入口时使用应用根路径' }
108
162
  }
109
- }
163
+ },
164
+ response: success(
165
+ 'Web App Manifest',
166
+ obj('Web App Manifest', {
167
+ id: str('manifest id,等于启动地址'),
168
+ name: str('应用名称(入口名称或应用显示名称)'),
169
+ short_name: str('短名称'),
170
+ description: str('应用描述'),
171
+ lang: str('语言'),
172
+ start_url: str('启动地址'),
173
+ scope: str('作用域,应用根路径'),
174
+ display: str('显示模式'),
175
+ theme_color: str('主题色'),
176
+ background_color: str('背景色'),
177
+ icons: list('图标', obj('图标', { src: str('图标地址'), sizes: str('尺寸'), type: str('MIME 类型') }))
178
+ })
179
+ )
110
180
  }
111
181
  },
112
182
  async (request, reply) => {
@@ -121,16 +191,19 @@ module.exports = fp(async (fastify, options) => {
121
191
  {
122
192
  onRequest: auth(),
123
193
  schema: {
194
+ tags: [TAG_ENV],
124
195
  summary: '合并更新应用环境变量',
196
+ description: '与现有变量合并:值为 null 删除该变量,值为 ******** 保持原密钥;PORT 与宿主维护的系统变量会被忽略。重启应用后生效',
125
197
  body: {
126
198
  type: 'object',
127
199
  required: ['name', 'env'],
128
200
  properties: {
129
- name: { type: 'string' },
130
- env: { type: 'object' },
131
- secretEnvKeys: { type: 'array', items: { type: 'string' } }
201
+ name: { type: 'string', description: APP_NAME },
202
+ env: { type: 'object', description: '要合并的环境变量 { 变量名: 值 }' },
203
+ secretEnvKeys: { type: 'array', items: { type: 'string' }, description: '追加为密钥的变量名(值在接口中掩码);已是密钥的变量不能撤销,只随变量删除而移除' }
132
204
  }
133
- }
205
+ },
206
+ response: success('更新后的应用', app())
134
207
  }
135
208
  },
136
209
  async request => services.app.saveEnv(request.body)
@@ -141,15 +214,42 @@ module.exports = fp(async (fastify, options) => {
141
214
  {
142
215
  onRequest: auth(),
143
216
  schema: {
217
+ tags: [TAG_ENV],
144
218
  summary: '环境变量声明:按版本 env.schema.json 列出需要填写的变量(不含应用中心注入的变量)',
145
219
  query: {
146
220
  type: 'object',
147
221
  required: ['name'],
148
222
  properties: {
149
- name: { type: 'string' },
223
+ name: { type: 'string', description: APP_NAME },
150
224
  versionId: { type: 'string', description: '默认取当前版本,没有则取最新上传的版本' }
151
225
  }
152
- }
226
+ },
227
+ response: success(
228
+ '环境变量声明与当前配置;密钥变量值掩码',
229
+ obj('环境变量声明', {
230
+ name: str('应用 name'),
231
+ versionId: any('声明所属版本 ID,没有版本为 null'),
232
+ version: str('声明所属版本号'),
233
+ hasSchema: bool('该版本是否有 env.schema.json'),
234
+ valid: bool('当前配置是否满足声明'),
235
+ declared: list(
236
+ 'schema 中声明的变量',
237
+ obj('声明的变量', {
238
+ key: str('变量名'),
239
+ type: any('声明的类型'),
240
+ required: bool('是否必填'),
241
+ default: any('默认值,没有为 null'),
242
+ description: str('说明'),
243
+ enum: any('可选值,没有为 null'),
244
+ secret: bool('是否为密钥(值掩码)'),
245
+ configured: bool('是否已配置'),
246
+ value: any('当前值,未配置为 null,密钥为 ********'),
247
+ error: str('校验不通过的原因,通过为 null')
248
+ })
249
+ ),
250
+ others: list('已配置但未在 schema 中声明的变量', obj('未声明的变量', { key: str('变量名'), secret: bool('是否为密钥'), value: any('当前值,密钥为 ********') }))
251
+ })
252
+ )
153
253
  }
154
254
  },
155
255
  async request => services.app.getEnvDeclaration(request.query)
@@ -160,14 +260,17 @@ module.exports = fp(async (fastify, options) => {
160
260
  {
161
261
  onRequest: auth(),
162
262
  schema: {
263
+ tags: [TAG_OPEN_API],
163
264
  summary: '运行中应用的 OpenAPI 文档(子应用 /open-api.json 原文,需配置 getOpenApiToken)',
265
+ description: '以服务令牌读取子应用的 /open-api.json 并原样返回;应用未运行或未开启文档时返回 400',
164
266
  query: {
165
267
  type: 'object',
166
268
  required: ['name'],
167
269
  properties: {
168
- name: { type: 'string' }
270
+ name: { type: 'string', description: APP_NAME }
169
271
  }
170
- }
272
+ },
273
+ response: success('子应用的 OpenAPI 文档原文')
171
274
  }
172
275
  },
173
276
  async request => services.openApi.getDocument(request.query)
@@ -178,15 +281,23 @@ module.exports = fp(async (fastify, options) => {
178
281
  {
179
282
  onRequest: auth(),
180
283
  schema: {
284
+ tags: [TAG_OPEN_API],
181
285
  summary: '以当前登录用户身份签发访问应用接口的令牌,供在线调试(需配置 issueUserAccessToken)',
182
286
  body: {
183
287
  type: 'object',
184
288
  required: ['name'],
185
289
  properties: {
186
- name: { type: 'string' },
187
- tenantId: { type: 'string' }
290
+ name: { type: 'string', description: APP_NAME },
291
+ tenantId: { type: 'string', description: '以该租户身份签发,透传给 issueUserAccessToken' }
188
292
  }
189
- }
293
+ },
294
+ response: success(
295
+ '签发的令牌',
296
+ obj('令牌', {
297
+ token: str('访问子应用接口的令牌'),
298
+ expiresAt: any('过期时间,令牌不含 exp 时为 null')
299
+ })
300
+ )
190
301
  }
191
302
  },
192
303
  async request => services.openApi.issueUserToken(Object.assign({}, request.body, { userId: request.userInfo?.id }))
@@ -197,19 +308,21 @@ module.exports = fp(async (fastify, options) => {
197
308
  {
198
309
  onRequest: auth(),
199
310
  schema: {
311
+ tags: [TAG_OPEN_API],
200
312
  summary: '应用接口调试记录或用例(按应用共享;调用记录倒序、最多保留 1000 条;传 limit 时按 offset 分页)',
201
313
  query: {
202
314
  type: 'object',
203
315
  required: ['name', 'type'],
204
316
  properties: {
205
- name: { type: 'string' },
206
- type: { type: 'string', enum: ['history', 'case'] },
317
+ name: { type: 'string', description: APP_NAME },
318
+ type: { type: 'string', enum: ['history', 'case'], description: RECORD_TYPE },
207
319
  operationKey: { type: 'string', description: '只取该接口的记录' },
208
320
  keyword: { type: 'string', description: '按方法、地址、标题、用例名模糊搜索(不区分大小写)' },
209
- offset: { type: 'integer', minimum: 0, default: 0 },
210
- limit: { type: 'integer', minimum: 1, maximum: 100 }
321
+ offset: { type: 'integer', minimum: 0, default: 0, description: '跳过的条数,传 limit 时生效' },
322
+ limit: { type: 'integer', minimum: 1, maximum: 100, description: '每次返回的条数,不传返回全部' }
211
323
  }
212
- }
324
+ },
325
+ response: success('记录列表;调用记录按时间倒序,用例按时间正序', pageResult('记录(保存时的原文)', any('记录原文')))
213
326
  }
214
327
  },
215
328
  async request => services.openApi.listRecords(request.query)
@@ -220,15 +333,17 @@ module.exports = fp(async (fastify, options) => {
220
333
  {
221
334
  onRequest: auth(),
222
335
  schema: {
336
+ tags: [TAG_OPEN_API],
223
337
  summary: '按接口统计应用接口调试记录 / 用例数量,返回 { [operationKey]: count }',
224
338
  query: {
225
339
  type: 'object',
226
340
  required: ['name', 'type'],
227
341
  properties: {
228
- name: { type: 'string' },
229
- type: { type: 'string', enum: ['history', 'case'] }
342
+ name: { type: 'string', description: APP_NAME },
343
+ type: { type: 'string', enum: ['history', 'case'], description: RECORD_TYPE }
230
344
  }
231
- }
345
+ },
346
+ response: success('{ [operationKey]: 记录数 },没有 operationKey 的记录计在空字符串键下')
232
347
  }
233
348
  },
234
349
  async request => services.openApi.countRecords(request.query)
@@ -239,16 +354,36 @@ module.exports = fp(async (fastify, options) => {
239
354
  {
240
355
  onRequest: auth(),
241
356
  schema: {
357
+ tags: [TAG_OPEN_API],
242
358
  summary: '新增或覆盖应用接口调试记录 / 用例(按 records[].id 覆盖)',
359
+ description: '记录原文整体保存;保存调用记录后只保留最新 1000 条',
243
360
  body: {
244
361
  type: 'object',
245
362
  required: ['name', 'type', 'records'],
246
363
  properties: {
247
- name: { type: 'string' },
248
- type: { type: 'string', enum: ['history', 'case'] },
249
- records: { type: 'array', items: { type: 'object', required: ['id'], additionalProperties: true } }
364
+ name: { type: 'string', description: APP_NAME },
365
+ type: { type: 'string', enum: ['history', 'case'], description: RECORD_TYPE },
366
+ records: {
367
+ type: 'array',
368
+ description: '记录列表,每条原样保存',
369
+ items: {
370
+ type: 'object',
371
+ required: ['id'],
372
+ additionalProperties: true,
373
+ properties: {
374
+ id: { description: '记录 ID(前端生成),同 ID 覆盖' },
375
+ operationKey: { description: '所属接口 key(method + path)' },
376
+ method: { description: '请求方法,参与关键字搜索' },
377
+ url: { description: '请求地址,参与关键字搜索' },
378
+ path: { description: '接口路径,没有 url 时参与关键字搜索' },
379
+ summary: { description: '接口标题,参与关键字搜索' },
380
+ name: { description: '用例名,参与关键字搜索' }
381
+ }
382
+ }
383
+ }
250
384
  }
251
- }
385
+ },
386
+ response: empty('保存成功,返回空对象')
252
387
  }
253
388
  },
254
389
  async request => services.openApi.saveRecords(Object.assign({}, request.body, { userId: request.userInfo?.id }))
@@ -259,17 +394,19 @@ module.exports = fp(async (fastify, options) => {
259
394
  {
260
395
  onRequest: auth(),
261
396
  schema: {
397
+ tags: [TAG_OPEN_API],
262
398
  summary: '删除应用接口调试记录 / 用例(all=true 时清空该类型全部记录)',
263
399
  body: {
264
400
  type: 'object',
265
401
  required: ['name', 'type'],
266
402
  properties: {
267
- name: { type: 'string' },
268
- type: { type: 'string', enum: ['history', 'case'] },
269
- ids: { type: 'array', items: { type: 'string' } },
270
- all: { type: 'boolean' }
403
+ name: { type: 'string', description: APP_NAME },
404
+ type: { type: 'string', enum: ['history', 'case'], description: RECORD_TYPE },
405
+ ids: { type: 'array', items: { type: 'string' }, description: '要删除的记录 ID' },
406
+ all: { type: 'boolean', description: '为 true 时忽略 ids,清空该类型全部记录' }
271
407
  }
272
- }
408
+ },
409
+ response: empty('删除成功,返回空对象')
273
410
  }
274
411
  },
275
412
  async request => services.openApi.removeRecords(request.body)
@@ -280,7 +417,11 @@ module.exports = fp(async (fastify, options) => {
280
417
  {
281
418
  onRequest: auth(),
282
419
  schema: {
283
- summary: '上传应用版本 zip(multipart 传 file,或 JSON 传宿主文件 fileId)'
420
+ tags: [TAG_VERSION],
421
+ summary: '上传应用版本 zip(multipart 传 file,或 JSON 传宿主文件 fileId)',
422
+ description:
423
+ '请求体字段:name 应用 name(必填);version 版本号(必填,同一应用不能重复);label 版本说明;file 部署包 zip(multipart)或 fileId 宿主文件 ID(JSON 或 multipart 字段,需配置 resolveVersionFile)。部署包需包含 server/ 与 server/env.schema.json,校验并安装依赖后登记版本',
424
+ response: success('上传的版本', version)
284
425
  }
285
426
  },
286
427
  async request => {
@@ -318,16 +459,14 @@ module.exports = fp(async (fastify, options) => {
318
459
  {
319
460
  onRequest: auth(),
320
461
  schema: {
462
+ tags: [TAG_VERSION],
321
463
  summary: '应用版本列表',
322
464
  query: {
323
465
  type: 'object',
324
466
  required: ['name'],
325
- properties: {
326
- name: { type: 'string' },
327
- perPage: { type: 'number', default: 20 },
328
- currentPage: { type: 'number', default: 1 }
329
- }
330
- }
467
+ properties: Object.assign({ name: { type: 'string', description: APP_NAME } }, pageQuery)
468
+ },
469
+ response: success('版本列表,按上传时间倒序', pageResult('版本', version))
331
470
  }
332
471
  },
333
472
  async request => services.app.listVersions(request.query)
@@ -338,15 +477,18 @@ module.exports = fp(async (fastify, options) => {
338
477
  {
339
478
  onRequest: auth(),
340
479
  schema: {
480
+ tags: [TAG_VERSION],
341
481
  summary: '下载版本部署包(有 fileId 返回原始 zip,否则由版本目录打包)',
342
482
  query: {
343
483
  type: 'object',
344
484
  required: ['name', 'versionId'],
345
485
  properties: {
346
- name: { type: 'string' },
347
- versionId: { type: 'string' }
486
+ name: { type: 'string', description: APP_NAME },
487
+ versionId: { type: 'string', description: '版本 ID' },
488
+ token: { type: 'string', description: QUERY_TOKEN }
348
489
  }
349
- }
490
+ },
491
+ response: raw('zip 文件流(application/zip,附件名 {name}-{version}.zip);由版本目录打包时不含 server/node_modules 与 server/build')
350
492
  }
351
493
  },
352
494
  async (request, reply) => {
@@ -371,15 +513,17 @@ module.exports = fp(async (fastify, options) => {
371
513
  {
372
514
  onRequest: auth(),
373
515
  schema: {
516
+ tags: [TAG_DEPLOY],
374
517
  summary: '部署指定版本',
518
+ description: '准备部署包、校验环境变量、按需执行 SQL 迁移并启动进程;启动后异步等待就绪,进度通过 deploy/stream 订阅。同一应用同时只能有一个部署',
375
519
  body: {
376
520
  type: 'object',
377
521
  required: ['name'],
378
522
  properties: {
379
- name: { type: 'string' },
380
- versionId: { type: 'string' },
381
- version: { type: 'string' },
382
- runMigration: { type: 'boolean', default: true },
523
+ name: { type: 'string', description: APP_NAME },
524
+ versionId: { type: 'string', description: '要部署的版本 ID;与 version 二选一,优先 versionId' },
525
+ version: { type: 'string', description: '要部署的版本号' },
526
+ runMigration: { type: 'boolean', default: true, description: '是否执行 SQL 迁移(需同时开启插件 migrateBeforeStart);为 false 时子进程收到 RUN_SQL_ON_SYNC=false' },
383
527
  progressId: { type: 'string', description: '进度标识;先用它订阅 deploy/stream 再发起部署' },
384
528
  migrations: {
385
529
  type: 'array',
@@ -388,13 +532,25 @@ module.exports = fp(async (fastify, options) => {
388
532
  type: 'object',
389
533
  required: ['name', 'action'],
390
534
  properties: {
391
- name: { type: 'string' },
392
- action: { type: 'string', enum: ['execute', 'skip', 'hold'] }
535
+ name: { type: 'string', description: '迁移脚本文件名' },
536
+ action: { type: 'string', enum: ['execute', 'skip', 'hold'], description: '处理方式:execute 执行,skip 跳过并标记已执行,hold 暂不执行' }
393
537
  }
394
538
  }
395
539
  }
396
540
  }
397
- }
541
+ },
542
+ response: success(
543
+ '部署已启动,应用状态为 deploying',
544
+ obj('部署结果', {
545
+ name: str('应用 name'),
546
+ status: str('应用状态,固定为 deploying'),
547
+ versionId: any('部署的版本 ID'),
548
+ progressId: str('部署进度标识'),
549
+ port: num('应用端口'),
550
+ pathUrl: str('访问地址,如 /app/{name}/'),
551
+ domain: str('绑定域名,未绑定为 null')
552
+ })
553
+ )
398
554
  }
399
555
  },
400
556
  async request => services.app.deploy(request.body)
@@ -405,15 +561,19 @@ module.exports = fp(async (fastify, options) => {
405
561
  {
406
562
  onRequest: auth(),
407
563
  schema: {
564
+ tags: [TAG_DEPLOY],
408
565
  summary: 'SSE 部署进度',
566
+ description: '先推送 snapshot 事件({ steps, events, deploy },可用于断线重连回放),之后推送 progress(阶段事件)与 log({ progressId, line })事件,连接保持到客户端断开',
409
567
  query: {
410
568
  type: 'object',
411
569
  required: ['name'],
412
570
  properties: {
413
- name: { type: 'string' },
414
- progressId: { type: 'string' }
571
+ name: { type: 'string', description: APP_NAME },
572
+ progressId: { type: 'string', description: '只推送该次部署的事件;不传推送该应用的所有部署' },
573
+ token: { type: 'string', description: QUERY_TOKEN }
415
574
  }
416
- }
575
+ },
576
+ response: raw('text/event-stream 事件流')
417
577
  }
418
578
  },
419
579
  async (request, reply) => {
@@ -455,16 +615,14 @@ module.exports = fp(async (fastify, options) => {
455
615
  {
456
616
  onRequest: auth(),
457
617
  schema: {
618
+ tags: [TAG_DEPLOY],
458
619
  summary: '部署记录列表(不含日志)',
459
620
  query: {
460
621
  type: 'object',
461
622
  required: ['name'],
462
- properties: {
463
- name: { type: 'string' },
464
- perPage: { type: 'number', default: 20 },
465
- currentPage: { type: 'number', default: 1 }
466
- }
467
- }
623
+ properties: Object.assign({ name: { type: 'string', description: APP_NAME } }, pageQuery)
624
+ },
625
+ response: success('部署记录,按开始时间倒序;每个应用只保留最近若干条', pageResult('部署记录', deployRecord))
468
626
  }
469
627
  },
470
628
  async request => services.app.deployList(request.query)
@@ -475,16 +633,28 @@ module.exports = fp(async (fastify, options) => {
475
633
  {
476
634
  onRequest: auth(),
477
635
  schema: {
636
+ tags: [TAG_DEPLOY],
478
637
  summary: '部署记录详情(含阶段事件与部署日志);progressId 为空取最近一次',
479
638
  query: {
480
639
  type: 'object',
481
640
  required: ['name'],
482
641
  properties: {
483
- name: { type: 'string' },
484
- progressId: { type: 'string' },
642
+ name: { type: 'string', description: APP_NAME },
643
+ progressId: { type: 'string', description: '部署进度标识,不传取最近一次' },
485
644
  logLines: { type: 'number', default: 0, description: '只返回最后 N 行日志;0 返回全部' }
486
645
  }
487
- }
646
+ },
647
+ response: success(
648
+ '部署记录详情;进行中的部署以内存中的最新进度为准',
649
+ obj(
650
+ '部署记录',
651
+ Object.assign({}, deployRecordProps, {
652
+ events: list('阶段事件', deployEvent),
653
+ logTotalLines: num('日志总行数'),
654
+ log: str('部署日志(按 logLines 截取的最后若干行,换行分隔)')
655
+ })
656
+ )
657
+ )
488
658
  }
489
659
  },
490
660
  async request => services.app.deployDetail(request.query)
@@ -495,15 +665,26 @@ module.exports = fp(async (fastify, options) => {
495
665
  {
496
666
  onRequest: auth(),
497
667
  schema: {
668
+ tags: [TAG_APP],
498
669
  summary: '应用列表',
499
670
  query: {
500
671
  type: 'object',
501
- properties: {
502
- filter: { type: 'object', default: {} },
503
- perPage: { type: 'number', default: 20 },
504
- currentPage: { type: 'number', default: 1 }
505
- }
506
- }
672
+ properties: Object.assign(
673
+ {
674
+ filter: {
675
+ type: 'object',
676
+ default: {},
677
+ description: '筛选条件',
678
+ properties: {
679
+ status: { description: '按应用状态精确筛选' },
680
+ keyword: { description: '按 name 或显示名称模糊搜索' }
681
+ }
682
+ }
683
+ },
684
+ pageQuery
685
+ )
686
+ },
687
+ response: success('应用列表,按创建时间倒序', pageResult('应用', appListItem))
507
688
  }
508
689
  },
509
690
  async request => services.app.list(request.query)
@@ -514,31 +695,41 @@ module.exports = fp(async (fastify, options) => {
514
695
  {
515
696
  onRequest: auth(),
516
697
  schema: {
698
+ tags: [TAG_APP],
517
699
  summary: '应用详情',
518
700
  query: {
519
701
  type: 'object',
520
702
  required: ['name'],
521
703
  properties: {
522
- name: { type: 'string' }
704
+ name: { type: 'string', description: APP_NAME }
523
705
  }
524
- }
706
+ },
707
+ response: success('应用详情,含进行中的部署与最近一次部署记录', appDetail)
525
708
  }
526
709
  },
527
710
  async request => services.app.detail(request.query)
528
711
  );
529
712
 
713
+ const processActions = {
714
+ start: { summary: 'start 应用', description: '按当前版本启动进程(启动前校验环境变量,不通过直接报错);就绪检测异步进行,状态先置为 deploying' },
715
+ stop: { summary: 'stop 应用', description: '停止 PM2 进程,状态置为 stopped' },
716
+ restart: { summary: 'restart 应用', description: '停止后按最新环境变量重新启动进程(启动前校验环境变量);就绪检测异步进行,状态先置为 deploying' }
717
+ };
530
718
  for (const action of ['start', 'stop', 'restart']) {
531
719
  fastify.post(
532
720
  `${options.prefix}/app/${action}`,
533
721
  {
534
722
  onRequest: auth(),
535
723
  schema: {
536
- summary: `${action} 应用`,
724
+ tags: [TAG_PROCESS],
725
+ summary: processActions[action].summary,
726
+ description: processActions[action].description,
537
727
  body: {
538
728
  type: 'object',
539
729
  required: ['name'],
540
- properties: { name: { type: 'string' } }
541
- }
730
+ properties: { name: { type: 'string', description: APP_NAME } }
731
+ },
732
+ response: success('操作后的应用', app())
542
733
  }
543
734
  },
544
735
  async request => services.app[action](request.body)
@@ -550,19 +741,30 @@ module.exports = fp(async (fastify, options) => {
550
741
  {
551
742
  onRequest: auth(),
552
743
  schema: {
744
+ tags: [TAG_APP],
553
745
  summary: '删除应用(默认可先导出并清理所属表)',
746
+ description: '删除 PM2 进程、版本、调试记录与应用目录;清理所属表不完整且未允许时不删除应用,返回 removed=false 与需手工执行的 SQL',
554
747
  body: {
555
748
  type: 'object',
556
749
  required: ['name'],
557
750
  properties: {
558
- name: { type: 'string' },
559
- exportBeforeRemove: { type: 'boolean', default: true },
560
- cleanupData: { type: 'boolean', default: true },
561
- allowRemoveIfCleanupIncomplete: { type: 'boolean', default: false },
562
- force: { type: 'boolean', default: false },
563
- removeSqliteFile: { type: 'boolean', default: false }
751
+ name: { type: 'string', description: APP_NAME },
752
+ exportBeforeRemove: { type: 'boolean', default: true, description: '删除前导出所属表数据(force 为 true 时不导出)' },
753
+ cleanupData: { type: 'boolean', default: true, description: '是否删除应用所属的数据表' },
754
+ allowRemoveIfCleanupIncomplete: { type: 'boolean', default: false, description: '清理数据表不完整时仍删除应用' },
755
+ force: { type: 'boolean', default: false, description: '跳过删除前导出' },
756
+ removeSqliteFile: { type: 'boolean', default: false, description: '独享 sqlite 库时同时删除数据库文件' }
564
757
  }
565
- }
758
+ },
759
+ response: success(
760
+ '删除结果',
761
+ obj('删除结果', {
762
+ removed: bool('应用是否已删除'),
763
+ reason: str('未删除的原因'),
764
+ export: exportResult,
765
+ cleanup: cleanupResult
766
+ })
767
+ )
566
768
  }
567
769
  },
568
770
  async request => services.app.remove(request.body)
@@ -573,15 +775,17 @@ module.exports = fp(async (fastify, options) => {
573
775
  {
574
776
  onRequest: auth(),
575
777
  schema: {
778
+ tags: [TAG_LOG],
576
779
  summary: '读取应用日志文件',
780
+ description: '从文件末尾倒序分页读取,第 1 页为最新的日志',
577
781
  query: {
578
782
  type: 'object',
579
783
  required: ['name'],
580
784
  properties: {
581
- name: { type: 'string' },
582
- stream: { type: 'string', enum: ['out', 'err'], default: 'out' },
583
- perPage: { type: 'number', default: 100, maximum: 100 },
584
- currentPage: { type: 'number', default: 1 },
785
+ name: { type: 'string', description: APP_NAME },
786
+ stream: { type: 'string', enum: ['out', 'err'], default: 'out', description: '输出流:out 标准输出,err 错误输出' },
787
+ perPage: { type: 'number', default: 100, maximum: 100, description: '每页行数,最多 100' },
788
+ currentPage: { type: 'number', default: 1, description: '页码,从 1 开始,第 1 页为最新' },
585
789
  beforeLine: {
586
790
  type: 'number',
587
791
  description: 'Load up to perPage lines with line number < beforeLine (for scroll-up history)'
@@ -591,7 +795,15 @@ module.exports = fp(async (fastify, options) => {
591
795
  description: 'Archive file name from /app/logs/files; overrides stream'
592
796
  }
593
797
  }
594
- }
798
+ },
799
+ response: success(
800
+ '日志行(新的在前)',
801
+ obj('日志分页', {
802
+ pageData: list('日志行,行号倒序', logLine),
803
+ totalCount: num('文件总行数'),
804
+ hasMore: bool('是否还有更早的日志')
805
+ })
806
+ )
595
807
  }
596
808
  },
597
809
  async request => services.app.logs(request.query)
@@ -602,14 +814,16 @@ module.exports = fp(async (fastify, options) => {
602
814
  {
603
815
  onRequest: auth(),
604
816
  schema: {
817
+ tags: [TAG_LOG],
605
818
  summary: '列出应用日志文件(当前文件与归档)',
606
819
  query: {
607
820
  type: 'object',
608
821
  required: ['name'],
609
822
  properties: {
610
- name: { type: 'string' }
823
+ name: { type: 'string', description: APP_NAME }
611
824
  }
612
- }
825
+ },
826
+ response: success('日志文件列表', pageResult('日志文件', logFile))
613
827
  }
614
828
  },
615
829
  async request => services.app.logFiles(request.query)
@@ -620,15 +834,18 @@ module.exports = fp(async (fastify, options) => {
620
834
  {
621
835
  onRequest: auth(),
622
836
  schema: {
837
+ tags: [TAG_LOG],
623
838
  summary: '下载日志文件',
624
839
  query: {
625
840
  type: 'object',
626
841
  required: ['name', 'file'],
627
842
  properties: {
628
- name: { type: 'string' },
629
- file: { type: 'string' }
843
+ name: { type: 'string', description: APP_NAME },
844
+ file: { type: 'string', description: '日志文件名(来自 logs/files)' },
845
+ token: { type: 'string', description: QUERY_TOKEN }
630
846
  }
631
- }
847
+ },
848
+ response: raw('日志文件流:.gz 归档为 application/gzip,其余为 text/plain;当前日志只读到请求时的长度')
632
849
  }
633
850
  },
634
851
  async (request, reply) => {
@@ -650,15 +867,18 @@ module.exports = fp(async (fastify, options) => {
650
867
  {
651
868
  onRequest: auth(),
652
869
  schema: {
870
+ tags: [TAG_LOG],
653
871
  summary: '批量打包下载日志文件',
654
872
  query: {
655
873
  type: 'object',
656
874
  required: ['name', 'files'],
657
875
  properties: {
658
- name: { type: 'string' },
659
- files: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 100 }
876
+ name: { type: 'string', description: APP_NAME },
877
+ files: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 100, description: '日志文件名(来自 logs/files),重复的会去重' },
878
+ token: { type: 'string', description: QUERY_TOKEN }
660
879
  }
661
- }
880
+ },
881
+ response: raw('zip 文件流(application/zip,附件名 {name}-logs-{时间}.zip)')
662
882
  }
663
883
  },
664
884
  async (request, reply) => {
@@ -685,15 +905,18 @@ module.exports = fp(async (fastify, options) => {
685
905
  {
686
906
  onRequest: auth(),
687
907
  schema: {
908
+ tags: [TAG_LOG],
688
909
  summary: '删除选中的日志归档',
910
+ description: '只能删除归档,当前日志文件(out.log、err.log)不可删除',
689
911
  body: {
690
912
  type: 'object',
691
913
  required: ['name', 'files'],
692
914
  properties: {
693
- name: { type: 'string' },
694
- files: { type: 'array', minItems: 1, items: { type: 'string' } }
915
+ name: { type: 'string', description: APP_NAME },
916
+ files: { type: 'array', minItems: 1, items: { type: 'string' }, description: '要删除的归档文件名' }
695
917
  }
696
- }
918
+ },
919
+ response: success('删除结果', obj('删除结果', { removed: strArray('实际删除的文件名(不存在的会跳过)') }))
697
920
  }
698
921
  },
699
922
  async request => services.app.removeLogFiles(request.body)
@@ -704,15 +927,19 @@ module.exports = fp(async (fastify, options) => {
704
927
  {
705
928
  onRequest: auth(),
706
929
  schema: {
930
+ tags: [TAG_LOG],
707
931
  summary: 'SSE 实时日志',
932
+ description: '先回放最近若干行,再实时推送 log 事件({ appName, stream, content, line, loggedAt }),连接保持到客户端断开',
708
933
  query: {
709
934
  type: 'object',
710
935
  required: ['name'],
711
936
  properties: {
712
- name: { type: 'string' },
713
- stream: { type: 'string', enum: ['out', 'err'] }
937
+ name: { type: 'string', description: APP_NAME },
938
+ stream: { type: 'string', enum: ['out', 'err'], description: '只推送该输出流;不传时回放 out 并推送全部' },
939
+ token: { type: 'string', description: QUERY_TOKEN }
714
940
  }
715
- }
941
+ },
942
+ response: raw('text/event-stream 事件流')
716
943
  }
717
944
  },
718
945
  async (request, reply) => {
@@ -754,14 +981,23 @@ module.exports = fp(async (fastify, options) => {
754
981
  {
755
982
  onRequest: auth(),
756
983
  schema: {
984
+ tags: [TAG_LOAD],
757
985
  summary: '应用当前负载与最近历史',
758
986
  query: {
759
987
  type: 'object',
760
988
  required: ['name'],
761
989
  properties: {
762
- name: { type: 'string' }
990
+ name: { type: 'string', description: APP_NAME }
763
991
  }
764
- }
992
+ },
993
+ response: success(
994
+ '负载数据',
995
+ obj('负载', {
996
+ intervalMs: num('采样间隔(毫秒),0 表示未开启采样'),
997
+ current: loadSample,
998
+ pageData: list('最近的采样历史,按时间正序', loadSample)
999
+ })
1000
+ )
765
1001
  }
766
1002
  },
767
1003
  async request => services.app.load(request.query)
@@ -772,14 +1008,18 @@ module.exports = fp(async (fastify, options) => {
772
1008
  {
773
1009
  onRequest: auth(),
774
1010
  schema: {
1011
+ tags: [TAG_LOAD],
775
1012
  summary: 'SSE 实时负载',
1013
+ description: '先推送 history 事件({ intervalMs, pageData }),之后每次采样推送 load 事件,连接保持到客户端断开',
776
1014
  query: {
777
1015
  type: 'object',
778
1016
  required: ['name'],
779
1017
  properties: {
780
- name: { type: 'string' }
1018
+ name: { type: 'string', description: APP_NAME },
1019
+ token: { type: 'string', description: QUERY_TOKEN }
781
1020
  }
782
- }
1021
+ },
1022
+ response: raw('text/event-stream 事件流')
783
1023
  }
784
1024
  },
785
1025
  async (request, reply) => {