diff --git a/package.json b/package.json index 181da4a9..4bca2257 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "scripts": { "test": "mocha ./src/phoenix/test ./src/phoenix/packages/contextlink/test", "start=gui": "nodemon --exec \"node dev-server.js\" ", - "start": "node run-selfhosted.js", + "start": "node ./tools/run-selfhosted.js", "build": "cd src/gui; node ./build.js", "check-translations": "node tools/check-translations.js" }, diff --git a/src/gui/dev-server.js b/src/gui/dev-server.js new file mode 100644 index 00000000..d506eff7 --- /dev/null +++ b/src/gui/dev-server.js @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2024 Puter Technologies Inc. + * + * This file is part of Puter. + * + * Puter is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import express from "express"; +import { generateDevHtml, build } from "./utils.js"; +import { argv } from 'node:process'; +import chalk from 'chalk'; +import dotenv from 'dotenv'; +import path_ from 'path'; +dotenv.config(); + +const app = express(); +let port = process.env.PORT ?? 4000; // Starting port +const maxAttempts = 10; // Maximum number of ports to try +const env = argv[2] ?? "dev"; + +const startServer = (attempt, useAnyFreePort = false) => { + if (attempt > maxAttempts) { + useAnyFreePort = true; // Use any port that is free + } + + const server = app.listen(useAnyFreePort ? 0 : port, () => { + console.log("\n-----------------------------------------------------------\n"); + console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${server.address().port}`)); + console.log("\n-----------------------------------------------------------\n"); + }).on('error', (err) => { + if (err.code === 'EADDRINUSE') { // Check if the error is because the port is already in use + console.error(chalk.red(`ERROR: Port ${port} is already in use. Trying next port...`)); + port++; // Increment the port number + startServer(attempt + 1); // Try the next port + } + }); +}; + +// Start the server with the first attempt +startServer(1); + +// build the GUI +build(); + +app.get(["/", "/app/*", "/action/*"], (req, res) => { + res.send(generateDevHtml({ + env: env, + api_origin: "https://api.puter.com", + title: "Puter", + max_item_name_length: 150, + require_email_verification_to_publish_website: false, + 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.`, + })); +}) +app.use(express.static('./')); + +if(env === "prod"){ + // make sure to serve the ./dist/ folder maps to the root of the website + app.use(express.static('./src/gui/dist/')); +} + +if(env === "dev"){ + app.use(express.static('./src/gui/src/')); +} + +export { app }; diff --git a/l_checker_config.json b/tools/l_checker_config.json similarity index 100% rename from l_checker_config.json rename to tools/l_checker_config.json diff --git a/run-selfhosted.js b/tools/run-selfhosted.js similarity index 100% rename from run-selfhosted.js rename to tools/run-selfhosted.js