env-plugin 0.5.6

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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/bin/index.js +23 -0
  3. package/dist/Container.js +55 -0
  4. package/dist/Plugin.js +25 -0
  5. package/dist/PostProxyServer.js +85 -0
  6. package/dist/client/assets/element-plus-wndYbaZY.js.gz +0 -0
  7. package/dist/client/assets/index-Bl3B2M1W.js.gz +0 -0
  8. package/dist/client/assets/index-nrcddKJg.css.gz +0 -0
  9. package/dist/client/assets/vue-BGFZjnqd.js.gz +0 -0
  10. package/dist/client/favicon.ico +0 -0
  11. package/dist/client/index.html +16 -0
  12. package/dist/constants/responseCode.js +27 -0
  13. package/dist/controllers/DevServerController.js +102 -0
  14. package/dist/controllers/EnvController.js +177 -0
  15. package/dist/controllers/PasswordController.js +106 -0
  16. package/dist/controllers/RouteRuleController.js +106 -0
  17. package/dist/dto/BaseRes.js +1 -0
  18. package/dist/dto/EnvDTO.js +4 -0
  19. package/dist/index.js +108 -0
  20. package/dist/middleware/dto.middleware.js +38 -0
  21. package/dist/middleware/globalErrorHandler.js +42 -0
  22. package/dist/middleware/responseEnhancer.js +52 -0
  23. package/dist/models/DevServerModel.js +1 -0
  24. package/dist/models/EnvModel.js +14 -0
  25. package/dist/repositories/DevServerRepo.js +101 -0
  26. package/dist/repositories/EnvRepo.js +121 -0
  27. package/dist/repositories/PasswordRepo.js +142 -0
  28. package/dist/repositories/RouteRuleRepo.js +106 -0
  29. package/dist/repositories/database.js +95 -0
  30. package/dist/routes/index.js +78 -0
  31. package/dist/service/DevServerService.js +189 -0
  32. package/dist/service/EnvService.js +214 -0
  33. package/dist/service/PasswordService.js +100 -0
  34. package/dist/service/PreProxyServer.js +344 -0
  35. package/dist/service/ProxyAutoStarterService.js +62 -0
  36. package/dist/service/RouteRuleService.js +131 -0
  37. package/dist/types/index.js +5 -0
  38. package/dist/types/shared/BaseRes.js +36 -0
  39. package/dist/types/shared/DevServerItem.js +51 -0
  40. package/dist/types/shared/EnvItem.js +57 -0
  41. package/dist/types/shared/EnvmConfig.js +17 -0
  42. package/dist/types/shared/ListRes.js +1 -0
  43. package/dist/types/shared/Password.js +48 -0
  44. package/dist/types/shared/RouteRule.js +49 -0
  45. package/dist/utils/ResolveConfig.js +79 -0
  46. package/dist/utils/errors.js +8 -0
  47. package/dist/utils/logger.js +93 -0
  48. package/package.json +78 -0
  49. package/readme.md +214 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 lumos934
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/index.js ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { program } from "commander";
4
+ import { EnvManage } from "../dist/index.js";
5
+
6
+ program
7
+ .description("Environment management tool")
8
+ .name("envm")
9
+ // 添加命令行选项
10
+ .option("-p, --port <number>", "specify the port number", Number)
11
+ .option("-a, --api-prefix <string>", "specify the API prefix", String)
12
+ .option("-c, --cookie-suffix <string>", "specify the cookie suffix", String)
13
+ .option(
14
+ "-l, --log-level <string>",
15
+ "specify the log level (info, debug, warn, error)",
16
+ String
17
+ )
18
+ .action((options) => {
19
+ const envMangePlugin = new EnvManage(options);
20
+ envMangePlugin.startIndependent();
21
+ });
22
+
23
+ program.parse();
@@ -0,0 +1,55 @@
1
+ import { EnvController } from "./controllers/EnvController.js";
2
+ import { DevServerController } from "./controllers/DevServerController.js";
3
+ import { RouteRuleController } from "./controllers/RouteRuleController.js";
4
+ import { PasswordController } from "./controllers/PasswordController.js";
5
+ import { EnvRepo } from "./repositories/EnvRepo.js";
6
+ import { EnvService } from "./service/EnvService.js";
7
+ import { DevServerService } from "./service/DevServerService.js";
8
+ import { DevServerRepo } from "./repositories/DevServerRepo.js";
9
+ import { RouteRuleRepo } from "./repositories/RouteRuleRepo.js";
10
+ import { RouteRuleService } from "./service/RouteRuleService.js";
11
+ import { PasswordRepo } from "./repositories/PasswordRepo.js";
12
+ import { PasswordService } from "./service/PasswordService.js";
13
+ import { ProxyAutoStarter } from "./service/ProxyAutoStarterService.js";
14
+ class Container {
15
+ constructor() {
16
+ this.dependencies = new Map();
17
+ // 环境和服务的注册
18
+ const envRepo = new EnvRepo();
19
+ const devServerRepo = new DevServerRepo();
20
+ const routeRuleRepo = new RouteRuleRepo();
21
+ const passwordRepo = new PasswordRepo();
22
+ // configIns.initConfig();
23
+ this.register("envService", new EnvService(envRepo, devServerRepo, routeRuleRepo));
24
+ this.register("envController", new EnvController(this.get("envService")));
25
+ // 开发服务器服务和控制器的注册
26
+ this.register("devServerService", new DevServerService(devServerRepo, envRepo));
27
+ this.register("devServerController", new DevServerController(this.get("devServerService")));
28
+ // 路由规则服务和控制器的注册
29
+ this.register("routeRuleService", new RouteRuleService(routeRuleRepo, envRepo));
30
+ this.register("routeRuleController", new RouteRuleController(this.get("routeRuleService")));
31
+ // 密码服务和控制器的注册
32
+ this.register("passwordService", new PasswordService(passwordRepo));
33
+ this.register("passwordController", new PasswordController(this.get("passwordService")));
34
+ setTimeout(() => {
35
+ new ProxyAutoStarter(envRepo, this.get("envService"), routeRuleRepo);
36
+ }, 5000);
37
+ }
38
+ static getInstance() {
39
+ if (!this.instance) {
40
+ this.instance = new Container();
41
+ }
42
+ return this.instance;
43
+ }
44
+ register(key, value) {
45
+ this.dependencies.set(key, value);
46
+ return this;
47
+ }
48
+ get(key) {
49
+ if (!this.dependencies.has(key)) {
50
+ throw new Error(`Dependency ${key} not found`);
51
+ }
52
+ return this.dependencies.get(key);
53
+ }
54
+ }
55
+ export { Container };
package/dist/Plugin.js ADDED
@@ -0,0 +1,25 @@
1
+ import { createUnplugin } from "unplugin";
2
+ import { EnvManage } from "./index.js";
3
+ let hasBeenCalled = false;
4
+ export const unpluginFactory = (options) => {
5
+ const envMangePlugin = new EnvManage(options);
6
+ return {
7
+ name: "unplugin-envm",
8
+ buildStart() {
9
+ if (hasBeenCalled) {
10
+ return;
11
+ }
12
+ hasBeenCalled = true;
13
+ envMangePlugin.startIndependent();
14
+ },
15
+ };
16
+ };
17
+ export const unpluginEnvm = /* #__PURE__ */ createUnplugin(unpluginFactory);
18
+ export default unpluginEnvm;
19
+ export const envmVitePlugin = unpluginEnvm.vite;
20
+ export const envmRollupPlugin = unpluginEnvm.rollup;
21
+ export const envmRolldownPlugin = unpluginEnvm.rolldown;
22
+ export const envmWebpackPlugin = unpluginEnvm.webpack;
23
+ export const envmRspackPlugin = unpluginEnvm.rspack;
24
+ export const envmEsbuildPlugin = unpluginEnvm.esbuild;
25
+ export const envmFarmPlugin = unpluginEnvm.farm;
@@ -0,0 +1,85 @@
1
+ import { join } from "path";
2
+ import { WebSocketServer } from "ws";
3
+ import express from "express";
4
+ import expressStaticGzip from "express-static-gzip";
5
+ import { createProxyMiddleware } from "http-proxy-middleware";
6
+ import { getConfig } from "./utils/ResolveConfig.js";
7
+ import { createRouter } from "./routes/index.js";
8
+ import { globalErrorHandler } from "./middleware/globalErrorHandler.js";
9
+ import { responseEnhancer } from "./middleware/responseEnhancer.js";
10
+ const currentModuleUrl = import.meta.url;
11
+ // 转换为文件系统路径
12
+ import { fileURLToPath } from "url";
13
+ const __filename = fileURLToPath(currentModuleUrl); // 当前文件的完整路径
14
+ // 获取目录路径
15
+ import { dirname } from "path";
16
+ const __dirname = dirname(__filename); // 当前文件所在的目录路径
17
+ import { envLogger } from "./utils/logger.js";
18
+ /**
19
+ * 后置代理服务器---同时也是管理页面的服务器
20
+ */
21
+ class PostProxyServer {
22
+ get config() {
23
+ return getConfig();
24
+ }
25
+ constructor(app = express()) {
26
+ this.app = app;
27
+ // 代理中间件
28
+ app.use(this.createPostProxyMiddleware());
29
+ // 静态资源
30
+ app.use(expressStaticGzip(join(__dirname, "client"), {}));
31
+ // 注入资源
32
+ app.use("/envm-inject", express.static(join(process.cwd(), this.config.injectScriptDir || ""), {}));
33
+ // 统一处理响应
34
+ app.use(responseEnhancer);
35
+ // 初始化管理路由
36
+ app.use(this.config.apiPrefix, createRouter());
37
+ // 全局错误处理中间件
38
+ app.use(globalErrorHandler);
39
+ // 启动服务器
40
+ const server = this.startServer();
41
+ this.registerWs(server);
42
+ }
43
+ /**
44
+ * 启动服务
45
+ */
46
+ startServer() {
47
+ return this.app.listen(this.config.port, () => {
48
+ envLogger.info(`Post Proxy Middleware is running on http://localhost:${this.config.port}`);
49
+ });
50
+ }
51
+ /**
52
+ * 注册ws服务用于通知客户端更新
53
+ * @param server
54
+ */
55
+ registerWs(server) {
56
+ const wss = new WebSocketServer({ noServer: true });
57
+ server.on("upgrade", (request, socket, head) => {
58
+ if (request?.url?.startsWith(this.config.apiPrefix)) {
59
+ wss.handleUpgrade(request, socket, head, () => { });
60
+ }
61
+ });
62
+ }
63
+ /**
64
+ * 创建后置服务器 代理转发中间件
65
+ * @returns
66
+ */
67
+ createPostProxyMiddleware() {
68
+ // 定义路径过滤函数,排除 管理url
69
+ const pathFilter = (path, req) => {
70
+ return !!req.headers["x-api-server"];
71
+ };
72
+ return createProxyMiddleware({
73
+ pathFilter,
74
+ ws: true,
75
+ changeOrigin: true,
76
+ router: async (req) => {
77
+ if (req.headers["x-api-server"]) {
78
+ const target = req.headers["x-api-server"];
79
+ return target;
80
+ }
81
+ },
82
+ });
83
+ }
84
+ }
85
+ export default PostProxyServer;
Binary file
@@ -0,0 +1,16 @@
1
+ <!doctype html>
2
+ <html lang="zh">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" href="/favicon.ico" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>ENVM (环境管理)</title>
8
+ <script type="module" crossorigin src="/assets/index-Bl3B2M1W.js"></script>
9
+ <link rel="modulepreload" crossorigin href="/assets/vue-BGFZjnqd.js">
10
+ <link rel="modulepreload" crossorigin href="/assets/element-plus-wndYbaZY.js">
11
+ <link rel="stylesheet" crossorigin href="/assets/index-nrcddKJg.css">
12
+ </head>
13
+ <body>
14
+ <div id="app"></div>
15
+ </body>
16
+ </html>
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Response code constants
3
+ * @file src/constants/responseCode.ts
4
+ * @description This file defines the response codes used throughout the application.
5
+ * @module constants/responseCode
6
+ */
7
+ export const ResponseCode = {
8
+ /**
9
+ * 成功
10
+ */
11
+ SUCCESS: 200,
12
+ /**
13
+ * 创建成功
14
+ */
15
+ CREATED: 201,
16
+ /**
17
+ * 请求已接受
18
+ */
19
+ BAD_REQUEST: 400,
20
+ /**
21
+ * 未授权访问
22
+ */
23
+ UNAUTHORIZED: 401,
24
+ FORBIDDEN: 403,
25
+ NOT_FOUND: 404,
26
+ INTERNAL_ERROR: 500,
27
+ }; // 只读约束
@@ -0,0 +1,102 @@
1
+ import { devServerLogger } from "../utils/logger.js";
2
+ class DevServerController {
3
+ constructor(devServerService) {
4
+ this.devServerService = devServerService;
5
+ }
6
+ /**
7
+ * 处理获取开发服务器列表
8
+ * @param req
9
+ * @param res
10
+ * @param next
11
+ * @returns
12
+ */
13
+ handleGetDevServerList(req, res, next) {
14
+ try {
15
+ const list = this.devServerService.handleGetList();
16
+ res.success({ list });
17
+ }
18
+ catch (error) {
19
+ next(error);
20
+ }
21
+ }
22
+ /**
23
+ * 处理获取单个开发服务器详情
24
+ * @param req
25
+ * @param res
26
+ * @param next
27
+ * @returns
28
+ */
29
+ handleGetDevServerById(req, res, next) {
30
+ try {
31
+ // 验证请求数据
32
+ const devServerData = req.dto;
33
+ devServerLogger.info(devServerData, "接收环境详情查询请求");
34
+ // 调用服务层获取数据(假设服务层有此方法)
35
+ const detail = this.devServerService.findOneById(devServerData);
36
+ if (!detail) {
37
+ res.error("环境不存在");
38
+ }
39
+ // 返回成功响应(包含详情数据)
40
+ res.success({
41
+ message: "环境详情查询成功",
42
+ data: detail,
43
+ });
44
+ }
45
+ catch (error) {
46
+ next(error);
47
+ }
48
+ }
49
+ /**
50
+ * 处理创建开发服务器
51
+ * @param req
52
+ * @param res
53
+ * @param next
54
+ * @returns
55
+ */
56
+ handleCreateDevServer(req, res, next) {
57
+ try {
58
+ // 验证请求体
59
+ const devServerItem = req.body;
60
+ this.devServerService.handleAddDevServer(devServerItem);
61
+ res.success("开发服务器创建成功");
62
+ }
63
+ catch (error) {
64
+ next(error);
65
+ }
66
+ }
67
+ /**
68
+ * 处理更新开发服务器
69
+ * @param req
70
+ * @param res
71
+ * @param next
72
+ * @returns
73
+ */
74
+ handleUpdateDevServer(req, res, next) {
75
+ try {
76
+ const updateData = req.dto;
77
+ const updatedServer = this.devServerService.handleUpdateDevServer(updateData);
78
+ res.success({ message: "开发服务器更新成功", server: updatedServer });
79
+ }
80
+ catch (error) {
81
+ next(error);
82
+ }
83
+ }
84
+ /**
85
+ * 处理删除开发服务器
86
+ * @param req
87
+ * @param res
88
+ * @param next
89
+ * @returns
90
+ */
91
+ handleDeleteDevServer(req, res, next) {
92
+ try {
93
+ const devServerData = req.dto;
94
+ this.devServerService.handleDeleteDevServer(devServerData);
95
+ res.success({ message: "开发服务器删除成功" });
96
+ }
97
+ catch (error) {
98
+ next(error);
99
+ }
100
+ }
101
+ }
102
+ export { DevServerController };
@@ -0,0 +1,177 @@
1
+ import { envLogger } from "../utils/logger.js";
2
+ /**
3
+ * 环境控制器
4
+ * 负责处理与环境相关的HTTP请求,作为请求入口层:
5
+ * - 解析并验证请求参数
6
+ * - 调用对应的服务层方法处理业务逻辑
7
+ * - 格式化并返回响应结果
8
+ * - 统一错误处理
9
+ */
10
+ class EnvController {
11
+ /**
12
+ * 构造函数 - 注入环境服务实例
13
+ * @param private readonly envService - 环境服务实例,处理核心业务逻辑
14
+ */
15
+ constructor(envService) {
16
+ this.envService = envService;
17
+ }
18
+ /**
19
+ * 新增环境
20
+ * @description 处理创建新环境的POST请求
21
+ * @param req - Express请求对象,包含待创建的环境数据(在req.dto中)
22
+ * @param res - Express响应对象,用于返回处理结果
23
+ * @param next - Express下一步中间件函数,用于错误处理
24
+ */
25
+ handleAddEnv(req, res, next) {
26
+ try {
27
+ // 验证请求数据
28
+ const envData = req.dto;
29
+ envLogger.info({ envData }, "接收新增环境请求");
30
+ // 调用服务层处理业务
31
+ this.envService.handleAddEnv(envData);
32
+ // 返回成功响应
33
+ res.success();
34
+ }
35
+ catch (error) {
36
+ envLogger.error(error, "新增环境请求处理失败");
37
+ next(error);
38
+ }
39
+ }
40
+ /**
41
+ * 删除环境
42
+ * @description 处理删除环境的DELETE请求
43
+ * @param req - Express请求对象,包含待删除环境的ID(在req.dto中)
44
+ * @param res - Express响应对象,用于返回处理结果
45
+ * @param next - Express下一步中间件函数,用于错误处理
46
+ */
47
+ handleDeleteEnv(req, res, next) {
48
+ try {
49
+ // 验证请求数据
50
+ const envData = req.dto;
51
+ envLogger.info({ envData }, "接收删除环境请求");
52
+ // 调用服务层处理业务
53
+ this.envService.handleDeleteEnv(envData);
54
+ // 返回成功响应
55
+ res.success({ message: "环境删除成功" });
56
+ }
57
+ catch (error) {
58
+ envLogger.error(error, "删除环境请求处理失败");
59
+ next(error);
60
+ }
61
+ }
62
+ /**
63
+ * 更新环境
64
+ * @description 处理更新环境信息的PUT请求
65
+ * @param req - Express请求对象,包含待更新的环境数据(在req.dto中)
66
+ * @param res - Express响应对象,用于返回处理结果
67
+ * @param next - Express下一步中间件函数,用于错误处理
68
+ */
69
+ handleUpdateEnv(req, res, next) {
70
+ try {
71
+ // 验证请求数据
72
+ const envData = req.dto;
73
+ envLogger.info({ envData }, "接收更新环境请求");
74
+ // 调用服务层处理业务
75
+ this.envService.handleUpdateEnv(envData);
76
+ // 返回成功响应
77
+ res.success({ message: "环境更新成功" });
78
+ }
79
+ catch (error) {
80
+ envLogger.error(error, "更新环境请求处理失败");
81
+ next(error);
82
+ }
83
+ }
84
+ /**
85
+ * 获取环境列表
86
+ * @description 处理查询所有环境的GET请求
87
+ * @param req - Express请求对象
88
+ * @param res - Express响应对象,用于返回环境列表
89
+ * @param next - Express下一步中间件函数,用于错误处理
90
+ */
91
+ handleGetList(req, res, next) {
92
+ try {
93
+ envLogger.info("接收环境列表查询请求");
94
+ // 调用服务层获取数据
95
+ const list = this.envService.handleGetList();
96
+ // 返回成功响应(包含列表数据)
97
+ res.success({ list, total: list.length });
98
+ }
99
+ catch (error) {
100
+ envLogger.error(error, "环境列表查询请求处理失败");
101
+ next(error);
102
+ }
103
+ }
104
+ /**
105
+ * 获取单个环境详情
106
+ * @description 处理查询指定环境的GET请求
107
+ * @param req - Express请求对象,包含待查询环境的ID(在req.dto中)
108
+ * @param res - Express响应对象,用于返回环境详情
109
+ * @param next - Express下一步中间件函数,用于错误处理
110
+ */
111
+ handleGetDetail(req, res, next) {
112
+ try {
113
+ // 验证请求数据
114
+ const envData = req.dto;
115
+ envLogger.info({ envData }, "接收环境详情查询请求");
116
+ // 调用服务层获取数据(假设服务层有此方法)
117
+ const detail = this.envService.findOneById(envData.id);
118
+ if (!detail) {
119
+ res.error("环境不存在");
120
+ }
121
+ // 返回成功响应(包含详情数据)
122
+ res.success({
123
+ message: "环境详情查询成功",
124
+ data: detail,
125
+ });
126
+ }
127
+ catch (error) {
128
+ envLogger.error(error, "环境详情查询请求处理失败");
129
+ next(error);
130
+ }
131
+ }
132
+ /**
133
+ * 启动代理服务器
134
+ * @description 处理启动指定环境代理服务的POST请求
135
+ * @param req - Express请求对象,包含目标环境的ID(在req.dto中)
136
+ * @param res - Express响应对象,用于返回处理结果
137
+ * @param next - Express下一步中间件函数,用于错误处理
138
+ */
139
+ async handleStartServer(req, res, next) {
140
+ try {
141
+ // 验证请求数据
142
+ const envData = req.dto;
143
+ envLogger.info({ envData }, "接收启动代理服务器请求");
144
+ // 调用服务层处理业务(异步操作)
145
+ await this.envService.handleStartServer(envData);
146
+ // 返回成功响应
147
+ res.success();
148
+ }
149
+ catch (error) {
150
+ envLogger.error(error, "启动代理服务器请求处理失败");
151
+ next(error);
152
+ }
153
+ }
154
+ /**
155
+ * 停止代理服务器
156
+ * @description 处理停止指定环境代理服务的POST请求
157
+ * @param req - Express请求对象,包含目标环境的ID(在req.dto中)
158
+ * @param res - Express响应对象,用于返回处理结果
159
+ * @param next - Express下一步中间件函数,用于错误处理
160
+ */
161
+ async handleStopServer(req, res, next) {
162
+ try {
163
+ // 验证请求数据
164
+ const envData = req.dto;
165
+ envLogger.info({ envData }, "接收停止代理服务器请求");
166
+ // 调用服务层处理业务(异步操作)
167
+ await this.envService.handleStopServer(envData);
168
+ // 返回成功响应
169
+ res.success({ message: "代理服务器停止成功" });
170
+ }
171
+ catch (error) {
172
+ envLogger.error(error, "停止代理服务器请求处理失败");
173
+ next(error);
174
+ }
175
+ }
176
+ }
177
+ export { EnvController };
@@ -0,0 +1,106 @@
1
+ import { envLogger } from "../utils/logger.js";
2
+ /**
3
+ * 密码控制器
4
+ * 负责处理与密码相关的HTTP请求,作为请求入口层:
5
+ * - 解析并验证请求参数
6
+ * - 调用对应的服务层方法处理业务逻辑
7
+ * - 格式化并返回响应结果
8
+ * - 统一错误处理
9
+ */
10
+ class PasswordController {
11
+ /**
12
+ * 构造函数 - 注入密码服务实例
13
+ * @param private readonly passwordService - 密码服务实例,处理核心业务逻辑
14
+ */
15
+ constructor(passwordService) {
16
+ this.passwordService = passwordService;
17
+ }
18
+ /**
19
+ * 获取指定环境的密码列表
20
+ * @description 处理查询指定环境所有密码的GET请求
21
+ * @param req - Express请求对象,包含环境ID(在params中)
22
+ * @param res - Express响应对象,用于返回密码列表
23
+ * @param next - Express下一步中间件函数,用于错误处理
24
+ */
25
+ handleGetList(req, res, next) {
26
+ try {
27
+ const envId = req.params.envId;
28
+ envLogger.info({ envId }, "接收密码列表查询请求");
29
+ // 调用服务层获取数据
30
+ const list = this.passwordService.handleGetByEnvId(envId);
31
+ // 返回成功响应(包含列表数据)
32
+ res.success({ list, total: list.length });
33
+ }
34
+ catch (error) {
35
+ envLogger.error(error, "密码列表查询请求处理失败");
36
+ next(error);
37
+ }
38
+ }
39
+ /**
40
+ * 新增密码
41
+ * @description 处理创建新密码的POST请求
42
+ * @param req - Express请求对象,包含待创建的密码数据(在req.dto中)
43
+ * @param res - Express响应对象,用于返回处理结果
44
+ * @param next - Express下一步中间件函数,用于错误处理
45
+ */
46
+ handleAdd(req, res, next) {
47
+ try {
48
+ // 验证请求数据
49
+ const passwordData = req.dto;
50
+ envLogger.info({ passwordData: { ...passwordData, password: "***" } }, "接收新增密码请求");
51
+ // 调用服务层处理业务
52
+ const newPassword = this.passwordService.handleAdd(passwordData);
53
+ // 返回成功响应
54
+ res.success({ message: "密码添加成功", data: newPassword });
55
+ }
56
+ catch (error) {
57
+ envLogger.error(error, "新增密码请求处理失败");
58
+ next(error);
59
+ }
60
+ }
61
+ /**
62
+ * 更新密码
63
+ * @description 处理更新密码的POST/PUT请求
64
+ * @param req - Express请求对象,包含待更新的密码数据(在req.dto中)
65
+ * @param res - Express响应对象,用于返回处理结果
66
+ * @param next - Express下一步中间件函数,用于错误处理
67
+ */
68
+ handleUpdate(req, res, next) {
69
+ try {
70
+ // 验证请求数据
71
+ const passwordData = req.dto;
72
+ envLogger.info({ passwordData: { ...passwordData, password: "***" } }, "接收更新密码请求");
73
+ // 调用服务层处理业务
74
+ const updatedPassword = this.passwordService.handleUpdate(passwordData);
75
+ // 返回成功响应
76
+ res.success({ message: "密码更新成功", data: updatedPassword });
77
+ }
78
+ catch (error) {
79
+ envLogger.error(error, "更新密码请求处理失败");
80
+ next(error);
81
+ }
82
+ }
83
+ /**
84
+ * 删除密码
85
+ * @description 处理删除密码的POST/DELETE请求
86
+ * @param req - Express请求对象,包含待删除密码的ID(在req.dto中)
87
+ * @param res - Express响应对象,用于返回处理结果
88
+ * @param next - Express下一步中间件函数,用于错误处理
89
+ */
90
+ handleDelete(req, res, next) {
91
+ try {
92
+ // 验证请求数据
93
+ const passwordData = req.dto;
94
+ envLogger.info({ passwordData }, "接收删除密码请求");
95
+ // 调用服务层处理业务
96
+ this.passwordService.handleDelete(passwordData);
97
+ // 返回成功响应
98
+ res.success({ message: "密码删除成功" });
99
+ }
100
+ catch (error) {
101
+ envLogger.error(error, "删除密码请求处理失败");
102
+ next(error);
103
+ }
104
+ }
105
+ }
106
+ export { PasswordController };