94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
const express = require("express");
|
|
const http = require("http");
|
|
const { Server } = require("socket.io");
|
|
const cors = require("cors");
|
|
const axios = require("axios");
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
|
|
const io = new Server(server, {
|
|
cors: { origin: "*", methods: ["GET", "POST"] },
|
|
});
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
app.post("/api/parallel-fetch", async (req, res) => {
|
|
const { socketId, url, timeout = 120_000 } = req.body;
|
|
|
|
if (!socketId || !url) {
|
|
return res.status(400).json({ error: "socketId and url are required" });
|
|
}
|
|
|
|
const socket = io.sockets.sockets.get(socketId);
|
|
if (!socket) {
|
|
return res.status(400).json({ error: `Socket ${socketId} not found` });
|
|
}
|
|
|
|
res.json({ status: "started" });
|
|
|
|
const baseUrl = "http://127.0.0.1:7088";
|
|
const endpoints = [
|
|
"/testmontools/curl",
|
|
"/testmontools/traceroute",
|
|
"/testmontools/ping",
|
|
];
|
|
|
|
// Сообщаем клиенту сколько запросов будет
|
|
socket.emit("batch:start", { url, total: endpoints.length });
|
|
|
|
const localTasks = endpoints.map((endpoint) => {
|
|
const fullUrl = baseUrl + endpoint;
|
|
const start = Date.now();
|
|
|
|
return axios({
|
|
method: "POST",
|
|
url: fullUrl,
|
|
headers: { "Content-Type": "application/json" },
|
|
data: { url },
|
|
timeout,
|
|
})
|
|
.then((response) => {
|
|
const elapsed = Date.now() - start;
|
|
console.log(`[OK] ${endpoint} (${elapsed}ms)`);
|
|
console.log("data:", response.data);
|
|
|
|
socket.emit("tool:response", {
|
|
endpoint,
|
|
ok: true,
|
|
status: response.status,
|
|
data: response.data,
|
|
elapsed,
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
const elapsed = Date.now() - start;
|
|
console.log(`[ERROR] ${endpoint} (${elapsed}ms) — ${err.message}`);
|
|
|
|
socket.emit("tool:error", {
|
|
endpoint,
|
|
ok: false,
|
|
error: err.message,
|
|
code: err.code || null,
|
|
elapsed,
|
|
});
|
|
});
|
|
});
|
|
|
|
// Когда все завершились — шлём финальное событие
|
|
Promise.allSettled(localTasks).then(() => {
|
|
console.log("=== ALL DONE ===");
|
|
socket.emit("batch:done", { url, total: endpoints.length });
|
|
});
|
|
});
|
|
|
|
app.get("/health", (_req, res) => res.json({ ok: true }));
|
|
|
|
io.on("connection", (socket) => {
|
|
console.log(`[socket] connected ${socket.id}`);
|
|
socket.on("disconnect", () => console.log(`[socket] disconnected ${socket.id}`));
|
|
});
|
|
|
|
const PORT = process.env.PORT || 4000;
|
|
server.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`)); |