2026-02-10 00:42:48 +01:00
|
|
|
import { createServer } from 'http'
|
|
|
|
|
import express from 'express'
|
|
|
|
|
import { WebSocketServer } from 'ws'
|
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
import { dirname, join } from 'path'
|
2026-02-10 09:53:46 +01:00
|
|
|
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
|
|
|
|
import { printServerInfo } from './src/server-utils.js'
|
2026-02-10 00:42:48 +01:00
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
const __dirname = dirname(__filename)
|
|
|
|
|
|
2026-02-10 09:53:46 +01:00
|
|
|
// --- Configurazione del server ---
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-02-10 23:45:58 +01:00
|
|
|
const DISPLAY_PORT = process.env.PORT || 3000
|
|
|
|
|
const CONTROLLER_PORT = process.env.CONTROLLER_PORT || 3001
|
|
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
// Server Display (porta principale)
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
|
|
const displayApp = express()
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-02-10 09:53:46 +01:00
|
|
|
// Espone i file generati dalla build di Vite.
|
2026-02-10 23:45:58 +01:00
|
|
|
displayApp.use(express.static(join(__dirname, 'dist')))
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-02-10 23:45:58 +01:00
|
|
|
// Fallback per SPA: restituisce `index.html` per tutte le route.
|
2026-02-12 14:11:05 +01:00
|
|
|
displayApp.get(/.*/, (_req, res) => {
|
2026-02-10 00:42:48 +01:00
|
|
|
res.sendFile(join(__dirname, 'dist', 'index.html'))
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-10 23:45:58 +01:00
|
|
|
const displayServer = createServer(displayApp)
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-02-10 23:45:58 +01:00
|
|
|
// Inizializza il server WebSocket condiviso.
|
|
|
|
|
const wss = new WebSocketServer({ noServer: true })
|
2026-02-10 09:53:46 +01:00
|
|
|
setupWebSocketHandler(wss)
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-02-10 23:45:58 +01:00
|
|
|
displayServer.on('upgrade', (request, socket, head) => {
|
|
|
|
|
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
|
|
|
|
if (pathname === '/ws') {
|
|
|
|
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
|
wss.emit('connection', ws, request)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
socket.destroy()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
displayServer.listen(DISPLAY_PORT, '0.0.0.0', () => {
|
|
|
|
|
console.log(`[Display] Server running on port ${DISPLAY_PORT}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
// Server Controller (porta separata)
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
|
|
const controllerApp = express()
|
|
|
|
|
|
|
|
|
|
// Espone gli stessi file statici della build.
|
2026-02-12 14:11:05 +01:00
|
|
|
// IMPORTANTE: { index: false } impedisce di servire index.html (il display) sulla root.
|
|
|
|
|
controllerApp.use(express.static(join(__dirname, 'dist'), { index: false }))
|
2026-02-10 23:45:58 +01:00
|
|
|
|
|
|
|
|
// Fallback: restituisce `controller.html` per tutte le route.
|
2026-02-12 14:11:05 +01:00
|
|
|
controllerApp.get(/.*/, (_req, res) => {
|
2026-02-10 23:45:58 +01:00
|
|
|
res.sendFile(join(__dirname, 'dist', 'controller.html'))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const controllerServer = createServer(controllerApp)
|
|
|
|
|
|
|
|
|
|
// Gestisce l'upgrade WebSocket anche sulla porta del controller.
|
|
|
|
|
controllerServer.on('upgrade', (request, socket, head) => {
|
|
|
|
|
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
|
|
|
|
if (pathname === '/ws') {
|
|
|
|
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
|
wss.emit('connection', ws, request)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
socket.destroy()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
controllerServer.listen(CONTROLLER_PORT, '0.0.0.0', () => {
|
|
|
|
|
printServerInfo(DISPLAY_PORT, CONTROLLER_PORT)
|
2026-02-10 00:42:48 +01:00
|
|
|
})
|