cross.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. const { Controller } = require("ee-core");
  3. const Cross = require("ee-core/cross");
  4. const Log = require("ee-core/log");
  5. const HttpClient = require("ee-core/httpclient");
  6. const Services = require("ee-core/services");
  7. /**
  8. * Cross
  9. * @class
  10. */
  11. class CrossController extends Controller {
  12. constructor(ctx) {
  13. super(ctx);
  14. }
  15. /**
  16. * View process service information
  17. */
  18. info() {
  19. const pids = Cross.getPids();
  20. Log.info("cross pids:", pids);
  21. let num = 1;
  22. pids.forEach((pid) => {
  23. let entity = Cross.getProc(pid);
  24. Log.info(`server-${num} name:${entity.name}`);
  25. Log.info(`server-${num} config:`, entity.config);
  26. num++;
  27. });
  28. return "hello electron-egg";
  29. }
  30. /**
  31. * Get service url
  32. */
  33. async getUrl(args) {
  34. const { name } = args;
  35. const serverUrl = Cross.getUrl(name);
  36. return serverUrl;
  37. }
  38. /**
  39. * kill service
  40. * By default (modifiable), killing the process will exit the electron application.
  41. */
  42. async killServer(args) {
  43. const { type, name } = args;
  44. if (type == "all") {
  45. Cross.killAll();
  46. } else {
  47. Cross.killByName(name);
  48. }
  49. return;
  50. }
  51. /**
  52. * create service
  53. */
  54. async createServer(args) {
  55. const { program } = args;
  56. if (program == "go") {
  57. Services.get("cross").createGoServer();
  58. } else if (program == "java") {
  59. Services.get("cross").createJavaServer();
  60. } else if (program == "python") {
  61. Services.get("cross").createPythonServer();
  62. }
  63. return;
  64. }
  65. /**
  66. * Access the api for the cross service
  67. */
  68. async requestApi(args) {
  69. const { name, urlPath, params } = args;
  70. const hc = new HttpClient();
  71. const serverUrl = Cross.getUrl(name);
  72. console.log("Server Url:", serverUrl);
  73. const apiHello = serverUrl + urlPath;
  74. const options = {
  75. method: "GET",
  76. data: params || {},
  77. dataType: "json",
  78. timeout: 1000,
  79. };
  80. const result = await hc.request(apiHello, options);
  81. return result.data;
  82. }
  83. }
  84. CrossController.toString = () => "[class CrossController]";
  85. module.exports = CrossController;