-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.94 KB
/
index.js
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
const express = require('express');
const { runPython, gradePython } = require('./pythonWorker');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 8080;
app.use(cors());
app.use(express.json());
app.post('/runcode', async (req, res) => {
const { code, input } = req.body;
// validation
if (!code) {
return res.status(400).json({ message: 'Code is required' });
}
try {
const result = await runPython(code, input);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ success: false, message: error.message || error });
}
});
app.post('/grade', async (req, res) => {
try {
const { code, testCases } = req.body;
// validation
if (!code) {
return res.status(400).json({ message: 'Code is required' });
}
if (!testCases) {
return res.status(400).json({ message: 'Test cases are required' });
}
const results = [];
let grade = 0;
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
// use gradePython
const result = await gradePython(code, testCase.input, testCase.output);
results.push(result);
if (result.success) {
grade++;
}
}
grade = Math.round(grade / testCases.length * 10000) / 100; // round to 2 decimal places
res.status(200).json({ results, grade });
} catch (error) {
res.status(500).json({ success: false, message: error.message || error });
}
});
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/validate-python', (req, res) => {
const { spawn } = require('child_process');
const pythonProcess = spawn('python3', ['--version']);
pythonProcess.stdout.on('data', (data) => {
res.send(`Python version: ${data}`);
});
pythonProcess.stderr.on('data', (data) => {
res.send(`Error executing Python: ${data}`);
});
});
app.listen(port, () => {
console.log(`API server is running on port ${port}`);
});