Refactor server configuration to use environment variables for PORT and HOST; normalize itemList structure in inventory state update

This commit is contained in:
MayaTheShy
2026-03-21 17:30:59 -04:00
parent 23d237fa20
commit bd5b1db5a0

View File

@@ -4,7 +4,8 @@ import cors from 'cors';
import { createServer } from 'http';
const app = express();
const PORT = 3001;
const PORT = process.env.PORT || 3001;
const HOST = process.env.HOST || '0.0.0.0';
app.use(cors());
app.use(express.json({ limit: '5mb' }));
@@ -282,8 +283,16 @@ app.post('/api/bridge/result', (req, res) => {
function updateStateFromBridge(data) {
if (data.cache) {
// Normalize itemList: Lua sends { name, total }, frontend expects { name, count }
const rawItems = data.cache.itemList || [];
const normalizedItems = rawItems.map(item => ({
name: item.name,
count: item.total !== undefined ? item.total : (item.count || 0),
displayName: item.displayName || item.name,
}));
inventoryState = {
itemList: data.cache.itemList || [],
itemList: normalizedItems,
grandTotal: data.cache.grandTotal || 0,
chestCount: data.cache.chestCount || 0,
totalSlots: data.cache.totalSlots || 0,
@@ -420,8 +429,8 @@ wss.on('connection', (ws, req) => {
// ========== Start Server ==========
server.listen(PORT, () => {
console.log(`✅ Inventory Manager Web Server ready on port ${PORT}`);
server.listen(PORT, HOST, () => {
console.log(`✅ Inventory Manager Web Server ready on ${HOST}:${PORT}`);
console.log(`\nBridge HTTP endpoint: http://localhost:${PORT}/api/bridge/state`);
console.log(`Bridge WebSocket: ws://localhost:${PORT}/ws/bridge`);
console.log(`Web client WebSocket: ws://localhost:${PORT}/ws`);