dev-server.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import express from "express";
  20. import { generateDevHtml, build } from "./utils.js";
  21. import { argv } from 'node:process';
  22. import chalk from 'chalk';
  23. import dotenv from 'dotenv';
  24. dotenv.config();
  25. const app = express();
  26. let port = process.env.PORT ?? 4000; // Starting port
  27. const maxAttempts = 10; // Maximum number of ports to try
  28. const env = argv[2] ?? "dev";
  29. const startServer = (attempt, useAnyFreePort = false) => {
  30. if (attempt > maxAttempts) {
  31. useAnyFreePort = true; // Use any port that is free
  32. }
  33. const server = app.listen(useAnyFreePort ? 0 : port, () => {
  34. console.log("\n-----------------------------------------------------------\n");
  35. console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${server.address().port}`));
  36. console.log("\n-----------------------------------------------------------\n");
  37. }).on('error', (err) => {
  38. if (err.code === 'EADDRINUSE') { // Check if the error is because the port is already in use
  39. console.error(chalk.red(`ERROR: Port ${port} is already in use. Trying next port...`));
  40. port++; // Increment the port number
  41. startServer(attempt + 1); // Try the next port
  42. }
  43. });
  44. };
  45. // Start the server with the first attempt
  46. startServer(1);
  47. // build the GUI
  48. build();
  49. app.get(["/", "/app/*", "/action/*"], (req, res) => {
  50. res.send(generateDevHtml({
  51. env: env,
  52. api_origin: "https://api.puter.com",
  53. title: "Puter",
  54. max_item_name_length: 150,
  55. require_email_verification_to_publish_website: false,
  56. short_description: `Puter is a privacy-first personal cloud that houses all your files, apps, and games in one private and secure place, accessible from anywhere at any time.`,
  57. }));
  58. })
  59. app.use(express.static('./'));
  60. if(env === "prod"){
  61. // make sure to serve the ./dist/ folder maps to the root of the website
  62. app.use(express.static('./dist/'));
  63. }
  64. if(env === "dev"){
  65. app.use(express.static('./src/'));
  66. }
  67. export { app };