feat: Introduce a React-based frontend dashboard with Webpack and Babel for improved UI and development.
This commit is contained in:
34
server.js
34
server.js
@@ -3,9 +3,13 @@ import express from 'express';
|
||||
import Database from 'better-sqlite3';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import webpack from 'webpack';
|
||||
import webpackDevMiddleware from 'webpack-dev-middleware';
|
||||
import config from './webpack.config.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const compiler = webpack(config);
|
||||
|
||||
// --- CONFIGURATION ---
|
||||
const BASE_URL = 'http://www.acinfinityserver.com';
|
||||
@@ -197,7 +201,7 @@ async function poll() {
|
||||
|
||||
// --- EXPRESS SERVER ---
|
||||
const app = express();
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
|
||||
// API: Devices
|
||||
app.get('/api/devices', (req, res) => {
|
||||
@@ -242,12 +246,38 @@ app.get('/api/history', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Webpack Middleware
|
||||
// NOTE: We override publicPath to '/' here because Nginx strips the '/ac/' prefix.
|
||||
// The incoming request for '/ac/bundle.js' becomes '/bundle.js' at this server.
|
||||
const devMiddleware = webpackDevMiddleware(compiler, {
|
||||
publicPath: '/',
|
||||
writeToDisk: false,
|
||||
});
|
||||
app.use(devMiddleware);
|
||||
|
||||
// Serve index.html for root request (SPA Fallback-ish)
|
||||
app.get('/', (req, res) => {
|
||||
// Access index.html from the memory filesystem
|
||||
// We attempt to read it from the middleware's outputFileSystem
|
||||
const indexFile = path.join(config.output.path, 'index.html');
|
||||
const fs = devMiddleware.context.outputFileSystem;
|
||||
|
||||
// Simple wait/retry logic could be added here, but usually startup takes a second.
|
||||
if (fs && fs.existsSync(indexFile)) {
|
||||
const html = fs.readFileSync(indexFile);
|
||||
res.set('Content-Type', 'text/html');
|
||||
res.send(html);
|
||||
} else {
|
||||
res.status(202).send('Building... Please refresh in a moment.');
|
||||
}
|
||||
});
|
||||
|
||||
// Start Server & Daemon
|
||||
app.listen(PORT, '127.0.0.1', () => {
|
||||
console.log(`Dashboard Server running at http://127.0.0.1:${PORT}`);
|
||||
|
||||
// Start Polling Loop
|
||||
console.log(`Starting AC Infinity Poll Loop (Interval: ${POLL_INTERVAL_MS}ms)`);
|
||||
poll(); // Initial run
|
||||
// poll(); // Initial run (optional)
|
||||
setInterval(poll, POLL_INTERVAL_MS);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user