* Introduced a new HTML frontend for network speed testing with a responsive UI * Implemented backend server functionality to serve the frontend and handle speed test APIs * Added speed test logic for downloading and uploading data, including progress tracking and result validation * Created README-SPEEDTEST.md for documentation on application architecture, setup, and usage. * Updated package.json to include necessary scripts and dependencies for development and testing
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
#!/usr/bin/env bun
|
|
import { readFile } from 'fs/promises';
|
|
import { LoggerFactory } from '@techniker-me/logger';
|
|
|
|
const logger = LoggerFactory.getLogger('FrontendServer');
|
|
|
|
const PORT = 3001;
|
|
|
|
const server = Bun.serve({
|
|
port: PORT,
|
|
async fetch(request) {
|
|
const url = new URL(request.url);
|
|
|
|
// Serve the HTML frontend
|
|
if (url.pathname === '/' || url.pathname === '/index.html') {
|
|
try {
|
|
const html = await readFile('./frontend.html', 'utf-8');
|
|
return new Response(html, {
|
|
headers: {
|
|
'Content-Type': 'text/html',
|
|
'Cache-Control': 'no-cache'
|
|
}
|
|
});
|
|
} catch (error) {
|
|
logger.error('Failed to read frontend.html:', error);
|
|
return new Response('Frontend file not found', { status: 404 });
|
|
}
|
|
}
|
|
|
|
// Health check endpoint
|
|
if (url.pathname === '/health') {
|
|
return new Response(JSON.stringify({
|
|
status: 'ok',
|
|
frontend: 'serving',
|
|
speedtest_server: 'http://localhost:8080'
|
|
}), {
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
return new Response('Not found', { status: 404 });
|
|
}
|
|
});
|
|
|
|
logger.info(`Frontend server started on http://localhost:${PORT}`);
|
|
logger.info(`Speedtest backend server expected on http://localhost:8080`);
|
|
logger.info(`Open http://localhost:${PORT} in your browser to run the speed test`);
|
|
|
|
process.on('SIGINT', () => {
|
|
logger.info('Shutting down frontend server...');
|
|
server.stop();
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGTERM', () => {
|
|
logger.info('Shutting down frontend server...');
|
|
server.stop();
|
|
process.exit(0);
|
|
});
|