@sisin/egg-client 1.0.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 +267 -0
- package/app/constants/code.js +9 -0
- package/app/constants/crypt.js +10 -0
- package/app/constants/error.js +20 -0
- package/app/constants/redis.js +17 -0
- package/app/controller/authSystem/auth.js +119 -0
- package/app/controller/common/health.js +15 -0
- package/app/controller/home.js +10 -0
- package/app/core/database/database-health.js +39 -0
- package/app/core/database/database-manager.js +43 -0
- package/app/core/database/index.js +19 -0
- package/app/core/database/prisma-logging.js +38 -0
- package/app/core/database/transaction-manager.js +9 -0
- package/app/core/repository/auth/user_repository.js +14 -0
- package/app/core/repository/base-repository.js +26 -0
- package/app/extend/context.js +57 -0
- package/app/extend/helper.js +24 -0
- package/app/middleware/auth.js +16 -0
- package/app/middleware/check_ready.js +11 -0
- package/app/middleware/error_handler.js +28 -0
- package/app/middleware/jwt_auth.js +27 -0
- package/app/middleware/login_limit.js +4 -0
- package/app/middleware/permission.js +8 -0
- package/app/middleware/request_log.js +28 -0
- package/app/middleware/upload_limit.js +17 -0
- package/app/model/redis/redis-auth.js +42 -0
- package/app/redis/index.js +25 -0
- package/app/redis/redis-manager.js +46 -0
- package/app/router/auth.js +17 -0
- package/app/router/common.js +11 -0
- package/app/router/index.js +13 -0
- package/app/router.js +11 -0
- package/app/service/authSystem/auth.js +144 -0
- package/app/service/authSystem/permission.js +4 -0
- package/app/service/authSystem/token.js +4 -0
- package/app/service/redis/auth.js +4 -0
- package/app/utils/common.js +58 -0
- package/app/utils/encrypt.js +33 -0
- package/app/utils/jwt.js +23 -0
- package/app/utils/logger.js +209 -0
- package/app/utils/permission.js +2 -0
- package/app/utils/prisma-manager.js +127 -0
- package/app/utils/prisma.js +21 -0
- package/app/validate/user.js +6 -0
- package/app.js +21 -0
- package/config/config.default.js +137 -0
- package/config/logging.js +26 -0
- package/config/plugin.default.js +3 -0
- package/index.js +22 -0
- package/init/index.js +39 -0
- package/init/init-auth.js +37 -0
- package/init/init-database.js +59 -0
- package/init/init-logger.js +18 -0
- package/init/init-redis.js +26 -0
- package/init/init-websocket.js +10 -0
- package/init/ready.js +8 -0
- package/package.json +60 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// 数据库初始化
|
|
2
|
+
const { initDatabases } = require('../app/core/database');
|
|
3
|
+
const PrismaManager = require('../app/utils/prisma-manager');
|
|
4
|
+
const logger = require('../app/utils/logger');
|
|
5
|
+
|
|
6
|
+
module.exports = async (app) => {
|
|
7
|
+
const dbConfig = app.config.Dbs;
|
|
8
|
+
if (!dbConfig || Object.keys(dbConfig).length === 0) {
|
|
9
|
+
logger.warn('[数据库] 配置为空,跳过数据库初始化');
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
logger.init('[数据库] 正在初始化...');
|
|
15
|
+
|
|
16
|
+
// ====== Prisma 文件生命周期管理 ======
|
|
17
|
+
const pm = new PrismaManager(app);
|
|
18
|
+
if (!pm.isReady()) {
|
|
19
|
+
logger.warn('[数据库] Prisma generated client 缺失,自动同步...');
|
|
20
|
+
pm.sync();
|
|
21
|
+
} else {
|
|
22
|
+
logger.info('[数据库] Prisma generated client 已就绪');
|
|
23
|
+
}
|
|
24
|
+
// ====================================
|
|
25
|
+
|
|
26
|
+
const clients = initDatabases(app);
|
|
27
|
+
|
|
28
|
+
// 测试连通性
|
|
29
|
+
for (const db in clients) {
|
|
30
|
+
if (!clients[db]) continue;
|
|
31
|
+
await clients[db].$connect();
|
|
32
|
+
logger.success('[数据库]' + db + ' 连接成功');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 挂载到 app 和 ctx
|
|
36
|
+
app.Dbs = clients;
|
|
37
|
+
app.context.Dbs = clients;
|
|
38
|
+
logger.success('[数据库] 实例挂载完成');
|
|
39
|
+
|
|
40
|
+
// 应用关闭时释放连接
|
|
41
|
+
app.beforeClose(async () => {
|
|
42
|
+
for (const db in clients) {
|
|
43
|
+
if (!clients[db]) continue;
|
|
44
|
+
try { await clients[db].$disconnect(); } catch (_) {}
|
|
45
|
+
}
|
|
46
|
+
logger.info('[数据库] 连接已关闭');
|
|
47
|
+
});
|
|
48
|
+
} catch (err) {
|
|
49
|
+
logger.error('[数据库] 初始化失败', err);
|
|
50
|
+
// 释放已建立的连接
|
|
51
|
+
if (app.Dbs) {
|
|
52
|
+
for (const db in app.Dbs) {
|
|
53
|
+
if (!app.Dbs[db]) continue;
|
|
54
|
+
try { await app.Dbs[db].$disconnect(); } catch (_) {}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// 挂载日志工具到 app 和 ctx
|
|
2
|
+
module.exports = async (app) => {
|
|
3
|
+
const { createLogger, requestLogger, prismaLogger, appLogger } = require('../app/utils/logger');
|
|
4
|
+
const loggingConfig = app.config.logging;
|
|
5
|
+
|
|
6
|
+
// 根据实际配置创建带文件路径的 logger
|
|
7
|
+
const appLoggerInstance = createLogger(loggingConfig.app);
|
|
8
|
+
const requestLoggerInstance = createLogger(loggingConfig.request);
|
|
9
|
+
const prismaLoggerInstance = createLogger({ level: loggingConfig.prisma.sqlLogLevel, filePath: loggingConfig.prisma.filePath, terminal: loggingConfig.prisma.sqlTerminal, pretty: loggingConfig.prisma.sqlTerminal });
|
|
10
|
+
|
|
11
|
+
app.logger = appLoggerInstance;
|
|
12
|
+
app.context.logger = appLoggerInstance;
|
|
13
|
+
|
|
14
|
+
// 替换模块级 logger 的引用(确保文件写入生效)
|
|
15
|
+
// 由于模块缓存,直接覆盖 exports 无效,通过挂载到 app 上供后续使用
|
|
16
|
+
app.requestLogger = requestLoggerInstance;
|
|
17
|
+
app.prismaLogger = prismaLoggerInstance;
|
|
18
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Redis 初始化
|
|
2
|
+
const redisCreate = require('../app/redis');
|
|
3
|
+
const RedisManager = require('../app/redis/redis-manager');
|
|
4
|
+
const logger = require('../app/utils/logger');
|
|
5
|
+
|
|
6
|
+
module.exports = async (app) => {
|
|
7
|
+
const redisConfig = app.config.redis;
|
|
8
|
+
if (!redisConfig || Object.keys(redisConfig).length === 0) {
|
|
9
|
+
logger.warn('[Redis] 配置为空,跳过 Redis 初始化');
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
logger.init('[Redis] 正在初始化...');
|
|
14
|
+
const clients = await redisCreate(app);
|
|
15
|
+
for (const name in clients) { await clients[name].connect(); }
|
|
16
|
+
app.redis = new RedisManager(clients);
|
|
17
|
+
app._redisClients = clients;
|
|
18
|
+
logger.success('[Redis] 实例挂载完成');
|
|
19
|
+
|
|
20
|
+
app.beforeClose(async () => {
|
|
21
|
+
for (const name in clients) {
|
|
22
|
+
try { await clients[name].quit(); } catch (_) { clients[name].disconnect(); }
|
|
23
|
+
}
|
|
24
|
+
logger.success('[Redis] 连接关闭');
|
|
25
|
+
});
|
|
26
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// WebSocket 初始化
|
|
2
|
+
const WebSocketServer = require('ws');
|
|
3
|
+
|
|
4
|
+
module.exports = (app) => {
|
|
5
|
+
const config = app.config.websocket;
|
|
6
|
+
if (!config || !config.enable) return;
|
|
7
|
+
const wss = new WebSocketServer.Server({ server: app.server, path: '/ws' });
|
|
8
|
+
wss.on('connection', (ws) => { console.log('WebSocket connected'); });
|
|
9
|
+
app.websocket = wss;
|
|
10
|
+
};
|
package/init/ready.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sisin/egg-client",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Egg.js 核心框架包 — 封装通用中间件、数据层、认证、日志、Redis 等基础设施",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"app.js",
|
|
9
|
+
"app/",
|
|
10
|
+
"config/",
|
|
11
|
+
"init/"
|
|
12
|
+
],
|
|
13
|
+
"egg": {
|
|
14
|
+
"framework": true
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "egg-bin test",
|
|
18
|
+
"test:unit": "mocha test/unit/*.test.js test/unit/**/*.test.js --timeout 10000",
|
|
19
|
+
"test:cov": "egg-bin cov",
|
|
20
|
+
"prisma:pull": "npx prisma db pull --config prisma.%npm_config_key%.config.ts",
|
|
21
|
+
"prisma:generate": "npx prisma generate --config prisma.%npm_config_key%.config.ts",
|
|
22
|
+
"publish:patch": "npm version patch && npm publish",
|
|
23
|
+
"publish:minor": "npm version minor && npm publish",
|
|
24
|
+
"publish:major": "npm version major && npm publish"
|
|
25
|
+
},
|
|
26
|
+
"keywords": ["egg", "egg-framework", "egg-client", "niceguy"],
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/niceguy802/niceguy-core-web.git",
|
|
30
|
+
"directory": "packages/egg-client"
|
|
31
|
+
},
|
|
32
|
+
"author": "niceguy",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@prisma/adapter-mariadb": "^7.8.0",
|
|
36
|
+
"@prisma/client": "^7.8.0",
|
|
37
|
+
"bcryptjs": "^3.0.3",
|
|
38
|
+
"dayjs": "^1.11.20",
|
|
39
|
+
"ioredis": "^5.10.1",
|
|
40
|
+
"jsonwebtoken": "^9.0.3",
|
|
41
|
+
"mysql2": "^3.22.3",
|
|
42
|
+
"pino": "^10.3.1",
|
|
43
|
+
"pino-http": "^11.0.0",
|
|
44
|
+
"pino-pretty": "^13.1.3",
|
|
45
|
+
"pino-roll": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"egg": ">=3.0.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"egg": "^3.17.5",
|
|
52
|
+
"egg-bin": "^6",
|
|
53
|
+
"egg-mock": "^5",
|
|
54
|
+
"mocha": "^10",
|
|
55
|
+
"prisma": "^7.8.0"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|