|
| 1 | +import http from "http"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +const PORT = 3001; |
| 6 | + |
| 7 | +interface MimeTypes { |
| 8 | + [key: string]: string; |
| 9 | +} |
| 10 | + |
| 11 | +const MIME_TYPES: MimeTypes = { |
| 12 | + ".html": "text/html", |
| 13 | + ".js": "text/javascript", |
| 14 | + ".css": "text/css", |
| 15 | + ".json": "application/json", |
| 16 | + ".png": "image/png", |
| 17 | + ".jpg": "image/jpg", |
| 18 | + ".gif": "image/gif", |
| 19 | + ".svg": "image/svg+xml", |
| 20 | + ".ico": "image/x-icon", |
| 21 | +}; |
| 22 | + |
| 23 | +const server = http.createServer((req, res) => { |
| 24 | + console.log(`${req.method} ${req.url}`); |
| 25 | + |
| 26 | + if (!req.url) { |
| 27 | + res.writeHead(400); |
| 28 | + res.end("Bad request: URL missing"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Handle the root path |
| 33 | + let filePath: string; |
| 34 | + if (req.url === "/") { |
| 35 | + filePath = path.join(__dirname, "../public/dashboard.html"); |
| 36 | + } else if (req.url === "/index.js") { |
| 37 | + // Serve the main JS file from public folder |
| 38 | + filePath = path.join(__dirname, "../public/index.js"); |
| 39 | + } else if (req.url === "/index.css") { |
| 40 | + // Serve the main CSS file from public folder |
| 41 | + filePath = path.join(__dirname, "../public/index.css"); |
| 42 | + } else if (req.url.startsWith("/metrics/")) { |
| 43 | + // Serve metrics files from the metrics directory |
| 44 | + const metricsPath = |
| 45 | + req.url === "/metrics/summary.json" |
| 46 | + ? path.join(__dirname, "../metrics", req.url.replace("/metrics/", "")) |
| 47 | + : path.join( |
| 48 | + __dirname, |
| 49 | + "../metrics/tasks", |
| 50 | + req.url.replace("/metrics/", ""), |
| 51 | + ); |
| 52 | + filePath = metricsPath; |
| 53 | + } else if (req.url.startsWith("/docs/")) { |
| 54 | + // Serve docs assets |
| 55 | + filePath = path.join(__dirname, "../public", req.url); |
| 56 | + } else { |
| 57 | + // Serve other static files from the public directory |
| 58 | + filePath = path.join(__dirname, "../public", req.url); |
| 59 | + } |
| 60 | + |
| 61 | + // Get the file extension |
| 62 | + const extname = path.extname(filePath); |
| 63 | + const contentType = MIME_TYPES[extname] || "application/octet-stream"; |
| 64 | + |
| 65 | + // Read the file |
| 66 | + fs.readFile(filePath, (err, content) => { |
| 67 | + if (err) { |
| 68 | + if (err.code === "ENOENT") { |
| 69 | + // Page not found |
| 70 | + console.error(`File not found: ${filePath}`); |
| 71 | + res.writeHead(404); |
| 72 | + res.end("404 Not Found"); |
| 73 | + } else { |
| 74 | + // Server error |
| 75 | + console.error(`Server error: ${err.code}`); |
| 76 | + res.writeHead(500); |
| 77 | + res.end(`Server Error: ${err.code}`); |
| 78 | + } |
| 79 | + } else { |
| 80 | + // Success |
| 81 | + res.writeHead(200, { "Content-Type": contentType }); |
| 82 | + res.end(content, "utf-8"); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +server.listen(PORT, () => { |
| 88 | + console.log(`Server running at http://localhost:${PORT}/`); |
| 89 | + console.log("Press Ctrl+C to stop the server"); |
| 90 | +}); |
0 commit comments