env-plugin 0.6.0 → 0.7.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/dist/Container.js +20 -1
- package/dist/PostProxyServer.js +37 -12
- package/dist/client/assets/element-plus-BN_JcO6B.js +19 -0
- package/dist/client/assets/element-plus-SrDBYmqw.css +1 -0
- package/dist/client/assets/index-CcOgzo-G.js +1 -0
- package/dist/client/assets/index-DcxBCPsx.css +1 -0
- package/dist/client/index.html +4 -4
- package/dist/controllers/DevServerController.js +10 -2
- package/dist/controllers/EnvController.js +43 -3
- package/dist/controllers/ImportExportController.js +44 -0
- package/dist/controllers/PasswordController.js +1 -0
- package/dist/controllers/RequestLogController.js +34 -0
- package/dist/controllers/RouteRuleController.js +1 -0
- package/dist/controllers/SystemSettingController.js +43 -0
- package/dist/index.js +13 -11
- package/dist/models/EnvModel.js +31 -3
- package/dist/repositories/DevServerRepo.js +14 -69
- package/dist/repositories/EnvRepo.js +30 -95
- package/dist/repositories/PasswordRepo.js +44 -106
- package/dist/repositories/RouteRuleRepo.js +26 -79
- package/dist/repositories/SystemSettingRepo.js +21 -0
- package/dist/repositories/database.js +11 -84
- package/dist/repositories/migrate.js +198 -0
- package/dist/repositories/store.js +118 -0
- package/dist/routes/index.js +197 -29
- package/dist/service/DevServerService.js +4 -2
- package/dist/service/EnvService.js +67 -7
- package/dist/service/ImportExportService.js +337 -0
- package/dist/service/PasswordService.js +11 -9
- package/dist/service/PreProxyServer.js +158 -36
- package/dist/service/ProxyAutoStarterService.js +3 -0
- package/dist/service/RequestLogService.js +77 -0
- package/dist/service/RouteRuleService.js +18 -16
- package/dist/service/SystemSettingService.js +90 -0
- package/dist/types/index.js +3 -0
- package/dist/types/shared/EnvItem.js +8 -0
- package/dist/types/shared/ImportExport.js +55 -0
- package/dist/types/shared/RequestLog.js +28 -0
- package/dist/types/shared/SystemSetting.js +18 -0
- package/dist/utils/ResolveConfig.js +4 -2
- package/dist/utils/asyncHandler.js +12 -0
- package/dist/utils/errors.js +2 -0
- package/dist/utils/resourceType.js +42 -0
- package/dist/utils/routeBuilder.js +19 -0
- package/package.json +36 -29
- package/templates/inject/envm-switcher.js +1418 -0
- package/dist/client/assets/element-plus-4FTV-s7u.js.gz +0 -0
- package/dist/client/assets/index-C2AH0x8U.css.gz +0 -0
- package/dist/client/assets/index-Db97pusF.js.gz +0 -0
- package/dist/client/assets/vue-CRjr1oeH.js.gz +0 -0
package/dist/Container.js
CHANGED
|
@@ -10,10 +10,18 @@ import { RouteRuleRepo } from "./repositories/RouteRuleRepo.js";
|
|
|
10
10
|
import { RouteRuleService } from "./service/RouteRuleService.js";
|
|
11
11
|
import { PasswordRepo } from "./repositories/PasswordRepo.js";
|
|
12
12
|
import { PasswordService } from "./service/PasswordService.js";
|
|
13
|
+
import { RequestLogService } from "./service/RequestLogService.js";
|
|
14
|
+
import { RequestLogController } from "./controllers/RequestLogController.js";
|
|
15
|
+
import { ImportExportService } from "./service/ImportExportService.js";
|
|
16
|
+
import { ImportExportController } from "./controllers/ImportExportController.js";
|
|
17
|
+
import { SystemSettingRepo } from "./repositories/SystemSettingRepo.js";
|
|
18
|
+
import { SystemSettingService } from "./service/SystemSettingService.js";
|
|
19
|
+
import { SystemSettingController } from "./controllers/SystemSettingController.js";
|
|
13
20
|
import { ProxyAutoStarter } from "./service/ProxyAutoStarterService.js";
|
|
14
21
|
class Container {
|
|
22
|
+
static instance;
|
|
23
|
+
dependencies = new Map();
|
|
15
24
|
constructor() {
|
|
16
|
-
this.dependencies = new Map();
|
|
17
25
|
// 环境和服务的注册
|
|
18
26
|
const envRepo = new EnvRepo();
|
|
19
27
|
const devServerRepo = new DevServerRepo();
|
|
@@ -31,6 +39,17 @@ class Container {
|
|
|
31
39
|
// 密码服务和控制器的注册
|
|
32
40
|
this.register("passwordService", new PasswordService(passwordRepo));
|
|
33
41
|
this.register("passwordController", new PasswordController(this.get("passwordService")));
|
|
42
|
+
// 请求日志服务和控制器
|
|
43
|
+
const requestLogService = new RequestLogService();
|
|
44
|
+
this.register("requestLogService", requestLogService);
|
|
45
|
+
this.register("requestLogController", new RequestLogController(requestLogService));
|
|
46
|
+
// 导入导出服务和控制器
|
|
47
|
+
this.register("importExportService", new ImportExportService(envRepo, devServerRepo, routeRuleRepo, passwordRepo));
|
|
48
|
+
this.register("importExportController", new ImportExportController(this.get("importExportService")));
|
|
49
|
+
// 系统设置服务和控制器
|
|
50
|
+
const systemSettingService = new SystemSettingService(new SystemSettingRepo());
|
|
51
|
+
this.register("systemSettingService", systemSettingService);
|
|
52
|
+
this.register("systemSettingController", new SystemSettingController(systemSettingService));
|
|
34
53
|
setTimeout(() => {
|
|
35
54
|
new ProxyAutoStarter(envRepo, this.get("envService"), routeRuleRepo);
|
|
36
55
|
}, 5000);
|
package/dist/PostProxyServer.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { join } from "path";
|
|
2
|
-
import { WebSocketServer } from "ws";
|
|
2
|
+
import { WebSocketServer, WebSocket } from "ws";
|
|
3
3
|
import express from "express";
|
|
4
|
-
import expressStaticGzip from "express-static-gzip";
|
|
5
4
|
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
6
5
|
import { getConfig } from "./utils/ResolveConfig.js";
|
|
7
6
|
import { createRouter } from "./routes/index.js";
|
|
@@ -14,11 +13,14 @@ const __filename = fileURLToPath(currentModuleUrl); // 当前文件的完整路
|
|
|
14
13
|
// 获取目录路径
|
|
15
14
|
import { dirname } from "path";
|
|
16
15
|
const __dirname = dirname(__filename); // 当前文件所在的目录路径
|
|
17
|
-
import {
|
|
16
|
+
import { Container } from "./Container.js";
|
|
17
|
+
import { envLogger, proxyLogger } from "./utils/logger.js";
|
|
18
18
|
/**
|
|
19
19
|
* 后置代理服务器---同时也是管理页面的服务器
|
|
20
20
|
*/
|
|
21
21
|
class PostProxyServer {
|
|
22
|
+
app;
|
|
23
|
+
wss;
|
|
22
24
|
get config() {
|
|
23
25
|
return getConfig();
|
|
24
26
|
}
|
|
@@ -27,9 +29,11 @@ class PostProxyServer {
|
|
|
27
29
|
// 代理中间件
|
|
28
30
|
app.use(this.createPostProxyMiddleware());
|
|
29
31
|
// 静态资源
|
|
30
|
-
app.use(
|
|
32
|
+
app.use(express.static(join(__dirname, "client")));
|
|
31
33
|
// 注入资源
|
|
32
|
-
|
|
34
|
+
// 优先从用户自定义目录加载(如 .envm/),找不到时回退到内置 templates/inject/
|
|
35
|
+
app.use("/envm-inject", express.static(join(process.cwd(), this.config.injectScriptDir || "")));
|
|
36
|
+
app.use("/envm-inject", express.static(join(__dirname, "..", "templates", "inject")));
|
|
33
37
|
// 统一处理响应
|
|
34
38
|
app.use(responseEnhancer);
|
|
35
39
|
// 初始化管理路由
|
|
@@ -49,16 +53,40 @@ class PostProxyServer {
|
|
|
49
53
|
});
|
|
50
54
|
}
|
|
51
55
|
/**
|
|
52
|
-
* 注册
|
|
56
|
+
* 注册 WebSocket 服务用于通知客户端更新和推送请求日志
|
|
53
57
|
* @param server
|
|
54
58
|
*/
|
|
55
59
|
registerWs(server) {
|
|
56
|
-
|
|
60
|
+
this.wss = new WebSocketServer({ noServer: true });
|
|
57
61
|
server.on("upgrade", (request, socket, head) => {
|
|
58
62
|
if (request?.url?.startsWith(this.config.apiPrefix)) {
|
|
59
|
-
wss.handleUpgrade(request, socket, head, () => {
|
|
63
|
+
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
64
|
+
this.wss.emit("connection", ws, request);
|
|
65
|
+
});
|
|
60
66
|
}
|
|
61
67
|
});
|
|
68
|
+
this.wss.on("connection", (ws) => {
|
|
69
|
+
proxyLogger.info("WebSocket 客户端已连接");
|
|
70
|
+
// 获取日志服务(Container 已由 createRouter() 初始化)
|
|
71
|
+
const container = Container.getInstance();
|
|
72
|
+
const logService = container.get("requestLogService");
|
|
73
|
+
// 连接时发送全量历史日志
|
|
74
|
+
const history = logService.getAllLogs();
|
|
75
|
+
ws.send(JSON.stringify({ action: "requestlog_history", data: history }));
|
|
76
|
+
// 订阅增量日志推送
|
|
77
|
+
const unsubscribe = logService.subscribe((entry) => {
|
|
78
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
79
|
+
ws.send(JSON.stringify({ action: "requestlog", data: entry }));
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
ws.on("close", () => {
|
|
83
|
+
unsubscribe();
|
|
84
|
+
proxyLogger.info("WebSocket 客户端已断开");
|
|
85
|
+
});
|
|
86
|
+
ws.on("error", (err) => {
|
|
87
|
+
proxyLogger.error(err, "WebSocket 连接错误");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
62
90
|
}
|
|
63
91
|
/**
|
|
64
92
|
* 创建后置服务器 代理转发中间件
|
|
@@ -74,10 +102,7 @@ class PostProxyServer {
|
|
|
74
102
|
ws: true,
|
|
75
103
|
changeOrigin: true,
|
|
76
104
|
router: async (req) => {
|
|
77
|
-
|
|
78
|
-
const target = req.headers["x-api-server"];
|
|
79
|
-
return target;
|
|
80
|
-
}
|
|
105
|
+
return req.headers["x-api-server"];
|
|
81
106
|
},
|
|
82
107
|
});
|
|
83
108
|
}
|