dev-server.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const express = require("express");
  2. const { generateDevHtml, build } = require("./utils.js");
  3. const { argv } = require('node:process');
  4. const chalk = require('chalk');
  5. const dotenv = require('dotenv');
  6. dotenv.config();
  7. const app = express();
  8. let port = process.env.PORT ?? 4000; // Starting port
  9. const maxAttempts = 10; // Maximum number of ports to try
  10. const env = argv[2] ?? "dev";
  11. const startServer = (attempt) => {
  12. if (attempt > maxAttempts) {
  13. console.error(chalk.red(`ERROR: Unable to find an available port after ${maxAttempts} attempts.`));
  14. return;
  15. }
  16. app.listen(port, () => {
  17. console.log("\n-----------------------------------------------------------\n");
  18. console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${port}`));
  19. console.log("\n-----------------------------------------------------------\n");
  20. }).on('error', (err) => {
  21. if (err.code === 'EADDRINUSE') { // Check if the error is because the port is already in use
  22. console.error(chalk.red(`ERROR: Port ${port} is already in use. Trying next port...`));
  23. port++; // Increment the port number
  24. startServer(attempt + 1); // Try the next port
  25. }
  26. });
  27. };
  28. // Start the server with the first attempt
  29. startServer(1);
  30. // build the GUI
  31. build();
  32. app.get(["/", "/app/*", "/action/*"], (req, res) => {
  33. res.send(generateDevHtml({
  34. env: env,
  35. api_origin: "https://api.puter.com",
  36. title: "Puter",
  37. max_item_name_length: 150,
  38. require_email_verification_to_publish_website: false,
  39. 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.`,
  40. }));
  41. })
  42. app.use(express.static('./'));
  43. if(env === "prod"){
  44. // make sure to serve the ./dist/ folder maps to the root of the website
  45. app.use(express.static('./dist/'));
  46. }
  47. if(env === "dev"){
  48. app.use(express.static('./src/'));
  49. }
  50. module.exports = app;