Files
reaktor/packages/server/server.js
Jose Luis b058997889 refactor: restructure to monorepo with npm workspaces (Phase 0)
Move frontend to packages/client/, server to packages/server/.
Root package.json uses npm workspaces to orchestrate both.

Structure:
  reaktor/
    packages/client/  (React + Vite + Tone.js frontend)
    packages/server/  (static file server, future API)
    dist/             (built output, shared)
    docker-compose.yml (app + PostgreSQL for future backend)

- npm run dev → runs Vite dev server from client workspace
- npm run build → builds client, outputs to root dist/
- npm run start → runs server.js serving dist/
- Dockerfile updated for multi-stage monorepo build
- docker-compose.yml added with PostgreSQL service (ready for Phase 1)
- All imports and paths preserved, zero functionality change

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 19:52:57 +01:00

48 lines
1.5 KiB
JavaScript

// Lightweight static file server for Reaktor modular synth
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 80;
const STATIC_DIR = path.join(__dirname, '..', '..', 'dist');
const MIME = {
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
'.svg': 'image/svg+xml', '.ico': 'image/x-icon', '.woff2': 'font/woff2',
'.wasm': 'application/wasm',
};
const server = http.createServer((req, res) => {
let filePath = path.join(STATIC_DIR, req.url === '/' ? 'index.html' : req.url);
// SPA fallback: if file doesn't exist, serve index.html
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
filePath = path.join(STATIC_DIR, 'index.html');
}
if (!filePath.startsWith(STATIC_DIR)) {
res.writeHead(403); res.end('Forbidden'); return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(err.code === 'ENOENT' ? 404 : 500);
res.end(err.code === 'ENOENT' ? 'Not found' : 'Server error');
return;
}
if (['.png', '.jpg', '.woff2', '.js', '.css'].includes(ext)) {
res.setHeader('Cache-Control', 'public, max-age=604800');
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`[reaktor] Running on port ${PORT}`);
});