web.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const express = require('express');
  2. const robot = require('./robot')
  3. const {getRustConfig} = require('./config')
  4. const http = require("./utils/http");
  5. const file = require('./utils/file')
  6. const {logger, fileLogger} = require("./utils/logger");
  7. const {spawn, exec, fork, execSync} = require("child_process");
  8. const path = require("path");
  9. const {ka, tr} = require("date-fns/locale");
  10. const {getAppMap} = require("./robot");
  11. const crypto = require('crypto');
  12. const fs = require("fs");
  13. /*******
  14. * web
  15. * **** */
  16. function init() {
  17. const app = express();
  18. const port = 3000;
  19. // 中间件示例,用于处理JSON请求体
  20. app.use(express.json());
  21. app.use((req, res, next) => {
  22. var k = "$8gBV!f&L@E"
  23. if ("timestamp" in req.headers) {
  24. var timestamp = req.headers.timestamp
  25. var md5 = req.headers.catilla
  26. //默认32位
  27. const md5Str = crypto.createHash('md5').update(k + timestamp).digest('hex');
  28. logger.info(JSON.stringify(md5Str))
  29. if (md5 === md5Str) {
  30. next();
  31. } else {
  32. res.status(403).send('拒绝访问!');
  33. }
  34. } else {
  35. res.status(403).send('没有访问权限!');
  36. }
  37. });
  38. // GET请求的路由示例
  39. app.get('/', (req, res) => {
  40. res.send('is OK!');
  41. });
  42. app.get('/robotList', (req, res) => {
  43. var map = robot.getAppMap()
  44. var list = new Array();
  45. robot.appMap.forEach((value, key) => {
  46. // str.set(key,JSON.stringify(value))
  47. list.push({
  48. id: value.id,
  49. nowBalance: value.nowBalance + "",
  50. messlist: value.messlist,
  51. threadStatus: value.threadStatus,
  52. errorMessage: value.errorMessage,
  53. threadStartTime: value.threadStartTime,
  54. status: value.status,
  55. restartStatus: value.restartStatus
  56. })
  57. })
  58. res.send({'code': 200, 'data': JSON.stringify(list), "message": "SUCCESS"});
  59. });
  60. app.get('/app11', (req, res) => {
  61. // res.send('is OK!');
  62. var s = JSON.parse("{}");
  63. robot.appMap.forEach((value, key) => {
  64. // str.set(key,JSON.stringify(value))
  65. s[key] = value
  66. })
  67. res.send({'code': 200, 'data': JSON.stringify(s), "message": "SUCCESS"});
  68. });
  69. //简单三个接口,为了方便使用get请求
  70. app.get('/isOK', (req, res) => {
  71. res.send({'code': 200, 'data': true, "message": "SUCCESS"});
  72. });
  73. app.get('/logs', (req, res) => {
  74. // 获取发送过来的信息(请求体的数据)
  75. const param = req.query;
  76. const n = param.n
  77. const id = param.id
  78. var port = 1111;
  79. robot.appMap.forEach((value, key) => {
  80. // logger.info(JSON.stringify(value))
  81. if (value.id + "" === id + "") {
  82. port = value.port
  83. }
  84. })
  85. let result = [];
  86. if (port !== -1) {
  87. var logPath = "./logs" + port
  88. logger.info(`访问-日志相对目录:${logPath}`)
  89. // 将相对路径转换为绝对路径
  90. const directoryPath = path.resolve(logPath);
  91. try {
  92. file.checkPathSync(directoryPath)
  93. file.getLastFile(logPath, 10, (fileNameList) => {
  94. logger.info('文件:', fileNameList);
  95. result = file.readLastNLines(logPath, fileNameList, n);
  96. // logger.info('?????:', result);
  97. res.send({'code': 200, 'data': result, "message": "SUCCESS"});
  98. });
  99. }catch (e) {
  100. logger.info('获取日志异常了~~', e);
  101. res.send({'code': 200, 'data': [], "message": "SUCCESS"});
  102. }
  103. } else {
  104. res.send({'code': 200, 'data': result, "message": "SUCCESS"});
  105. }
  106. });
  107. app.post('/execute', (req, res) => {
  108. // 获取发送过来的信息(请求体的数据)
  109. const param = req.body;
  110. // logger.info('--web 启动',param);
  111. (async (param) => {
  112. // logger.info('请求体:', );
  113. var executeType = param.executeType
  114. if (executeType === "RUN") {
  115. await robot.run(param)
  116. } else if (executeType === "STOP") {
  117. await robot.closeApp(param)
  118. } else if (executeType === "RESTART") {
  119. await robot.restartApp(param)
  120. }
  121. })(param);
  122. // 回应信号(响应请求)
  123. res.send({'code': 200, 'data': "null", "message": "SUCCESS"});
  124. })
  125. // 使服务器监听特定端口
  126. app.listen(port, () => {
  127. // console.log(`服务器正在监听 http://localhost:${port}`);
  128. });
  129. logger.info('--web 启动');
  130. }
  131. // node-已上线-上报
  132. function robotNodeStatus() {
  133. var config = getRustConfig()
  134. http.request_post(`${config.reportedUrl}/report/beOnline`, {}, {...config.headers}).then((data) => {
  135. // logger.info('??', data);
  136. logger.info('#####################汇报:node 在线-上报成功!', data);
  137. }).catch((error) => {
  138. logger.error(`node 在线!-上报失败: ${error.message}`); // 处理可能发生的错误
  139. });
  140. }
  141. function isOK(req, res) {
  142. res.send({'code': 200, 'data': "null", "message": "SUCCESS"});
  143. }
  144. // 日志读取操作
  145. module.exports = {
  146. init,
  147. robotNodeStatus
  148. };