fix: run RODA with Vite dev

This commit is contained in:
GuavaPopper
2026-06-05 17:55:51 +07:00
parent f5dfb68bcc
commit e18641ff52
6 changed files with 116 additions and 82 deletions
+16 -55
View File
@@ -10,9 +10,11 @@ const rodaDashboardCacheTtlMs = 5 * 60 * 1000;
const dashboardRequestHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36',
};
const rodaHost = process.env.RODA_HOST || '127.0.0.1';
const rodaPort = process.env.RODA_PORT || '3000';
const rodaTarget = `http://${rodaHost}:${rodaPort}`;
let rodaDashboardHtmlCache;
let rodaDashboardDevCache = { html: '', timestamp: 0 };
function collectHtmlInputs(dir) {
const inputs = {};
@@ -124,61 +126,20 @@ function embedRodaDashboardData() {
};
}
function serveRodaDashboardApi() {
return {
name: 'serve-roda-dashboard-api',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
const requestUrl = new URL(req.url || '/', 'http://localhost');
if (requestUrl.pathname !== '/api/dashboard') {
next();
return;
}
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('METHOD_NOT_ALLOWED');
return;
}
const now = Date.now();
try {
let html = rodaDashboardDevCache.html;
let cacheStatus = 'HIT';
if (!html || (now - rodaDashboardDevCache.timestamp) >= rodaDashboardCacheTtlMs) {
html = await requestRodaDashboardHtml();
rodaDashboardDevCache = { html, timestamp: Date.now() };
cacheStatus = 'MISS';
}
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
'X-Cache': cacheStatus,
});
res.end(req.method === 'HEAD' ? '' : html);
} catch (error) {
if (rodaDashboardDevCache.html) {
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
'X-Cache': 'STALE',
});
res.end(req.method === 'HEAD' ? '' : rodaDashboardDevCache.html);
return;
}
const message = error instanceof Error ? error.message : String(error);
console.warn(`[roda-dashboard] Dev dashboard fetch failed: ${message}`);
res.writeHead(502, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('DASHBOARD_FETCH_FAILED');
}
});
},
};
}
export default defineConfig({
plugins: [serveRodaDashboardApi(), embedRodaDashboardData(), copyStaticAssets()],
plugins: [embedRodaDashboardData(), copyStaticAssets()],
server: {
proxy: {
'/groups/RODA/api/dashboard': {
target: rodaTarget,
changeOrigin: true,
},
'/api/dashboard': {
target: rodaTarget,
changeOrigin: true,
},
},
},
build: {
rollupOptions: {
input: collectHtmlInputs(root),