checkConfig.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import fetch from "node-fetch";
  2. import dotenv from "dotenv";
  3. import { io } from "socket.io-client";
  4. import fs from "fs";
  5. dotenv.config();
  6. dotenv.config({ path: "backend/.env" });
  7. const siteId = process.env.VITE_RICOCHET_SITEID;
  8. const socketURL = process.env.VITE_SOCKET_URL;
  9. const socketPath = process.env.VITE_SOCKET_PATH || "/socket.io";
  10. const apiEndpoint = process.env.VITE_API_ENDPOINT;
  11. const fileStore = process.env.FILE_STORE_BACKEND;
  12. const jsonStore = process.env.JSON_STORE_BACKEND;
  13. const check = async () => {
  14. if (!siteId) {
  15. console.log(
  16. "🚨 You must define a VITE_RICOCHET_SITEID environment variable."
  17. );
  18. process.exit(1);
  19. }
  20. try {
  21. const result = await fetch(`${apiEndpoint}/${siteId}/`);
  22. // console.log(result);
  23. if (result.status !== 400) {
  24. console.log(
  25. "🚨 The Ricochet.js server doesn't respond correctly. " +
  26. `Have you started it? Tested url: ${apiEndpoint}`
  27. );
  28. return;
  29. }
  30. const data = await result.json();
  31. if (!data.message.includes("X-Ricochet-Origin")) {
  32. console.log(
  33. "🚨 The Ricochet.js server doesn't respond correctly. " +
  34. `Have you started it? Tested url: ${apiEndpoint}`
  35. );
  36. return;
  37. }
  38. const patch = await fetch(`${apiEndpoint}/_register/${siteId}`, {
  39. method: "PATCH",
  40. });
  41. if (patch.status === 404) {
  42. console.log(
  43. `🚨 The '${siteId}' doesn't seem to exists on Ricochet.js. ` +
  44. "Have you registered it?"
  45. );
  46. return;
  47. }
  48. if (patch.status === 400) {
  49. console.log(
  50. `✅ Ricochet.js is running and site ${siteId} seems to be registered.`
  51. );
  52. }
  53. } catch (e) {
  54. if (e.code === "ECONNREFUSED") {
  55. console.log(
  56. "🚨 The Ricochet.js server doesn't seem to be up and running. " +
  57. `Have you started it? Tested url: ${apiEndpoint}`
  58. );
  59. return;
  60. }
  61. }
  62. if (!fs.existsSync("./public/ricochet.json")) {
  63. console.log(
  64. "🚨 The './public/ricochet.json' file is missing. " +
  65. "Have you generated it? \nHint: from backend/ dir execute `npm run watch`"
  66. );
  67. return;
  68. }
  69. if (jsonStore === "memory") {
  70. console.log(
  71. "⚠️ The Ricochet.js JSON store is in memory. " +
  72. "Remember that you'll lost all changes and registered sites each time you stop the server."
  73. );
  74. }
  75. if (fileStore === "memory") {
  76. console.log(
  77. "⚠️ The Ricochet.js File store is in memory. " +
  78. "Remember that you'll lost all files each time you stop the server."
  79. );
  80. }
  81. const testConn = new Promise((resolve, reject) => {
  82. const socket = io(socketURL, {
  83. transports: ["websocket"],
  84. path: socketPath,
  85. });
  86. const out = setTimeout(() => {
  87. reject("failed");
  88. socket.disconnect();
  89. }, 5000);
  90. socket.on("connect", () => {
  91. resolve("ok");
  92. clearTimeout(out);
  93. socket.disconnect();
  94. });
  95. });
  96. try {
  97. await testConn;
  98. console.log("✅ Wire.io server is up and running.");
  99. } catch (e) {
  100. console.log(
  101. "🚨 The Wire.io server hasn't responded in 5s. " +
  102. `Have you started it? Tested url: ${socketURL}${socketPath}`
  103. );
  104. return;
  105. }
  106. console.log(
  107. "\n👏 Congrats, everything works fine!\n\nDo you still have an issue? Go to discord channel for more help."
  108. );
  109. };
  110. check();