forked from X-Financial-Technologies/Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (62 loc) · 2.05 KB
/
server.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
// server.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const app = express();
app.use(express.static('.'));
const excludedRootItems = [
'server.js',
'script.js',
'index.html', // Only exclude from root
'node_modules',
'.git',
'.gitignore',
'package.json',
'package-lock.json',
'.nixpacks',
];
function getDirectoryContents(dirPath) {
const items = fs.readdirSync(dirPath, { withFileTypes: true });
return items
.filter(item => {
// Only apply exclusions to root directory
if (dirPath === '.') {
return !excludedRootItems.includes(item.name);
}
// For subdirectories, exclude only system files
return !['node_modules', '.git', 'package.json', 'package-lock.json', '.gitignore'].includes(item.name);
})
.map(item => ({
name: item.name,
isDirectory: item.isDirectory(),
path: path.join(dirPath, item.name).replace(/\\/g, '/')
}));
}
app.get('/api/files/*', (req, res) => {
const requestedPath = req.params[0] || '.';
try {
const contents = getDirectoryContents(requestedPath);
res.json(contents);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.use('/:folder', (req, res, next) => {
const folderPath = req.params.folder;
if (fs.existsSync(`./${folderPath}/index.js`)) {
// Set up folder-specific static files
app.use(express.static(`./${folderPath}/public`));
// Handle Python script execution
app.get(`/${folderPath}/run_code`, (req, res) => {
const scriptName = req.query.script;
const scriptPath = path.join(__dirname, folderPath, 'src', scriptName);
exec(`python ${scriptPath}`, (error, stdout, stderr) => {
if (error) return res.status(500).send({ error: error.message });
res.send({ output: stdout });
});
});
}
next();
});
app.listen(3000);