'use strict'; const express = require('express'); const cors = require('cors'); const database = require('./database'); class WebServer { constructor() { console.log('WebServer: Constructor called'); this.app = null; this.port = null; this.host = null; this.server = null; } async start(bot) { console.log('WebServer: start() called'); const conf = require('../../conf'); this.port = conf.storage?.webPort || 3000; this.host = conf.storage?.webHost || '0.0.0.0'; console.log(`WebServer: Configuring server on ${this.host}:${this.port}`); this.app = express(); // Middleware this.app.use(express.json()); this.app.use(cors()); // Request logging this.app.use((req, res, next) => { console.log(`WebServer: ${req.method} ${req.path}`); next(); }); // Routes this.setupRoutes(); // Start server return new Promise((resolve, reject) => { this.server = this.app.listen(this.port, this.host, () => { console.log(`WebServer: Running at http://${this.host}:${this.port}`); console.log(`WebServer: Try http://localhost:${this.port}/health`); resolve(); }); this.server.on('error', (err) => { console.error('WebServer: Failed to start:', err); reject(err); }); }); } setupRoutes() { console.log('WebServer: Setting up routes...'); // Index page - Full web UI this.app.get('/', async (req, res) => { res.send(this.getIndexHTML()); }); // Health check this.app.get('/health', (req, res) => { res.json({ status: 'ok', server: `${this.host}:${this.port}` }); }); // Inventory - Get all items aggregated this.app.get('/api/inventory', async (req, res) => { try { const items = await database.searchItems(req.query.q); res.json({ items }); } catch (error) { console.error('API Error /api/inventory:', error); res.status(500).json({ error: error.message }); } }); // Get specific item details this.app.get('/api/inventory/:itemId', async (req, res) => { try { const itemId = parseInt(req.params.itemId); const item = await database.getItemDetails(itemId); if (!item) { return res.status(404).json({ error: 'Item not found' }); } res.json({ item }); } catch (error) { console.error('API Error /api/inventory/:itemId:', error); res.status(500).json({ error: error.message }); } }); // Get all chests this.app.get('/api/chests', async (req, res) => { try { const chests = await database.getChests(); res.json({ chests }); } catch (error) { console.error('API Error /api/chests:', error); res.status(500).json({ error: error.message }); } }); // Get specific chest with shulker contents this.app.get('/api/chests/:id', async (req, res) => { try { // This would return chest + shulker details // Implementation would scan the specified chest and shulkers const chestId = parseInt(req.params.id); const chest = await database.getChestById(chestId); if (!chest) { return res.status(404).json({ error: 'Chest not found' }); } const shulkers = await database.getShulkersByChest(chestId); // Return chest + shulkers res.json({ chest, shulkers }); } catch (error) { console.error('API Error /api/chests/:id:', error); res.status(500).json({ error: error.message }); } }); // Stats this.app.get('/api/stats', async (req, res) => { try { const stats = await database.getStats(); res.json(stats); } catch (error) { console.error('API Error /api/stats:', error); res.status(500).json({ error: error.message }); } }); // Trade history this.app.get('/api/trades', async (req, res) => { try { const limit = parseInt(req.query.limit) || 50; const trades = await database.getRecentTrades(limit); res.json({ trades }); } catch (error) { console.error('API Error /api/trades:', error); res.status(500).json({ error: error.message }); } }); // Pending withdrawals (by player) this.app.get('/api/pending/:playerName', async (req, res) => { try { const playerName = req.params.playerName; const pending = await database.getPendingWithdrawals(playerName); res.json({ pending }); } catch (error) { console.error('API Error /api/pending/:playerName:', error); res.status(500).json({ error: error.message }); } }); } async close() { console.log('WebServer: Closing...'); if (this.server) { this.server.close(); console.log('WebServer: Closed'); } } getIndexHTML() { return ` Storage System

📦 Storage System

-
Loading...
📦 Inventory
Loading inventory...
🗃️ Chests
`; } } module.exports = WebServer;