1
0

dev-server.js 2.0 KB

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