modularize code, add dummy version of roverd

This commit is contained in:
legop3
2025-11-12 01:13:29 -05:00
parent b5ce6706d6
commit 73b1b44c35
41 changed files with 1018 additions and 401 deletions
+6
View File
@@ -0,0 +1,6 @@
const path = require('path');
module.exports = {
port: process.env.PORT || 8080,
staticDir: path.join(__dirname, '..', '..', 'public'),
};
+14
View File
@@ -0,0 +1,14 @@
const path = require('path');
const http = require('http');
const express = require('express');
const morgan = require('morgan');
const config = require('./config');
const app = express();
app.use(morgan('dev'));
app.use(express.json());
app.use(express.static(config.staticDir));
const httpServer = http.createServer(app);
module.exports = { app, httpServer };
+8
View File
@@ -0,0 +1,8 @@
const { Server: SocketIOServer } = require('socket.io');
const { httpServer } = require('./http');
const io = new SocketIOServer(httpServer, {
cors: { origin: '*' },
});
module.exports = io;
+9
View File
@@ -0,0 +1,9 @@
function stamp(level, args) {
return [new Date().toISOString(), `[${level}]`, ...args];
}
module.exports = {
info: (...args) => console.log(...stamp('INFO', args)),
warn: (...args) => console.warn(...stamp('WARN', args)),
error: (...args) => console.error(...stamp('ERROR', args)),
};
+21
View File
@@ -0,0 +1,21 @@
const { WebSocketServer } = require('ws');
const { httpServer } = require('./http');
const logger = require('./logger');
const roverWSS = new WebSocketServer({ noServer: true });
httpServer.on('upgrade', (req, socket, head) => {
if (req.url.startsWith('/rover')) {
roverWSS.handleUpgrade(req, socket, head, (ws) => {
roverWSS.emit('connection', ws, req);
});
} else {
socket.destroy();
}
});
roverWSS.on('connection', () => {
logger.info('Rover websocket connected');
});
module.exports = roverWSS;