@fastcar/cli 0.0.7 → 0.1.0
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/README.md +259 -21
- package/bin/cli.js +202 -32
- package/package.json +3 -3
- package/skills/fastcar-database/SKILL.md +374 -0
- package/skills/fastcar-framework/SKILL.md +661 -0
- package/skills/fastcar-framework/assets/project-template/package.json +24 -0
- package/skills/fastcar-framework/assets/project-template/resource/application.yml +14 -0
- package/skills/fastcar-framework/assets/project-template/src/app.ts +18 -0
- package/skills/fastcar-framework/assets/project-template/src/component/StartupRunner.ts +15 -0
- package/skills/fastcar-framework/assets/project-template/src/controller/HelloController.ts +21 -0
- package/skills/fastcar-framework/assets/project-template/src/service/HelloService.ts +12 -0
- package/skills/fastcar-framework/assets/project-template/tsconfig.json +21 -0
- package/skills/fastcar-framework/references/api-reference.md +353 -0
- package/skills/fastcar-rpc-microservices/SKILL.md +311 -0
- package/skills/fastcar-serverless/SKILL.md +211 -0
- package/skills/fastcar-toolkit/SKILL.md +282 -0
- package/src/init.js +615 -201
- package/src/pack.js +204 -0
- package/src/reverse.js +204 -0
- package/src/skill-targets.js +74 -0
- package/src/skill.js +362 -0
- package/src/templates.json +32 -0
- package/src/utils.js +51 -66
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: fastcar-framework
|
|
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, (7) Selecting and configuring project templates (web, rpc, cos, static, microservices), (8) Writing application.yml for different templates.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# FastCar Framework
|
|
7
|
+
|
|
8
|
+
FastCar 是基于 TypeScript 的 Node.js 企业级应用开发框架,灵感来源于 Spring Boot,采用 IoC(控制反转)设计思想。
|
|
9
|
+
|
|
10
|
+
## 核心概念
|
|
11
|
+
|
|
12
|
+
### IoC 容器与装饰器
|
|
13
|
+
|
|
14
|
+
| 装饰器 | 用途 | 示例 |
|
|
15
|
+
|--------|------|------|
|
|
16
|
+
| `@Application` | 入口应用类 | `@Application class App {}` |
|
|
17
|
+
| `@Component` | 通用组件 | `@Component class UtilService {}` |
|
|
18
|
+
| `@Service` | 服务层 | `@Service class UserService {}` |
|
|
19
|
+
| `@Controller` | 控制器层 | `@Controller class UserController {}` |
|
|
20
|
+
| `@Repository` | 数据访问层 | `@Repository class UserRepository {}` |
|
|
21
|
+
| `@Autowired` | 依赖注入 | `@Autowired private userService!: UserService;` |
|
|
22
|
+
|
|
23
|
+
### 基础应用结构
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
27
|
+
import { Application, Autowired, Component, Service } from "@fastcar/core/annotation";
|
|
28
|
+
|
|
29
|
+
// 服务层
|
|
30
|
+
@Service
|
|
31
|
+
class UserService {
|
|
32
|
+
getUsers() {
|
|
33
|
+
return [{ id: 1, name: "Alice" }];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 控制器层
|
|
38
|
+
@Controller
|
|
39
|
+
class UserController {
|
|
40
|
+
@Autowired
|
|
41
|
+
private userService!: UserService;
|
|
42
|
+
|
|
43
|
+
getUsers() {
|
|
44
|
+
return this.userService.getUsers();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 应用入口
|
|
49
|
+
@Application
|
|
50
|
+
class App {
|
|
51
|
+
app!: FastCarApplication;
|
|
52
|
+
|
|
53
|
+
async start() {
|
|
54
|
+
console.log("应用启动成功!");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 启动
|
|
59
|
+
const app = new App();
|
|
60
|
+
app.start();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 模块速查
|
|
64
|
+
|
|
65
|
+
### Web 开发 (@fastcar/koa)
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { GET, POST, REQUEST, Body, Param } from "@fastcar/koa/annotation";
|
|
69
|
+
|
|
70
|
+
@Controller
|
|
71
|
+
@REQUEST("/api/users")
|
|
72
|
+
class UserController {
|
|
73
|
+
@GET
|
|
74
|
+
async list() {
|
|
75
|
+
return { data: [] };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@GET("/:id")
|
|
79
|
+
async getById(@Param("id") id: string) {
|
|
80
|
+
return { id };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@POST
|
|
84
|
+
async create(@Body user: UserDTO) {
|
|
85
|
+
return { created: true };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 数据库 (@fastcar/mysql)
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { Repository, Table, Field, PrimaryKey, SqlSession } from "@fastcar/mysql/annotation";
|
|
94
|
+
|
|
95
|
+
@Table("users")
|
|
96
|
+
class User {
|
|
97
|
+
@PrimaryKey
|
|
98
|
+
@Field("id")
|
|
99
|
+
id!: number;
|
|
100
|
+
|
|
101
|
+
@Field("name")
|
|
102
|
+
name!: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@Repository
|
|
106
|
+
class UserRepository {
|
|
107
|
+
@SqlSession
|
|
108
|
+
private session!: SqlSession;
|
|
109
|
+
|
|
110
|
+
async findById(id: number) {
|
|
111
|
+
return this.session.findById(User, id);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Redis (@fastcar/redis)
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { RedisClient } from "@fastcar/redis/annotation";
|
|
120
|
+
|
|
121
|
+
@Service
|
|
122
|
+
class CacheService {
|
|
123
|
+
@RedisClient
|
|
124
|
+
private redis!: RedisClient;
|
|
125
|
+
|
|
126
|
+
async get(key: string) {
|
|
127
|
+
return this.redis.get(key);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async set(key: string, value: string, ttl?: number) {
|
|
131
|
+
await this.redis.set(key, value, ttl);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 定时任务 (@fastcar/timer)
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { Scheduled, Cron } from "@fastcar/timer/annotation";
|
|
140
|
+
|
|
141
|
+
@Component
|
|
142
|
+
class TaskService {
|
|
143
|
+
// 间隔执行(毫秒)
|
|
144
|
+
@Scheduled(60000)
|
|
145
|
+
async intervalTask() {
|
|
146
|
+
console.log("每分钟执行");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Cron 表达式
|
|
150
|
+
@Cron("0 0 * * * *")
|
|
151
|
+
async hourlyTask() {
|
|
152
|
+
console.log("每小时执行");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 工作线程池 (@fastcar/workerpool)
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { WorkerPool, WorkerTask } from "@fastcar/workerpool/annotation";
|
|
161
|
+
|
|
162
|
+
@Component
|
|
163
|
+
class ComputeService {
|
|
164
|
+
@WorkerPool({ minWorkers: 2, maxWorkers: 4 })
|
|
165
|
+
private pool!: WorkerPool;
|
|
166
|
+
|
|
167
|
+
@WorkerTask
|
|
168
|
+
heavyComputation(data: number[]): number {
|
|
169
|
+
// 在 worker 线程中执行
|
|
170
|
+
return data.reduce((a, b) => a + b, 0);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 项目模板速查
|
|
176
|
+
|
|
177
|
+
FastCar CLI 提供 5 种项目模板,分别适用于不同的业务场景。
|
|
178
|
+
|
|
179
|
+
### 模板选择指南
|
|
180
|
+
|
|
181
|
+
| 模板 | 适用场景 | 核心依赖 | 关键注解 |
|
|
182
|
+
|------|---------|---------|---------|
|
|
183
|
+
| web | RESTful API 服务 | @fastcar/koa, @fastcar/server | @EnableKoa |
|
|
184
|
+
| static | 静态资源服务器 | @fastcar/koa, @fastcar/server | @EnableKoa + KoaStatic |
|
|
185
|
+
| rpc | RPC 微服务通信 | @fastcar/rpc, @fastcar/server | @EnableRPC |
|
|
186
|
+
| cos | 对象存储/文件上传/直播转码 | @fastcar/koa, @fastcar/cossdk, @fastcar/server | @EnableKoa |
|
|
187
|
+
| microservices | 分布式多服务架构 | @fastcar/koa, @fastcar/rpc, @fastcar/server, @fastcar/timer | @EnableKoa / @EnableRPC(按服务模块) |
|
|
188
|
+
|
|
189
|
+
### 各模板入口示例
|
|
190
|
+
|
|
191
|
+
#### Web 模板
|
|
192
|
+
```typescript
|
|
193
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
194
|
+
import { Application } from "@fastcar/core/annotation";
|
|
195
|
+
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
196
|
+
import { ExceptionGlobalHandler, KoaBodyParser } from "@fastcar/koa";
|
|
197
|
+
|
|
198
|
+
@Application
|
|
199
|
+
@EnableKoa
|
|
200
|
+
@KoaMiddleware(ExceptionGlobalHandler)
|
|
201
|
+
@KoaMiddleware(KoaBodyParser)
|
|
202
|
+
class APP {
|
|
203
|
+
app!: FastCarApplication;
|
|
204
|
+
}
|
|
205
|
+
export default new APP();
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### Static 模板
|
|
209
|
+
```typescript
|
|
210
|
+
import { Application } from "@fastcar/core/annotation";
|
|
211
|
+
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
212
|
+
import { ExceptionGlobalHandler, KoaStatic } from "@fastcar/koa";
|
|
213
|
+
|
|
214
|
+
@Application
|
|
215
|
+
@EnableKoa
|
|
216
|
+
@KoaMiddleware(ExceptionGlobalHandler)
|
|
217
|
+
@KoaMiddleware(KoaStatic)
|
|
218
|
+
class APP {
|
|
219
|
+
app!: any;
|
|
220
|
+
}
|
|
221
|
+
export default new APP();
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### RPC 模板
|
|
225
|
+
```typescript
|
|
226
|
+
import { Application } from "@fastcar/core/annotation";
|
|
227
|
+
import { EnableRPC } from "@fastcar/rpc/annotation";
|
|
228
|
+
|
|
229
|
+
@Application
|
|
230
|
+
@EnableRPC
|
|
231
|
+
class APP {}
|
|
232
|
+
export default new APP();
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### COS 模板
|
|
236
|
+
```typescript
|
|
237
|
+
import { Application } from "@fastcar/core/annotation";
|
|
238
|
+
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
239
|
+
import { ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors } from "@fastcar/koa";
|
|
240
|
+
|
|
241
|
+
@EnableKoa
|
|
242
|
+
@Application
|
|
243
|
+
@KoaMiddleware(ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors)
|
|
244
|
+
class APP {}
|
|
245
|
+
export default new APP();
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### Microservices 模板
|
|
249
|
+
微服务模板采用多服务架构,包含 `app-node.ts`(子进程启动多服务)和 `app-pm2.ts`(PM2 启动入口)。服务模块分为:
|
|
250
|
+
- **center**:服务中心,提供服务注册与发现
|
|
251
|
+
- **connector**:连接器服务,处理客户端连接
|
|
252
|
+
- **chat**:聊天服务,处理实时消息
|
|
253
|
+
- **web**:Web 服务,提供 HTTP 接口
|
|
254
|
+
- **base**:基础服务,提供公共功能
|
|
255
|
+
|
|
256
|
+
各服务模块内部根据职责使用 `@EnableKoa` 或 `@EnableRPC`。
|
|
257
|
+
|
|
258
|
+
### 项目结构示例
|
|
259
|
+
|
|
260
|
+
**Web / Static / RPC / COS 模板**
|
|
261
|
+
```
|
|
262
|
+
template/
|
|
263
|
+
├── src/
|
|
264
|
+
│ ├── controller/ # 控制器(web/cos)
|
|
265
|
+
│ ├── middleware/ # 中间件(web/cos)
|
|
266
|
+
│ ├── model/ # 数据模型
|
|
267
|
+
│ └── app.ts # 应用入口
|
|
268
|
+
├── resource/
|
|
269
|
+
│ └── application.yml # 配置文件
|
|
270
|
+
├── target/ # 编译输出
|
|
271
|
+
├── package.json
|
|
272
|
+
├── tsconfig.json
|
|
273
|
+
└── ecosystem.config.yml
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
**Microservices 模板**
|
|
277
|
+
```
|
|
278
|
+
template/
|
|
279
|
+
├── src/
|
|
280
|
+
│ ├── annotation/ # 注解定义
|
|
281
|
+
│ ├── common/ # 公共代码
|
|
282
|
+
│ ├── middleware/ # 中间件
|
|
283
|
+
│ ├── servers/ # 服务目录
|
|
284
|
+
│ │ ├── base/ # 基础服务
|
|
285
|
+
│ │ ├── center/ # 服务中心
|
|
286
|
+
│ │ ├── chat/ # 聊天服务
|
|
287
|
+
│ │ ├── connector/ # 连接器服务
|
|
288
|
+
│ │ └── web/ # Web 服务
|
|
289
|
+
│ ├── types/ # 类型定义
|
|
290
|
+
│ ├── utils/ # 工具函数
|
|
291
|
+
│ ├── app-node.ts # 单节点入口(子进程启动)
|
|
292
|
+
│ └── app-pm2.ts # PM2 入口
|
|
293
|
+
├── resource/
|
|
294
|
+
│ ├── application.yml
|
|
295
|
+
│ ├── application-dev.yml
|
|
296
|
+
│ └── ecosystem.config.yml
|
|
297
|
+
├── test/
|
|
298
|
+
├── package.json
|
|
299
|
+
└── tsconfig.json
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### 模板依赖安装
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
# Web / Static
|
|
306
|
+
npm i @fastcar/core @fastcar/koa @fastcar/server
|
|
307
|
+
|
|
308
|
+
# RPC
|
|
309
|
+
npm i @fastcar/core @fastcar/rpc @fastcar/server
|
|
310
|
+
|
|
311
|
+
# COS
|
|
312
|
+
npm i @fastcar/core @fastcar/koa @fastcar/cossdk @fastcar/server
|
|
313
|
+
|
|
314
|
+
# Microservices
|
|
315
|
+
npm i @fastcar/core @fastcar/koa @fastcar/rpc @fastcar/server @fastcar/timer
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
## 配置管理
|
|
319
|
+
|
|
320
|
+
配置文件放在 `resource/application.yml`。FastCar 支持按 `env` 加载多文件,例如 `application-dev.yml` 会与主配置合并。
|
|
321
|
+
|
|
322
|
+
### 基础配置示例
|
|
323
|
+
|
|
324
|
+
```yaml
|
|
325
|
+
application:
|
|
326
|
+
name: my-app
|
|
327
|
+
version: 1.0.0
|
|
328
|
+
env: dev
|
|
329
|
+
|
|
330
|
+
mysql:
|
|
331
|
+
host: localhost
|
|
332
|
+
port: 3306
|
|
333
|
+
database: mydb
|
|
334
|
+
username: root
|
|
335
|
+
password: password
|
|
336
|
+
|
|
337
|
+
redis:
|
|
338
|
+
host: localhost
|
|
339
|
+
port: 6379
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
使用配置:
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { Configure, Value } from "@fastcar/core/annotation";
|
|
346
|
+
|
|
347
|
+
@Configure
|
|
348
|
+
class AppConfig {
|
|
349
|
+
@Value("server.port")
|
|
350
|
+
port!: number;
|
|
351
|
+
|
|
352
|
+
@Value("mysql.host")
|
|
353
|
+
dbHost!: string;
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### 各模板 application.yml 详解
|
|
358
|
+
|
|
359
|
+
#### Web / Static / COS 通用 Koa 配置
|
|
360
|
+
|
|
361
|
+
```yaml
|
|
362
|
+
application:
|
|
363
|
+
env: "dev"
|
|
364
|
+
|
|
365
|
+
settings:
|
|
366
|
+
koa:
|
|
367
|
+
server:
|
|
368
|
+
- { port: 8080, host: "0.0.0.0" }
|
|
369
|
+
# HTTPS 示例:
|
|
370
|
+
# - { port: 443, host: "0.0.0.0", protocol: https, ssl: { key: "./ssl/server.key", cert: "./ssl/server.pem" } }
|
|
371
|
+
koaStatic: # 静态资源映射
|
|
372
|
+
{ "public": "public" } # 别名: 路径(相对 resource 目录或绝对路径)
|
|
373
|
+
koaBodyParser: # 请求体解析
|
|
374
|
+
enableTypes: ["json", "form", "text"]
|
|
375
|
+
extendTypes: { text: ["text/xml", "application/xml"] }
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
- `settings.koa.server`:服务器监听配置数组。支持 `http`(默认)、`http2`、`https`;启用 HTTPS 时需额外指定 `protocol: https` 和 `ssl.key / ssl.cert`。
|
|
379
|
+
- `settings.koa.koaStatic`:静态资源访问映射,格式为 `{ "别名": "路径" }`。
|
|
380
|
+
- `settings.koa.koaBodyParser`:Koa Body 解析器配置,`enableTypes` 控制允许的请求体类型,`extendTypes` 可扩展 XML 等特殊类型。
|
|
381
|
+
|
|
382
|
+
#### RPC 模板配置
|
|
383
|
+
|
|
384
|
+
```yaml
|
|
385
|
+
application:
|
|
386
|
+
name: "fastcar-boot-rpc"
|
|
387
|
+
|
|
388
|
+
settings:
|
|
389
|
+
rpc:
|
|
390
|
+
list:
|
|
391
|
+
- id: "server-1"
|
|
392
|
+
type: "ws" # 通信协议:ws / http / grpc / mqtt
|
|
393
|
+
server: { port: 1235 }
|
|
394
|
+
extra: {}
|
|
395
|
+
serviceType: "base" # 服务类型分类
|
|
396
|
+
secure:
|
|
397
|
+
username: "user"
|
|
398
|
+
password: "123456"
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
- `settings.rpc.list`:RPC 服务端点数组。
|
|
402
|
+
- `id`:节点唯一标识。
|
|
403
|
+
- `type`:通信协议,常见取值 `ws`、`http`、`grpc`、`mqtt`。
|
|
404
|
+
- `server`:监听配置,通常只写 `{ port }`。
|
|
405
|
+
- `extra`:协议扩展参数。
|
|
406
|
+
- `serviceType`:服务类型,用于服务分组或路由。
|
|
407
|
+
- `secure`:安全认证信息,包含 `username` 和 `password`。
|
|
408
|
+
|
|
409
|
+
#### COS 模板特有配置
|
|
410
|
+
|
|
411
|
+
```yaml
|
|
412
|
+
application:
|
|
413
|
+
env: "prod"
|
|
414
|
+
|
|
415
|
+
settings:
|
|
416
|
+
hotterSysConfig: true # 启用系统配置热更新监听
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
- `settings.hotterSysConfig`:设为 `true` 时,框架会监听配置变更并自动热更新。
|
|
420
|
+
|
|
421
|
+
#### Microservices 模板配置
|
|
422
|
+
|
|
423
|
+
主配置通常只声明环境:
|
|
424
|
+
|
|
425
|
+
```yaml
|
|
426
|
+
application:
|
|
427
|
+
env: "dev"
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
详细集群配置放在 `application-dev.yml`:
|
|
431
|
+
|
|
432
|
+
```yaml
|
|
433
|
+
settings:
|
|
434
|
+
hotterSysConfig: true # 监听系统配置变更
|
|
435
|
+
|
|
436
|
+
microservices:
|
|
437
|
+
center: # 服务中心
|
|
438
|
+
token: "nW0tT4bZ6qM7mF7wD2rT2pR9dT7gK3hZ"
|
|
439
|
+
servers:
|
|
440
|
+
- host: "localhost"
|
|
441
|
+
clusters: 1 # 实例数,serviceId 和端口号自动递增
|
|
442
|
+
list:
|
|
443
|
+
- type: "ws"
|
|
444
|
+
server: { port: 60000 }
|
|
445
|
+
timeout: 0 # 0 表示永不超时
|
|
446
|
+
connectionLimit: 1
|
|
447
|
+
disconnectInterval: 1000 # 断线重连间隔(毫秒)
|
|
448
|
+
retry:
|
|
449
|
+
retryCount: 3
|
|
450
|
+
retryInterval: 3000
|
|
451
|
+
timeout: 30000
|
|
452
|
+
maxMsgNum: 10000
|
|
453
|
+
increase: true
|
|
454
|
+
|
|
455
|
+
connector: # 连接器服务
|
|
456
|
+
token: "x3TGsWC9uloZu235LA07eAiJ61nQ1A5f"
|
|
457
|
+
servers:
|
|
458
|
+
- host: "localhost"
|
|
459
|
+
clusters: 1
|
|
460
|
+
list:
|
|
461
|
+
- front: true # 标记为面向客户端的前置节点
|
|
462
|
+
type: "ws"
|
|
463
|
+
server: { port: 60100 }
|
|
464
|
+
|
|
465
|
+
chat: # 聊天服务
|
|
466
|
+
token: "go0kbkNM3wQ4e2Vgo0kbkNM3wQ4e2V"
|
|
467
|
+
servers:
|
|
468
|
+
- host: "localhost"
|
|
469
|
+
clusters: 1
|
|
470
|
+
list:
|
|
471
|
+
- type: "ws"
|
|
472
|
+
server: { port: 60200 }
|
|
473
|
+
|
|
474
|
+
web: # Web 服务
|
|
475
|
+
token: "go0kbkNM3wQ4e2Vgo0kbkNM3wQ4e2V"
|
|
476
|
+
koa:
|
|
477
|
+
koaBodyParser:
|
|
478
|
+
enableTypes: ["json", "form", "text"]
|
|
479
|
+
extendTypes: { text: ["text/xml", "application/xml"] }
|
|
480
|
+
servers:
|
|
481
|
+
- host: "localhost"
|
|
482
|
+
clusters: 1
|
|
483
|
+
list:
|
|
484
|
+
- type: "http"
|
|
485
|
+
server: { port: 8080 }
|
|
486
|
+
- type: "ws"
|
|
487
|
+
server: { port: 60300 }
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
- `settings.microservices.<服务名>`:定义各微服务模块的集群配置。
|
|
491
|
+
- `token`:服务间通信鉴权令牌,防止非法节点接入。
|
|
492
|
+
- `servers`:服务器集群列表。
|
|
493
|
+
- `host`:主机地址。
|
|
494
|
+
- `clusters`:集群实例数量。若大于 1,框架会自动递增 `serviceId` 和端口号生成多个实例。
|
|
495
|
+
- `list`:该集群对外暴露的协议端点列表。
|
|
496
|
+
- `type`:协议类型,如 `ws`、`http`。
|
|
497
|
+
- `server`:端口配置 `{ port }`。
|
|
498
|
+
- `front: true`:仅 connector 等前置服务需要,表示该节点直接面向客户端。
|
|
499
|
+
- `timeout`:连接超时时间(毫秒),`0` 表示永不超时。
|
|
500
|
+
- `connectionLimit`:最大连接数限制。
|
|
501
|
+
- `disconnectInterval`:断线后重连间隔(毫秒)。
|
|
502
|
+
- `retry`:消息重试策略。
|
|
503
|
+
- `retryCount`:最大重试次数。
|
|
504
|
+
- `retryInterval`:重试间隔。
|
|
505
|
+
- `timeout`:重试总超时。
|
|
506
|
+
- `maxMsgNum`:消息队列最大长度。
|
|
507
|
+
- `increase`:是否递增重试间隔。
|
|
508
|
+
|
|
509
|
+
## 生命周期钩子
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
import { ApplicationStart, ApplicationStop, ApplicationInit } from "@fastcar/core/annotation";
|
|
513
|
+
|
|
514
|
+
@Component
|
|
515
|
+
class LifecycleService {
|
|
516
|
+
// 应用启动时执行
|
|
517
|
+
@ApplicationStart
|
|
518
|
+
async onStart() {
|
|
519
|
+
console.log("应用启动");
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// 应用停止前执行
|
|
523
|
+
@ApplicationStop
|
|
524
|
+
async onStop() {
|
|
525
|
+
console.log("应用停止");
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 初始化(配合 @ApplicationRunner)
|
|
529
|
+
@ApplicationInit
|
|
530
|
+
async init() {
|
|
531
|
+
console.log("初始化完成");
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
## 表单验证
|
|
537
|
+
|
|
538
|
+
```typescript
|
|
539
|
+
import { ValidForm, NotNull, Size, Rule } from "@fastcar/core/annotation";
|
|
540
|
+
|
|
541
|
+
class UserDTO {
|
|
542
|
+
@NotNull
|
|
543
|
+
name!: string;
|
|
544
|
+
|
|
545
|
+
@Size({ minSize: 1, maxSize: 150 })
|
|
546
|
+
age!: number;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
@Controller
|
|
550
|
+
class UserController {
|
|
551
|
+
@ValidForm
|
|
552
|
+
createUser(@Rule() @NotNull user: UserDTO) {
|
|
553
|
+
// 参数自动校验
|
|
554
|
+
return this.userService.create(user);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
## 工具类
|
|
560
|
+
|
|
561
|
+
```typescript
|
|
562
|
+
import { DateUtil, CryptoUtil, FileUtil, TypeUtil } from "@fastcar/core/utils";
|
|
563
|
+
|
|
564
|
+
// 日期时间
|
|
565
|
+
DateUtil.toDateTime(); // "2024-03-10 15:30:45"
|
|
566
|
+
DateUtil.toDay(); // "2024-03-10"
|
|
567
|
+
|
|
568
|
+
// 加密
|
|
569
|
+
CryptoUtil.aesEncode(key, iv, "data");
|
|
570
|
+
CryptoUtil.sha256Encode("password");
|
|
571
|
+
|
|
572
|
+
// 文件操作
|
|
573
|
+
FileUtil.getFilePathList("./src");
|
|
574
|
+
FileUtil.getResource("./config.yml");
|
|
575
|
+
|
|
576
|
+
// 类型判断
|
|
577
|
+
TypeUtil.isFunction(() => {}); // true
|
|
578
|
+
TypeUtil.isClass(MyClass); // true
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
## 完整模块列表
|
|
582
|
+
|
|
583
|
+
| 模块 | 安装命令 | 用途 |
|
|
584
|
+
|------|----------|------|
|
|
585
|
+
| @fastcar/core | `npm i @fastcar/core` | IoC 容器、配置管理 |
|
|
586
|
+
| @fastcar/koa | `npm i @fastcar/koa @fastcar/server` | Web 开发 |
|
|
587
|
+
| @fastcar/mysql | `npm i @fastcar/mysql` | MySQL 数据库 |
|
|
588
|
+
| @fastcar/pgsql | `npm i @fastcar/pgsql` | PostgreSQL |
|
|
589
|
+
| @fastcar/mongo | `npm i @fastcar/mongo` | MongoDB |
|
|
590
|
+
| @fastcar/redis | `npm i @fastcar/redis` | Redis 缓存 |
|
|
591
|
+
| @fastcar/cache | `npm i @fastcar/cache` | 缓存组件 |
|
|
592
|
+
| @fastcar/timer | `npm i @fastcar/timer` | 定时任务 |
|
|
593
|
+
| @fastcar/timewheel | `npm i @fastcar/timewheel` | 时间轮延时任务 |
|
|
594
|
+
| @fastcar/workerpool | `npm i @fastcar/workerpool` | 工作线程池 |
|
|
595
|
+
| @fastcar/rpc | `npm i @fastcar/rpc` | RPC 通信 |
|
|
596
|
+
| @fastcar/serverless | `npm i @fastcar/serverless` | Serverless 支持 |
|
|
597
|
+
| @fastcar/cos-sdk | `npm i @fastcar/cos-sdk` | 对象存储 |
|
|
598
|
+
|
|
599
|
+
## 快速开始新项目
|
|
600
|
+
|
|
601
|
+
### 使用 CLI 创建项目(推荐)
|
|
602
|
+
|
|
603
|
+
```bash
|
|
604
|
+
# Web 项目
|
|
605
|
+
mkdir my-web-app && cd my-web-app
|
|
606
|
+
fastcar-cli init web
|
|
607
|
+
npm install
|
|
608
|
+
npm run debug
|
|
609
|
+
|
|
610
|
+
# Static 项目
|
|
611
|
+
mkdir my-static-app && cd my-static-app
|
|
612
|
+
fastcar-cli init static
|
|
613
|
+
npm install
|
|
614
|
+
npm run debug
|
|
615
|
+
|
|
616
|
+
# RPC 项目
|
|
617
|
+
mkdir my-rpc-app && cd my-rpc-app
|
|
618
|
+
fastcar-cli init rpc
|
|
619
|
+
npm install
|
|
620
|
+
npm run debug
|
|
621
|
+
|
|
622
|
+
# COS 项目
|
|
623
|
+
mkdir my-cos-app && cd my-cos-app
|
|
624
|
+
fastcar-cli init cos
|
|
625
|
+
npm install
|
|
626
|
+
npm run debug
|
|
627
|
+
|
|
628
|
+
# Microservices 项目
|
|
629
|
+
mkdir my-ms-app && cd my-ms-app
|
|
630
|
+
fastcar-cli init microservices
|
|
631
|
+
npm install
|
|
632
|
+
npm run start-node # 单节点模式(子进程启动全部服务)
|
|
633
|
+
# 或
|
|
634
|
+
npm run start-pm2 # PM2 模式
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### 手动创建项目
|
|
638
|
+
|
|
639
|
+
```bash
|
|
640
|
+
# 1. 创建项目
|
|
641
|
+
mkdir my-fastcar-app && cd my-fastcar-app
|
|
642
|
+
npm init -y
|
|
643
|
+
|
|
644
|
+
# 2. 安装依赖
|
|
645
|
+
npm i @fastcar/core @fastcar/koa @fastcar/server
|
|
646
|
+
npm i -D typescript ts-node @types/node
|
|
647
|
+
|
|
648
|
+
# 3. 初始化 TypeScript
|
|
649
|
+
npx tsc --init
|
|
650
|
+
|
|
651
|
+
# 4. 启用装饰器(tsconfig.json)
|
|
652
|
+
# "experimentalDecorators": true
|
|
653
|
+
# "emitDecoratorMetadata": true
|
|
654
|
+
|
|
655
|
+
# 5. 创建入口文件和配置文件,开始开发
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
## 参考资源
|
|
659
|
+
|
|
660
|
+
- 详细 API 文档:[references/api-reference.md](references/api-reference.md)
|
|
661
|
+
- 项目模板:[assets/project-template/](assets/project-template/)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-fastcar-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "FastCar application",
|
|
5
|
+
"main": "dist/app.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"start": "ts-node src/app.ts",
|
|
9
|
+
"dev": "ts-node-dev --respawn src/app.ts",
|
|
10
|
+
"clean": "rm -rf dist"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@fastcar/core": "^0.3.0",
|
|
14
|
+
"@fastcar/koa": "^0.3.0",
|
|
15
|
+
"@fastcar/server": "^0.3.0",
|
|
16
|
+
"reflect-metadata": "^0.2.2"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^20.0.0",
|
|
20
|
+
"ts-node": "^10.9.0",
|
|
21
|
+
"ts-node-dev": "^2.0.0",
|
|
22
|
+
"typescript": "^5.6.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
3
|
+
import { Application, ApplicationStart } from "@fastcar/core/annotation";
|
|
4
|
+
|
|
5
|
+
@Application
|
|
6
|
+
class App {
|
|
7
|
+
app!: FastCarApplication;
|
|
8
|
+
|
|
9
|
+
@ApplicationStart
|
|
10
|
+
async start() {
|
|
11
|
+
console.log("✅ FastCar 应用启动成功!");
|
|
12
|
+
console.log(`📊 内存使用: ${JSON.stringify(this.app.getMemoryUsage())}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 启动应用
|
|
17
|
+
const app = new App();
|
|
18
|
+
app.start();
|