@fastcar/cli 0.1.2 → 0.1.3
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.
- package/bin/cli.js +9 -0
- package/package.json +1 -1
- package/skills/fastcar-database/SKILL.md +260 -337
- package/skills/fastcar-framework/SKILL.md +75 -355
- package/skills/fastcar-rpc-microservices/SKILL.md +19 -69
- package/skills/fastcar-serverless/SKILL.md +48 -48
- package/skills/fastcar-toolkit/SKILL.md +22 -31
- package/skills/typescript-coding-style/SKILL.md +144 -0
- package/src/pack.js +7 -7
- package/src/update.js +301 -0
- package/src/utils.js +2 -2
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fastcar-framework
|
|
3
|
-
description: FastCar 是一个基于 TypeScript 的 Node.js 企业级应用开发框架,采用 IoC
|
|
3
|
+
description: FastCar 是一个基于 TypeScript 的 Node.js 企业级应用开发框架,采用 IoC(控制反转)设计思想。Use when working with FastCar framework for: (1) Creating IoC-based Node.js applications, (2) Using dependency injection with decorators (@Component, @Service, @Autowired), (3) Building web APIs with @fastcar/koa, (4) Database operations with MySQL/MongoDB/Redis, (5) Setting up scheduled tasks or worker pools, (6) Managing application lifecycle and configuration.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# FastCar Framework
|
|
7
7
|
|
|
8
|
-
FastCar 是基于 TypeScript 的 Node.js
|
|
8
|
+
FastCar 是基于 TypeScript 的 Node.js 企业级应用开发框架,采用 IoC(控制反转)设计思想。
|
|
9
9
|
|
|
10
10
|
## 核心概念
|
|
11
11
|
|
|
@@ -15,37 +15,34 @@ FastCar 是基于 TypeScript 的 Node.js 企业级应用开发框架,灵感来
|
|
|
15
15
|
|--------|------|------|
|
|
16
16
|
| `@Application` | 入口应用类 | `@Application class App {}` |
|
|
17
17
|
| `@Component` | 通用组件 | `@Component class UtilService {}` |
|
|
18
|
-
| `@Service` | 服务层 | `@Service class
|
|
19
|
-
| `@Controller` | 控制器层 | `@Controller class
|
|
20
|
-
| `@Repository` | 数据访问层 | `@Repository class
|
|
21
|
-
| `@Autowired` | 依赖注入 | `@Autowired private
|
|
18
|
+
| `@Service` | 服务层 | `@Service class BizService {}` |
|
|
19
|
+
| `@Controller` | 控制器层 | `@Controller class ApiController {}` |
|
|
20
|
+
| `@Repository` | 数据访问层 | `@Repository class DataRepository {}` |
|
|
21
|
+
| `@Autowired` | 依赖注入 | `@Autowired private service!: BizService;` |
|
|
22
22
|
|
|
23
23
|
### 基础应用结构
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
26
|
import { FastCarApplication } from "@fastcar/core";
|
|
27
|
-
import { Application, Autowired, Component, Service } from "@fastcar/core/annotation";
|
|
27
|
+
import { Application, Autowired, Component, Service, Controller } from "@fastcar/core/annotation";
|
|
28
28
|
|
|
29
|
-
// 服务层
|
|
30
29
|
@Service
|
|
31
|
-
class
|
|
32
|
-
|
|
33
|
-
return [{ id: 1, name: "
|
|
30
|
+
class BizService {
|
|
31
|
+
getData() {
|
|
32
|
+
return [{ id: 1, name: "示例" }];
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
// 控制器层
|
|
38
36
|
@Controller
|
|
39
|
-
class
|
|
37
|
+
class ApiController {
|
|
40
38
|
@Autowired
|
|
41
|
-
private
|
|
39
|
+
private service!: BizService;
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
return this.
|
|
41
|
+
getData() {
|
|
42
|
+
return this.service.getData();
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
|
|
48
|
-
// 应用入口
|
|
49
46
|
@Application
|
|
50
47
|
class App {
|
|
51
48
|
app!: FastCarApplication;
|
|
@@ -55,7 +52,6 @@ class App {
|
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
// 启动
|
|
59
55
|
const app = new App();
|
|
60
56
|
app.start();
|
|
61
57
|
```
|
|
@@ -64,14 +60,14 @@ app.start();
|
|
|
64
60
|
|
|
65
61
|
### Web 开发 (@fastcar/koa)
|
|
66
62
|
|
|
67
|
-
|
|
63
|
+
**路由装饰器使用方式:**
|
|
68
64
|
|
|
69
65
|
```typescript
|
|
70
66
|
import { GET, POST, REQUEST } from "@fastcar/koa/annotation";
|
|
71
67
|
|
|
72
68
|
@Controller
|
|
73
|
-
@REQUEST("/api/
|
|
74
|
-
class
|
|
69
|
+
@REQUEST("/api/items")
|
|
70
|
+
class ItemController {
|
|
75
71
|
// GET 请求 - 无路径参数时必须有括号
|
|
76
72
|
@GET()
|
|
77
73
|
async list() {
|
|
@@ -86,7 +82,7 @@ class UserController {
|
|
|
86
82
|
|
|
87
83
|
// POST 请求
|
|
88
84
|
@POST()
|
|
89
|
-
async create(body:
|
|
85
|
+
async create(body: ItemDTO) {
|
|
90
86
|
return { created: true };
|
|
91
87
|
}
|
|
92
88
|
}
|
|
@@ -106,8 +102,8 @@ class UserController {
|
|
|
106
102
|
```typescript
|
|
107
103
|
import { Table, Field, DBType, PrimaryKey, NotNull, Size } from "@fastcar/core/annotation";
|
|
108
104
|
|
|
109
|
-
@Table("
|
|
110
|
-
class
|
|
105
|
+
@Table("entities")
|
|
106
|
+
class Entity {
|
|
111
107
|
@Field("id")
|
|
112
108
|
@DBType("int")
|
|
113
109
|
@PrimaryKey
|
|
@@ -127,11 +123,10 @@ class User {
|
|
|
127
123
|
import { Entity, Repository } from "@fastcar/core/annotation";
|
|
128
124
|
import { MysqlMapper } from "@fastcar/mysql";
|
|
129
125
|
|
|
130
|
-
@Entity(
|
|
126
|
+
@Entity(Entity)
|
|
131
127
|
@Repository
|
|
132
|
-
class
|
|
133
|
-
|
|
134
|
-
export default UserMapper;
|
|
128
|
+
class EntityMapper extends MysqlMapper<Entity> {}
|
|
129
|
+
export default EntityMapper;
|
|
135
130
|
```
|
|
136
131
|
|
|
137
132
|
**Service 中使用:**
|
|
@@ -139,92 +134,65 @@ export default UserMapper;
|
|
|
139
134
|
```typescript
|
|
140
135
|
import { Service, Autowired } from "@fastcar/core/annotation";
|
|
141
136
|
import { OrderEnum } from "@fastcar/core/db";
|
|
142
|
-
import
|
|
137
|
+
import EntityMapper from "./EntityMapper";
|
|
143
138
|
|
|
144
139
|
@Service
|
|
145
|
-
class
|
|
140
|
+
class EntityService {
|
|
146
141
|
@Autowired
|
|
147
|
-
private
|
|
142
|
+
private mapper!: EntityMapper;
|
|
148
143
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return this.userMapper.select({
|
|
144
|
+
async getList() {
|
|
145
|
+
return this.mapper.select({
|
|
152
146
|
where: { status: 1 },
|
|
153
147
|
orders: { createTime: OrderEnum.desc },
|
|
154
148
|
limit: 10
|
|
155
149
|
});
|
|
156
150
|
}
|
|
157
151
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return this.userMapper.selectOne({ where: { id } });
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// 根据主键查询
|
|
164
|
-
async getUserById(id: number) {
|
|
165
|
-
return this.userMapper.selectByPrimaryKey({ id } as User);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 插入
|
|
169
|
-
async createUser(user: User) {
|
|
170
|
-
return this.userMapper.saveOne(user);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// 更新
|
|
174
|
-
async updateUser(id: number, data: Partial<User>) {
|
|
175
|
-
return this.userMapper.update({ where: { id }, row: data });
|
|
152
|
+
async getOne(id: number) {
|
|
153
|
+
return this.mapper.selectOne({ where: { id } });
|
|
176
154
|
}
|
|
177
155
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
return this.userMapper.updateByPrimaryKey(user);
|
|
156
|
+
async create(data: Entity) {
|
|
157
|
+
return this.mapper.saveOne(data);
|
|
181
158
|
}
|
|
182
159
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return this.userMapper.delete({ where: { id } });
|
|
160
|
+
async update(id: number, data: Partial<Entity>) {
|
|
161
|
+
return this.mapper.update({ where: { id }, row: data });
|
|
186
162
|
}
|
|
187
163
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return this.userMapper.count({});
|
|
164
|
+
async delete(id: number) {
|
|
165
|
+
return this.mapper.delete({ where: { id } });
|
|
191
166
|
}
|
|
192
167
|
}
|
|
193
168
|
```
|
|
194
169
|
|
|
195
170
|
### 表单验证 (@fastcar/core)
|
|
196
171
|
|
|
197
|
-
**正确的表单验证方式:**
|
|
198
|
-
|
|
199
172
|
```typescript
|
|
200
173
|
import { ValidForm, NotNull, Size, Rule } from "@fastcar/core/annotation";
|
|
201
174
|
|
|
202
|
-
|
|
203
|
-
class UserDTO {
|
|
175
|
+
class ItemDTO {
|
|
204
176
|
@NotNull
|
|
205
177
|
name!: string;
|
|
206
178
|
|
|
207
179
|
@Size({ minSize: 1, maxSize: 150 })
|
|
208
|
-
|
|
180
|
+
value!: number;
|
|
209
181
|
}
|
|
210
182
|
|
|
211
183
|
@Controller
|
|
212
|
-
@REQUEST("/api/
|
|
213
|
-
class
|
|
214
|
-
|
|
215
|
-
// GET 请求 - 无需表单验证
|
|
184
|
+
@REQUEST("/api/items")
|
|
185
|
+
class ItemController {
|
|
216
186
|
@GET()
|
|
217
187
|
async list(page: number = 1, pageSize: number = 10) {
|
|
218
188
|
return { page, pageSize, data: [] };
|
|
219
189
|
}
|
|
220
190
|
|
|
221
|
-
// POST 请求 - 使用 @ValidForm + @Rule 进行表单验证
|
|
222
191
|
@ValidForm
|
|
223
192
|
@POST()
|
|
224
|
-
async create(@Rule() body:
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return this.userService.create({ name, age });
|
|
193
|
+
async create(@Rule() body: ItemDTO) {
|
|
194
|
+
const { name, value } = body;
|
|
195
|
+
return this.service.create({ name, value });
|
|
228
196
|
}
|
|
229
197
|
}
|
|
230
198
|
```
|
|
@@ -238,32 +206,6 @@ class UserController {
|
|
|
238
206
|
| `@NotNull` | 参数不能为空 | 放在 DTO 字段上 |
|
|
239
207
|
| `@Size({min, max})` | 大小限制 | 放在 DTO 字段上 |
|
|
240
208
|
|
|
241
|
-
**⚠️ 常见错误:**
|
|
242
|
-
|
|
243
|
-
❌ **错误** - 使用不存在的装饰器:
|
|
244
|
-
```typescript
|
|
245
|
-
// 这些装饰器在 FastCar 中不存在!
|
|
246
|
-
import { Body, Param, Query } from "@fastcar/koa/annotation"; // ❌ 错误
|
|
247
|
-
|
|
248
|
-
@GET("/:id")
|
|
249
|
-
async getById(@Param("id") id: string) { ... } // ❌ 错误
|
|
250
|
-
|
|
251
|
-
@POST()
|
|
252
|
-
async create(@Body body: UserDTO) { ... } // ❌ 错误
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
✅ **正确** - 直接使用方法参数:
|
|
256
|
-
```typescript
|
|
257
|
-
@GET("/:id")
|
|
258
|
-
async getById(id: string) { ... } // ✅ 正确
|
|
259
|
-
|
|
260
|
-
@POST()
|
|
261
|
-
async create(body: UserDTO) { ... } // ✅ 正确
|
|
262
|
-
|
|
263
|
-
@GET()
|
|
264
|
-
async list(page: number = 1) { ... } // ✅ 正确
|
|
265
|
-
```
|
|
266
|
-
|
|
267
209
|
### Redis (@fastcar/redis)
|
|
268
210
|
|
|
269
211
|
```typescript
|
|
@@ -287,19 +229,21 @@ class CacheService {
|
|
|
287
229
|
|
|
288
230
|
### 定时任务 (@fastcar/timer)
|
|
289
231
|
|
|
232
|
+
> **推荐使用 `@fastcar/timer/scheduling2` 模块**
|
|
233
|
+
|
|
290
234
|
```typescript
|
|
291
|
-
import {
|
|
235
|
+
import { ScheduledInterval, ScheduledCron } from "@fastcar/timer/scheduling2";
|
|
292
236
|
|
|
293
237
|
@Component
|
|
294
238
|
class TaskService {
|
|
295
239
|
// 间隔执行(毫秒)
|
|
296
|
-
@
|
|
240
|
+
@ScheduledInterval({ fixedRate: 60000 })
|
|
297
241
|
async intervalTask() {
|
|
298
242
|
console.log("每分钟执行");
|
|
299
243
|
}
|
|
300
244
|
|
|
301
245
|
// Cron 表达式
|
|
302
|
-
@
|
|
246
|
+
@ScheduledCron("0 0 * * * *")
|
|
303
247
|
async hourlyTask() {
|
|
304
248
|
console.log("每小时执行");
|
|
305
249
|
}
|
|
@@ -318,7 +262,6 @@ class ComputeService {
|
|
|
318
262
|
|
|
319
263
|
@WorkerTask
|
|
320
264
|
heavyComputation(data: number[]): number {
|
|
321
|
-
// 在 worker 线程中执行
|
|
322
265
|
return data.reduce((a, b) => a + b, 0);
|
|
323
266
|
}
|
|
324
267
|
}
|
|
@@ -326,23 +269,20 @@ class ComputeService {
|
|
|
326
269
|
|
|
327
270
|
## 项目模板速查
|
|
328
271
|
|
|
329
|
-
FastCar CLI 提供 5
|
|
330
|
-
|
|
331
|
-
### 模板选择指南
|
|
272
|
+
FastCar CLI 提供 5 种项目模板:
|
|
332
273
|
|
|
333
274
|
| 模板 | 适用场景 | 核心依赖 | 关键注解 |
|
|
334
275
|
|------|---------|---------|---------|
|
|
335
276
|
| web | RESTful API 服务 | @fastcar/koa, @fastcar/server | @EnableKoa |
|
|
336
277
|
| static | 静态资源服务器 | @fastcar/koa, @fastcar/server | @EnableKoa + KoaStatic |
|
|
337
278
|
| rpc | RPC 微服务通信 | @fastcar/rpc, @fastcar/server | @EnableRPC |
|
|
338
|
-
| cos |
|
|
339
|
-
| microservices | 分布式多服务架构 | @fastcar/koa, @fastcar/rpc, @fastcar/server, @fastcar/timer | @EnableKoa / @EnableRPC
|
|
279
|
+
| cos | 对象存储/文件上传 | @fastcar/koa, @fastcar/cossdk, @fastcar/server | @EnableKoa |
|
|
280
|
+
| microservices | 分布式多服务架构 | @fastcar/koa, @fastcar/rpc, @fastcar/server, @fastcar/timer | @EnableKoa / @EnableRPC |
|
|
340
281
|
|
|
341
282
|
### 各模板入口示例
|
|
342
283
|
|
|
343
|
-
|
|
284
|
+
**Web 模板**
|
|
344
285
|
```typescript
|
|
345
|
-
import { FastCarApplication } from "@fastcar/core";
|
|
346
286
|
import { Application } from "@fastcar/core/annotation";
|
|
347
287
|
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
348
288
|
import { ExceptionGlobalHandler, KoaBodyParser } from "@fastcar/koa";
|
|
@@ -357,23 +297,7 @@ class APP {
|
|
|
357
297
|
export default new APP();
|
|
358
298
|
```
|
|
359
299
|
|
|
360
|
-
|
|
361
|
-
```typescript
|
|
362
|
-
import { Application } from "@fastcar/core/annotation";
|
|
363
|
-
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
364
|
-
import { ExceptionGlobalHandler, KoaStatic } from "@fastcar/koa";
|
|
365
|
-
|
|
366
|
-
@Application
|
|
367
|
-
@EnableKoa
|
|
368
|
-
@KoaMiddleware(ExceptionGlobalHandler)
|
|
369
|
-
@KoaMiddleware(KoaStatic)
|
|
370
|
-
class APP {
|
|
371
|
-
app!: any;
|
|
372
|
-
}
|
|
373
|
-
export default new APP();
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
#### RPC 模板
|
|
300
|
+
**RPC 模板**
|
|
377
301
|
```typescript
|
|
378
302
|
import { Application } from "@fastcar/core/annotation";
|
|
379
303
|
import { EnableRPC } from "@fastcar/rpc/annotation";
|
|
@@ -384,70 +308,21 @@ class APP {}
|
|
|
384
308
|
export default new APP();
|
|
385
309
|
```
|
|
386
310
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
import { Application } from "@fastcar/core/annotation";
|
|
390
|
-
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
391
|
-
import { ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors } from "@fastcar/koa";
|
|
392
|
-
|
|
393
|
-
@EnableKoa
|
|
394
|
-
@Application
|
|
395
|
-
@KoaMiddleware(ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors)
|
|
396
|
-
class APP {}
|
|
397
|
-
export default new APP();
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
#### Microservices 模板
|
|
401
|
-
微服务模板采用多服务架构,包含 `app-node.ts`(子进程启动多服务)和 `app-pm2.ts`(PM2 启动入口)。服务模块分为:
|
|
402
|
-
- **center**:服务中心,提供服务注册与发现
|
|
403
|
-
- **connector**:连接器服务,处理客户端连接
|
|
404
|
-
- **chat**:聊天服务,处理实时消息
|
|
405
|
-
- **web**:Web 服务,提供 HTTP 接口
|
|
406
|
-
- **base**:基础服务,提供公共功能
|
|
407
|
-
|
|
408
|
-
各服务模块内部根据职责使用 `@EnableKoa` 或 `@EnableRPC`。
|
|
311
|
+
**Microservices 模板**
|
|
312
|
+
微服务模板包含多服务架构:center(服务中心)、connector(连接器)、message(消息服务)、web(Web服务)、base(基础服务)。
|
|
409
313
|
|
|
410
314
|
### 项目结构示例
|
|
411
315
|
|
|
412
|
-
**Web / Static / RPC / COS 模板**
|
|
413
316
|
```
|
|
414
317
|
template/
|
|
415
318
|
├── src/
|
|
416
319
|
│ ├── controller/ # 控制器(web/cos)
|
|
417
320
|
│ ├── dto/ # DTO 类(表单验证)
|
|
418
|
-
│ ├──
|
|
321
|
+
│ ├── service/ # 服务层
|
|
419
322
|
│ ├── model/ # 数据模型
|
|
420
323
|
│ └── app.ts # 应用入口
|
|
421
324
|
├── resource/
|
|
422
325
|
│ └── application.yml # 配置文件
|
|
423
|
-
├── target/ # 编译输出
|
|
424
|
-
├── package.json
|
|
425
|
-
├── tsconfig.json
|
|
426
|
-
└── ecosystem.config.yml
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
**Microservices 模板**
|
|
430
|
-
```
|
|
431
|
-
template/
|
|
432
|
-
├── src/
|
|
433
|
-
│ ├── annotation/ # 注解定义
|
|
434
|
-
│ ├── common/ # 公共代码
|
|
435
|
-
│ ├── middleware/ # 中间件
|
|
436
|
-
│ ├── servers/ # 服务目录
|
|
437
|
-
│ │ ├── base/ # 基础服务
|
|
438
|
-
│ │ ├── center/ # 服务中心
|
|
439
|
-
│ │ ├── chat/ # 聊天服务
|
|
440
|
-
│ │ ├── connector/ # 连接器服务
|
|
441
|
-
│ │ └── web/ # Web 服务
|
|
442
|
-
│ ├── types/ # 类型定义
|
|
443
|
-
│ ├── utils/ # 工具函数
|
|
444
|
-
│ ├── app-node.ts # 单节点入口(子进程启动)
|
|
445
|
-
│ └── app-pm2.ts # PM2 入口
|
|
446
|
-
├── resource/
|
|
447
|
-
│ ├── application.yml
|
|
448
|
-
│ ├── application-dev.yml
|
|
449
|
-
│ └── ecosystem.config.yml
|
|
450
|
-
├── test/
|
|
451
326
|
├── package.json
|
|
452
327
|
└── tsconfig.json
|
|
453
328
|
```
|
|
@@ -470,7 +345,7 @@ npm i @fastcar/core @fastcar/koa @fastcar/rpc @fastcar/server @fastcar/timer
|
|
|
470
345
|
|
|
471
346
|
## 配置管理
|
|
472
347
|
|
|
473
|
-
配置文件放在 `resource/application.yml
|
|
348
|
+
配置文件放在 `resource/application.yml`。支持按 `env` 加载多文件,例如 `application-dev.yml` 会与主配置合并。
|
|
474
349
|
|
|
475
350
|
### 基础配置示例
|
|
476
351
|
|
|
@@ -507,9 +382,7 @@ class AppConfig {
|
|
|
507
382
|
}
|
|
508
383
|
```
|
|
509
384
|
|
|
510
|
-
###
|
|
511
|
-
|
|
512
|
-
#### Web / Static / COS 通用 Koa 配置
|
|
385
|
+
### Web 模板 application.yml
|
|
513
386
|
|
|
514
387
|
```yaml
|
|
515
388
|
application:
|
|
@@ -519,20 +392,13 @@ settings:
|
|
|
519
392
|
koa:
|
|
520
393
|
server:
|
|
521
394
|
- { port: 8080, host: "0.0.0.0" }
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
{ "public": "public" } # 别名: 路径(相对 resource 目录或绝对路径)
|
|
526
|
-
koaBodyParser: # 请求体解析
|
|
395
|
+
koaStatic:
|
|
396
|
+
{ "public": "public" }
|
|
397
|
+
koaBodyParser:
|
|
527
398
|
enableTypes: ["json", "form", "text"]
|
|
528
|
-
extendTypes: { text: ["text/xml", "application/xml"] }
|
|
529
399
|
```
|
|
530
400
|
|
|
531
|
-
|
|
532
|
-
- `settings.koa.koaStatic`:静态资源访问映射,格式为 `{ "别名": "路径" }`。
|
|
533
|
-
- `settings.koa.koaBodyParser`:Koa Body 解析器配置,`enableTypes` 控制允许的请求体类型,`extendTypes` 可扩展 XML 等特殊类型。
|
|
534
|
-
|
|
535
|
-
#### RPC 模板配置
|
|
401
|
+
### RPC 模板配置
|
|
536
402
|
|
|
537
403
|
```yaml
|
|
538
404
|
application:
|
|
@@ -542,123 +408,38 @@ settings:
|
|
|
542
408
|
rpc:
|
|
543
409
|
list:
|
|
544
410
|
- id: "server-1"
|
|
545
|
-
type: "ws"
|
|
411
|
+
type: "ws"
|
|
546
412
|
server: { port: 1235 }
|
|
547
|
-
|
|
548
|
-
serviceType: "base" # 服务类型分类
|
|
413
|
+
serviceType: "base"
|
|
549
414
|
secure:
|
|
550
415
|
username: "user"
|
|
551
|
-
password: "
|
|
416
|
+
password: "password"
|
|
552
417
|
```
|
|
553
418
|
|
|
554
|
-
|
|
555
|
-
- `id`:节点唯一标识。
|
|
556
|
-
- `type`:通信协议,常见取值 `ws`、`http`、`grpc`、`mqtt`。
|
|
557
|
-
- `server`:监听配置,通常只写 `{ port }`。
|
|
558
|
-
- `extra`:协议扩展参数。
|
|
559
|
-
- `serviceType`:服务类型,用于服务分组或路由。
|
|
560
|
-
- `secure`:安全认证信息,包含 `username` 和 `password`。
|
|
561
|
-
|
|
562
|
-
#### COS 模板特有配置
|
|
419
|
+
### Microservices 模板配置
|
|
563
420
|
|
|
564
421
|
```yaml
|
|
565
|
-
application:
|
|
566
|
-
env: "prod"
|
|
567
|
-
|
|
568
422
|
settings:
|
|
569
|
-
hotterSysConfig: true # 启用系统配置热更新监听
|
|
570
|
-
```
|
|
571
|
-
|
|
572
|
-
- `settings.hotterSysConfig`:设为 `true` 时,框架会监听配置变更并自动热更新。
|
|
573
|
-
|
|
574
|
-
#### Microservices 模板配置
|
|
575
|
-
|
|
576
|
-
主配置通常只声明环境:
|
|
577
|
-
|
|
578
|
-
```yaml
|
|
579
|
-
application:
|
|
580
|
-
env: "dev"
|
|
581
|
-
```
|
|
582
|
-
|
|
583
|
-
详细集群配置放在 `application-dev.yml`:
|
|
584
|
-
|
|
585
|
-
```yaml
|
|
586
|
-
settings:
|
|
587
|
-
hotterSysConfig: true # 监听系统配置变更
|
|
588
|
-
|
|
589
423
|
microservices:
|
|
590
|
-
center:
|
|
591
|
-
token: "
|
|
424
|
+
center:
|
|
425
|
+
token: "your-token-here"
|
|
592
426
|
servers:
|
|
593
427
|
- host: "localhost"
|
|
594
|
-
clusters: 1
|
|
428
|
+
clusters: 1
|
|
595
429
|
list:
|
|
596
430
|
- type: "ws"
|
|
597
431
|
server: { port: 60000 }
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
disconnectInterval: 1000 # 断线重连间隔(毫秒)
|
|
601
|
-
retry:
|
|
602
|
-
retryCount: 3
|
|
603
|
-
retryInterval: 3000
|
|
604
|
-
timeout: 30000
|
|
605
|
-
maxMsgNum: 10000
|
|
606
|
-
increase: true
|
|
607
|
-
|
|
608
|
-
connector: # 连接器服务
|
|
609
|
-
token: "x3TGsWC9uloZu235LA07eAiJ61nQ1A5f"
|
|
432
|
+
connector:
|
|
433
|
+
token: "your-token-here"
|
|
610
434
|
servers:
|
|
611
435
|
- host: "localhost"
|
|
612
436
|
clusters: 1
|
|
613
437
|
list:
|
|
614
|
-
- front: true
|
|
438
|
+
- front: true
|
|
615
439
|
type: "ws"
|
|
616
440
|
server: { port: 60100 }
|
|
617
|
-
|
|
618
|
-
chat: # 聊天服务
|
|
619
|
-
token: "go0kbkNM3wQ4e2Vgo0kbkNM3wQ4e2V"
|
|
620
|
-
servers:
|
|
621
|
-
- host: "localhost"
|
|
622
|
-
clusters: 1
|
|
623
|
-
list:
|
|
624
|
-
- type: "ws"
|
|
625
|
-
server: { port: 60200 }
|
|
626
|
-
|
|
627
|
-
web: # Web 服务
|
|
628
|
-
token: "go0kbkNM3wQ4e2Vgo0kbkNM3wQ4e2V"
|
|
629
|
-
koa:
|
|
630
|
-
koaBodyParser:
|
|
631
|
-
enableTypes: ["json", "form", "text"]
|
|
632
|
-
extendTypes: { text: ["text/xml", "application/xml"] }
|
|
633
|
-
servers:
|
|
634
|
-
- host: "localhost"
|
|
635
|
-
clusters: 1
|
|
636
|
-
list:
|
|
637
|
-
- type: "http"
|
|
638
|
-
server: { port: 8080 }
|
|
639
|
-
- type: "ws"
|
|
640
|
-
server: { port: 60300 }
|
|
641
441
|
```
|
|
642
442
|
|
|
643
|
-
- `settings.microservices.<服务名>`:定义各微服务模块的集群配置。
|
|
644
|
-
- `token`:服务间通信鉴权令牌,防止非法节点接入。
|
|
645
|
-
- `servers`:服务器集群列表。
|
|
646
|
-
- `host`:主机地址。
|
|
647
|
-
- `clusters`:集群实例数量。若大于 1,框架会自动递增 `serviceId` 和端口号生成多个实例。
|
|
648
|
-
- `list`:该集群对外暴露的协议端点列表。
|
|
649
|
-
- `type`:协议类型,如 `ws`、`http`。
|
|
650
|
-
- `server`:端口配置 `{ port }`。
|
|
651
|
-
- `front: true`:仅 connector 等前置服务需要,表示该节点直接面向客户端。
|
|
652
|
-
- `timeout`:连接超时时间(毫秒),`0` 表示永不超时。
|
|
653
|
-
- `connectionLimit`:最大连接数限制。
|
|
654
|
-
- `disconnectInterval`:断线后重连间隔(毫秒)。
|
|
655
|
-
- `retry`:消息重试策略。
|
|
656
|
-
- `retryCount`:最大重试次数。
|
|
657
|
-
- `retryInterval`:重试间隔。
|
|
658
|
-
- `timeout`:重试总超时。
|
|
659
|
-
- `maxMsgNum`:消息队列最大长度。
|
|
660
|
-
- `increase`:是否递增重试间隔。
|
|
661
|
-
|
|
662
443
|
## 生命周期钩子
|
|
663
444
|
|
|
664
445
|
```typescript
|
|
@@ -666,19 +447,16 @@ import { ApplicationStart, ApplicationStop, ApplicationInit } from "@fastcar/cor
|
|
|
666
447
|
|
|
667
448
|
@Component
|
|
668
449
|
class LifecycleService {
|
|
669
|
-
// 应用启动时执行
|
|
670
450
|
@ApplicationStart
|
|
671
451
|
async onStart() {
|
|
672
452
|
console.log("应用启动");
|
|
673
453
|
}
|
|
674
454
|
|
|
675
|
-
// 应用停止前执行
|
|
676
455
|
@ApplicationStop
|
|
677
456
|
async onStop() {
|
|
678
457
|
console.log("应用停止");
|
|
679
458
|
}
|
|
680
459
|
|
|
681
|
-
// 初始化(配合 @ApplicationRunner)
|
|
682
460
|
@ApplicationInit
|
|
683
461
|
async init() {
|
|
684
462
|
console.log("初始化完成");
|
|
@@ -737,52 +515,17 @@ fastcar-cli init web
|
|
|
737
515
|
npm install
|
|
738
516
|
npm run debug
|
|
739
517
|
|
|
740
|
-
# Static 项目
|
|
741
|
-
mkdir my-static-app && cd my-static-app
|
|
742
|
-
fastcar-cli init static
|
|
743
|
-
npm install
|
|
744
|
-
npm run debug
|
|
745
|
-
|
|
746
518
|
# RPC 项目
|
|
747
519
|
mkdir my-rpc-app && cd my-rpc-app
|
|
748
520
|
fastcar-cli init rpc
|
|
749
521
|
npm install
|
|
750
522
|
npm run debug
|
|
751
523
|
|
|
752
|
-
# COS 项目
|
|
753
|
-
mkdir my-cos-app && cd my-cos-app
|
|
754
|
-
fastcar-cli init cos
|
|
755
|
-
npm install
|
|
756
|
-
npm run debug
|
|
757
|
-
|
|
758
524
|
# Microservices 项目
|
|
759
525
|
mkdir my-ms-app && cd my-ms-app
|
|
760
526
|
fastcar-cli init microservices
|
|
761
527
|
npm install
|
|
762
|
-
npm run start-node
|
|
763
|
-
# 或
|
|
764
|
-
npm run start-pm2 # PM2 模式
|
|
765
|
-
```
|
|
766
|
-
|
|
767
|
-
### 手动创建项目
|
|
768
|
-
|
|
769
|
-
```bash
|
|
770
|
-
# 1. 创建项目
|
|
771
|
-
mkdir my-fastcar-app && cd my-fastcar-app
|
|
772
|
-
npm init -y
|
|
773
|
-
|
|
774
|
-
# 2. 安装依赖
|
|
775
|
-
npm i @fastcar/core @fastcar/koa @fastcar/server
|
|
776
|
-
npm i -D typescript ts-node @types/node
|
|
777
|
-
|
|
778
|
-
# 3. 初始化 TypeScript
|
|
779
|
-
npx tsc --init
|
|
780
|
-
|
|
781
|
-
# 4. 启用装饰器(tsconfig.json)
|
|
782
|
-
# "experimentalDecorators": true
|
|
783
|
-
# "emitDecoratorMetadata": true
|
|
784
|
-
|
|
785
|
-
# 5. 创建入口文件和配置文件,开始开发
|
|
528
|
+
npm run start-node
|
|
786
529
|
```
|
|
787
530
|
|
|
788
531
|
## 常见错误与注意事项
|
|
@@ -809,18 +552,12 @@ import { Body, Param, Query } from "@fastcar/koa/annotation";
|
|
|
809
552
|
|
|
810
553
|
@GET("/:id")
|
|
811
554
|
async getById(@Param("id") id: string) { }
|
|
812
|
-
|
|
813
|
-
@POST()
|
|
814
|
-
async create(@Body body: UserDTO) { }
|
|
815
555
|
```
|
|
816
556
|
|
|
817
557
|
✅ **正确:**
|
|
818
558
|
```typescript
|
|
819
559
|
@GET("/:id")
|
|
820
560
|
async getById(id: string) { }
|
|
821
|
-
|
|
822
|
-
@POST()
|
|
823
|
-
async create(body: UserDTO) { }
|
|
824
561
|
```
|
|
825
562
|
|
|
826
563
|
### 3. 表单验证使用 @ValidForm + @Rule
|
|
@@ -828,29 +565,12 @@ async create(body: UserDTO) { }
|
|
|
828
565
|
❌ **错误:**
|
|
829
566
|
```typescript
|
|
830
567
|
@POST()
|
|
831
|
-
async create(@Body body:
|
|
568
|
+
async create(@Body body: ItemDTO) { }
|
|
832
569
|
```
|
|
833
570
|
|
|
834
571
|
✅ **正确:**
|
|
835
572
|
```typescript
|
|
836
573
|
@ValidForm
|
|
837
574
|
@POST()
|
|
838
|
-
async create(@Rule() body:
|
|
839
|
-
```
|
|
840
|
-
|
|
841
|
-
### 4. DTO 类放在单独文件夹
|
|
842
|
-
|
|
843
|
-
推荐项目结构:
|
|
575
|
+
async create(@Rule() body: ItemDTO) { }
|
|
844
576
|
```
|
|
845
|
-
src/
|
|
846
|
-
├── controller/ # 控制器
|
|
847
|
-
├── dto/ # DTO 类(表单验证)
|
|
848
|
-
├── service/ # 服务层
|
|
849
|
-
├── model/ # 数据模型
|
|
850
|
-
└── app.ts
|
|
851
|
-
```
|
|
852
|
-
|
|
853
|
-
## 参考资源
|
|
854
|
-
|
|
855
|
-
- 详细 API 文档:[references/api-reference.md](references/api-reference.md)
|
|
856
|
-
- 项目模板:[assets/project-template/](assets/project-template/)
|