-
Notifications
You must be signed in to change notification settings - Fork 0
/
@codebase
403 lines (335 loc) · 7.11 KB
/
@codebase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# @codebase文件说明
此文件用于Cursor编辑器的AI辅助功能:
1. 为AI提供项目上下文信息
2. 辅助代码补全和生成
3. 提供项目结构参考
4. 作为开发指南
# Flutter Frontend
# 项目结构
## 前端 (Flutter)
- lib/models: 数据模型
- lib/views: UI视图
- lib/services: 服务类
- user_service.dart
- text_service.dart
- tts_service.dart
- reading_history_service.dart
- lib/utils: 工具类
- lib/main.dart: 应用入口
- lib/views/screens
- reading_history_screen.dart
- auth_page.dart
- home_page.dart
## 后端 (Node.js)
- src/models
- user.model.js
- text.model.js
- reading_history.model.js
- src/routes
- user.routes.js
- text.routes.js
- reading_history.routes.js
- src/index.js
- src: 源代码
- tests: 测试代码
# 环境要求
- Flutter 3.x
- Node.js 16+
- MongoDB 4.4+
- Docker (可选)
## pubspec.yaml
# Flutter项目的依赖配置文件,定义项目信息和依赖项
name: ai_reading_app
description: AI powered reading app
version: 1.0.0+1
environment:
sdk: ">=2.17.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# 状态管理库
get: ^4.6.5
# 文本转语音库
flutter_tts: ^3.5.0
# 本地数据存储
hive: ^2.2.3
# 依赖注入
provider: ^6.0.3
# 网络请求
http: ^0.13.5
# 文件路径管理
path_provider: ^2.0.11
# 本地配置存储
shared_preferences: ^2.0.15
# PDF文件渲染
pdf_render: ^1.3.0
# EPUB文件支持
epub_view: ^3.0.0
## lib/main.dart
# 应用程序入口文件,配置全局设置和主题
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'AI Reading App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
## lib/models/user.dart
# 用户数据模型,定义用户相关的数据结构
class User {
final String id;
final String username;
final String email;
final UserSettings settings;
// 构造函数
User({
required this.id,
required this.username,
required this.email,
required this.settings,
});
// JSON序列化方法
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
username: json['username'],
email: json['email'],
settings: UserSettings.fromJson(json['settings']),
);
}
}
## lib/services/api_service.dart
# API服务类,处理与后端的通信
class ApiService extends GetxService {
final String baseUrl = 'http://localhost:3000/api';
// GET请求方法
Future<Response> get(String path) async {
try {
return await GetConnect().get('$baseUrl/$path');
} catch (e) {
throw Exception('API Error: $e');
}
}
// POST请求方法
Future<Response> post(String path, dynamic data) async {
try {
return await GetConnect().post('$baseUrl/$path', data);
} catch (e) {
throw Exception('API Error: $e');
}
}
}
# Node.js Backend
## package.json
# Node.js项目配置文件,定义项目依赖和脚本
{
"name": "ai-reading-backend",
"version": "1.0.0",
"main": "src/index.ts",
"scripts": {
"start": "node dist/index.js",
"dev": "ts-node-dev src/index.ts",
"build": "tsc",
"test": "jest"
},
"dependencies": {
// Web框架
"express": "^4.18.2",
// 数据库ODM
"mongoose": "^6.7.0",
// 身份认证
"jsonwebtoken": "^8.5.1",
// 密码加密
"bcrypt": "^5.1.0",
// 跨域支持
"cors": "^2.8.5",
// 环境变量
"dotenv": "^16.0.3",
// TypeScript支持
"typescript": "^4.8.4"
},
"devDependencies": {
// 类型定义文件
"@types/express": "^4.17.14",
"@types/jest": "^29.2.0",
// 测试框架
"jest": "^29.2.2",
"ts-jest": "^29.0.3",
// 开发服务器
"ts-node-dev": "^2.0.0"
}
}
## src/index.ts
# 后端应用程序入口文件
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
import dotenv from 'dotenv';
// 加载环境变量
dotenv.config();
const app = express();
// 中间件配置
app.use(cors());
app.use(express.json());
// 数据库连接
mongoose.connect(process.env.MONGODB_URI as string)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoDB connection error:', err));
// 启动服务器
app.listen(3000, () => {
console.log('Server running on port 3000');
});
## src/models/user.model.ts
# 用户数据模型定义
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
settings: {
voiceModel: String,
speed: Number,
pitch: Number
}
});
export const User = mongoose.model('User', userSchema);
# Docker Configuration
## docker-compose.yml
# Docker容器编排配置文件
version: '3'
services:
# 前端服务配置
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
"80:80"
environment:
API_URL=http://backend:3000
# 后端服务配置
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
"3000:3000"
environment:
MONGODB_URI=mongodb://mongodb:27017/aireading
depends_on:
mongodb
# MongoDB数据库配置
mongodb:
image: mongo:4.4
ports:
"27017:27017"
volumes:
mongodb_data:/data/db
volumes:
mongodb_data:
## frontend/Dockerfile
# 前端Docker构建文件
FROM nginx:alpine
COPY build/web /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
## backend/Dockerfile
# 后端Docker构建文件
FROM node:16-alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
# Configuration Files
## .env.example
# 环境变量示例文件
MongoDB连接URI
MONGODB_URI=mongodb://localhost:27017/aireading
JWT密钥
JWT_SECRET=your_jwt_secret
AI API密钥
AI_API_KEY=your_ai_api_key
## .gitignore
# Git忽略文件配置
Flutter相关
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
build/
ios/Pods/
android/.gradle/
Node.js相关
node_modules/
dist/
.env
IDE相关
.idea/
.vscode/
.iml
其他
.DS_Store
.log
## tsconfig.json
# TypeScript配置文件
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src//"],
"exclude": ["node_modules", "/.test.ts"]
}
# Testing
## frontend/test/widget_test.dart
# Flutter部件测试文件
import 'package:flutter_test/flutter_test.dart';
import 'package:ai_reading_app/main.dart';
void main() {
testWidgets('App should render', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.text('AI Reading App'), findsOneWidget);
});
}
## backend/tests/user.test.ts
# 后端用户模块测试文件
import { User } from '../src/models/user.model';
import mongoose from 'mongoose';
// 测试前连接数据库
beforeAll(async () => {
await mongoose.connect(process.env.MONGODB_URI as string);
});
// 测试后关闭数据库连接
afterAll(async () => {
await mongoose.connection.close();
});
// 用户模型测试用例
describe('User Model Test', () => {
it('should create & save user successfully', async () => {
const user = new User({
username: 'test',
email: 'test@test.com',
password: 'password123'
});
const savedUser = await user.save();
expect(savedUser.username).toBe('test');
});
});