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-05-12 12:22:43 +02:00
|
|
|
const PORT = process.env.PORT || 3000
|
|
|
|
|
const distDir = join(__dirname, 'dist')
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
const app = express()
|
2026-02-10 23:45:58 +01:00
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
app.use(express.static(distDir, { index: false }))
|
2026-02-10 23:45:58 +01:00
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
app.get(['/', '/display', '/display/*splat'], (_req, res) => {
|
|
|
|
|
res.sendFile(join(distDir, 'index.html'))
|
2026-02-10 00:42:48 +01:00
|
|
|
})
|
|
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
app.get(['/controller', '/controller/*splat'], (_req, res) => {
|
|
|
|
|
res.sendFile(join(distDir, 'controller.html'))
|
|
|
|
|
})
|
2026-02-10 00:42:48 +01:00
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
const server = createServer(app)
|
2026-02-10 23:45:58 +01:00
|
|
|
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-05-12 12:22:43 +02:00
|
|
|
server.on('upgrade', (request, socket, head) => {
|
2026-02-10 23:45:58 +01:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
server.listen(PORT, '0.0.0.0', () => {
|
|
|
|
|
printServerInfo(PORT)
|
2026-02-10 00:42:48 +01:00
|
|
|
})
|