← back to Dw Boardroom V2

frontend/serve.cjs

45 lines

// Simple static server with Basic Auth for Boardroom V2 UI
const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 4050;
const USER = 'admin';
const PASS = process.env.BOARDROOM_AUTH_PASS || process.env.AUTH_PASS;
if (!PASS) throw new Error('BOARDROOM_AUTH_PASS not set — route via /secrets');
const DIST = path.join(__dirname, 'dist');

const MIME = {
  '.html': 'text/html', '.js': 'application/javascript', '.css': 'text/css',
  '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
  '.svg': 'image/svg+xml', '.woff2': 'font/woff2', '.woff': 'font/woff',
};

const server = http.createServer((req, res) => {
  const auth = req.headers.authorization;
  if (!auth || !auth.startsWith('Basic ')) {
    res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="DW Boardroom V2"' });
    return res.end('Unauthorized');
  }
  const [u, p] = Buffer.from(auth.slice(6), 'base64').toString().split(':');
  if (u !== USER || p !== PASS) {
    res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="DW Boardroom V2"' });
    return res.end('Unauthorized');
  }

  let filePath = path.join(DIST, req.url === '/' ? 'index.html' : req.url);
  if (!fs.existsSync(filePath)) filePath = path.join(DIST, 'index.html');
  const ext = path.extname(filePath);
  const mime = MIME[ext] || 'application/octet-stream';
  try {
    const content = fs.readFileSync(filePath);
    res.writeHead(200, { 'Content-Type': mime });
    res.end(content);
  } catch {
    res.writeHead(404);
    res.end('Not found');
  }
});

server.listen(PORT, '0.0.0.0', () => console.log(`[Boardroom V2 UI] Serving on port ${PORT} with auth`));