Refactor server configuration to use environment variables for PORT and HOST; normalize itemList structure in inventory state update
This commit is contained in:
@@ -4,7 +4,8 @@ import cors from 'cors';
|
|||||||
import { createServer } from 'http';
|
import { createServer } from 'http';
|
||||||
|
|
||||||
const app = express();
|
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(cors());
|
||||||
app.use(express.json({ limit: '5mb' }));
|
app.use(express.json({ limit: '5mb' }));
|
||||||
@@ -282,8 +283,16 @@ app.post('/api/bridge/result', (req, res) => {
|
|||||||
|
|
||||||
function updateStateFromBridge(data) {
|
function updateStateFromBridge(data) {
|
||||||
if (data.cache) {
|
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 = {
|
inventoryState = {
|
||||||
itemList: data.cache.itemList || [],
|
itemList: normalizedItems,
|
||||||
grandTotal: data.cache.grandTotal || 0,
|
grandTotal: data.cache.grandTotal || 0,
|
||||||
chestCount: data.cache.chestCount || 0,
|
chestCount: data.cache.chestCount || 0,
|
||||||
totalSlots: data.cache.totalSlots || 0,
|
totalSlots: data.cache.totalSlots || 0,
|
||||||
@@ -420,8 +429,8 @@ wss.on('connection', (ws, req) => {
|
|||||||
|
|
||||||
// ========== Start Server ==========
|
// ========== Start Server ==========
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, HOST, () => {
|
||||||
console.log(`✅ Inventory Manager Web Server ready on port ${PORT}`);
|
console.log(`✅ Inventory Manager Web Server ready on ${HOST}:${PORT}`);
|
||||||
console.log(`\nBridge HTTP endpoint: http://localhost:${PORT}/api/bridge/state`);
|
console.log(`\nBridge HTTP endpoint: http://localhost:${PORT}/api/bridge/state`);
|
||||||
console.log(`Bridge WebSocket: ws://localhost:${PORT}/ws/bridge`);
|
console.log(`Bridge WebSocket: ws://localhost:${PORT}/ws/bridge`);
|
||||||
console.log(`Web client WebSocket: ws://localhost:${PORT}/ws`);
|
console.log(`Web client WebSocket: ws://localhost:${PORT}/ws`);
|
||||||
|
|||||||
Reference in New Issue
Block a user