-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
113 lines (92 loc) · 2.68 KB
/
server.js
File metadata and controls
113 lines (92 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const { spawn } = require("child_process");
const path = require("path");
const app = express();
const port = 5000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Serve static files from public directory
app.use(express.static(path.join(__dirname, "public")));
// Handle favicon requests
app.get("/favicon.ico", (req, res) => {
res.status(204).end();
});
// Path to the C++ binary
const binaryPath = path.join(__dirname, "Backend", "code.exe");
// Root route - serve index.html
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Compare endpoint
app.post("/compare", (req, res) => {
const { codeA, codeB, window } = req.body;
if (!codeA || !codeB) {
return res.status(400).json({ error: "Missing codeA or codeB" });
}
const win = window || 4;
const lenA = Buffer.byteLength(codeA, "utf8");
const lenB = Buffer.byteLength(codeB, "utf8");
const header = `A ${lenA}\nB ${lenB}\n`;
const inputData = Buffer.concat([
Buffer.from(header, "utf8"),
Buffer.from(codeA, "utf8"),
Buffer.from(codeB, "utf8"),
]);
// Spawn the C++ binary process
const childProcess = spawn(binaryPath, {
env: {
...process.env,
WINDOW: String(win),
},
});
let output = "";
let errorOutput = "";
let responseSent = false;
childProcess.stdout.on("data", (data) => {
output += data.toString();
});
childProcess.stderr.on("data", (data) => {
errorOutput += data.toString();
});
childProcess.on("close", (code) => {
if (responseSent) return;
responseSent = true;
if (code !== 0) {
return res.status(500).json({
error: "C++ binary execution failed",
details: errorOutput,
});
}
// Parse and return JSON response
try {
const result = JSON.parse(output);
return res.json(result);
} catch (e) {
// If output is not JSON, return as-is
res.setHeader("Content-Type", "application/json");
return res.send(output);
}
});
// Send input data to the C++ binary
childProcess.stdin.write(inputData);
childProcess.stdin.end();
// Timeout after 30 seconds
setTimeout(() => {
if (responseSent) return;
if (!childProcess.killed) {
childProcess.kill();
responseSent = true;
return res.status(500).json({ error: "Process timeout" });
}
}, 30000);
});
// Serve static files (CSS, JS, etc.)
app.get("/:filename", (req, res) => {
res.sendFile(path.join(__dirname, "public", req.params.filename));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});