2024-03-20 14:10:39 +00:00
import express from "express" ;
import { generateDevHtml , build } from "./utils.js" ;
import { argv } from 'node:process' ;
import chalk from 'chalk' ;
import dotenv from 'dotenv' ;
2024-03-13 05:42:13 +00:00
dotenv . config ( ) ;
2024-03-03 02:39:14 +00:00
const app = express ( ) ;
2024-03-13 05:42:13 +00:00
let port = process . env . PORT ? ? 4000 ; // Starting port
2024-03-03 02:39:14 +00:00
const maxAttempts = 10 ; // Maximum number of ports to try
const env = argv [ 2 ] ? ? "dev" ;
2024-03-14 06:23:53 +00:00
const startServer = ( attempt , useAnyFreePort = false ) => {
2024-03-03 02:39:14 +00:00
if ( attempt > maxAttempts ) {
2024-03-14 06:23:53 +00:00
useAnyFreePort = true ; // Use any port that is free
2024-03-03 02:39:14 +00:00
}
2024-03-14 06:23:53 +00:00
const server = app . listen ( useAnyFreePort ? 0 : port , ( ) => {
2024-03-03 02:39:14 +00:00
console . log ( "\n-----------------------------------------------------------\n" ) ;
2024-03-14 06:23:53 +00:00
console . log ( ` Puter is now live at: ` , chalk . underline . blue ( ` http://localhost: ${ server . address ( ) . port } ` ) ) ;
2024-03-03 02:39:14 +00:00
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 ( './dist/' ) ) ;
}
if ( env === "dev" ) {
app . use ( express . static ( './src/' ) ) ;
}
2024-03-20 14:10:39 +00:00
export { app } ;