Clean server
This commit is contained in:
parent
14cb50e1d2
commit
85ddb7b80f
138
server.js
138
server.js
|
@ -1,3 +1,4 @@
|
||||||
|
// Requires
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const process = require('process');
|
const process = require('process');
|
||||||
const pathtools = require('path');
|
const pathtools = require('path');
|
||||||
|
@ -6,14 +7,16 @@ const fileUpload = require("express-fileupload");
|
||||||
const { spawn } = require('child_process');
|
const { spawn } = require('child_process');
|
||||||
const pug = require('pug');
|
const pug = require('pug');
|
||||||
const unzip = require('unzip');
|
const unzip = require('unzip');
|
||||||
const app = express();
|
|
||||||
|
// Consts
|
||||||
const port = 8000;
|
const port = 8000;
|
||||||
|
const pythonPath = "/home/pytron/miniconda3/envs/pytron/bin/python"
|
||||||
const aisPath = "pytron_run/ai_manager/"
|
const aisPath = "pytron_run/ai_manager/"
|
||||||
const aisPathOld = "pytron_run/ai_manager_old"
|
const aisPathOld = "pytron_run/ai_manager_old"
|
||||||
|
|
||||||
// Create the directories to store the files
|
// Create the directories to store the files
|
||||||
try { fs.mkdirSync('pytron_run/ai_manager'); } catch { }
|
try { fs.mkdirSync(aisPath); } catch { }
|
||||||
try { fs.mkdirSync('pytron_run/ai_manager_old'); } catch { }
|
try { fs.mkdirSync(aisPathOld); } catch { }
|
||||||
|
|
||||||
let pythonRunning = false;
|
let pythonRunning = false;
|
||||||
let pythonShouldRun = false;
|
let pythonShouldRun = false;
|
||||||
|
@ -28,7 +31,7 @@ function runPython() {
|
||||||
}
|
}
|
||||||
process.stdout.write('\n');
|
process.stdout.write('\n');
|
||||||
|
|
||||||
let p = spawn('python', ['pytron_run/run.py']);
|
let p = spawn(pythonPath, ['pytron_run/run.py']);
|
||||||
p.stdout.on('data', (data) => {
|
p.stdout.on('data', (data) => {
|
||||||
let content = data.toString().split('\n');
|
let content = data.toString().split('\n');
|
||||||
for (let line of content) {
|
for (let line of content) {
|
||||||
|
@ -97,69 +100,90 @@ function parse(data) {
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
app.set('view engine', 'pug');
|
function startServer() {
|
||||||
app.use(fileUpload());
|
const app = express();
|
||||||
|
app.set('view engine', 'pug');
|
||||||
|
app.use(fileUpload());
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
|
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
|
||||||
if (err != null) {
|
if (err != null) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
let parsed = parse(data);
|
let parsed = parse(data);
|
||||||
res.render('index', parsed);
|
res.render('index', parsed);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/upload', (req, res) => {
|
app.get('/upload', (req, res) => {
|
||||||
res.render('upload', {});
|
res.render('upload', {});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/upload-target', (req, res) => {
|
app.post('/upload-target', (req, res) => {
|
||||||
let path = pathtools.join(aisPath, req.body.name)
|
let path = pathtools.join(aisPath, req.body.name)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (fs.statSync(path).isDirectory()) {
|
if (fs.statSync(path).isDirectory()) {
|
||||||
// Move it to old
|
// Move it to old
|
||||||
let version = 0;
|
let version = 0;
|
||||||
for(;;) {
|
for(;;) {
|
||||||
let movePath = pathtools.join(aisPathOld, req.body.name + '.' + version);
|
let movePath = pathtools.join(aisPathOld, req.body.name + '.' + version);
|
||||||
try {
|
try {
|
||||||
fs.statSync(movePath);
|
fs.statSync(movePath);
|
||||||
// If the sync succeded, it means that the directory already exists
|
// If the sync succeded, it means that the directory already exists
|
||||||
version++;
|
version++;
|
||||||
} catch {
|
} catch {
|
||||||
// If we're here, it means that we found the right path. We
|
// If we're here, it means that we found the right path. We
|
||||||
// will move the old one to the old directories, and save
|
// will move the old one to the old directories, and save
|
||||||
// the new one
|
// the new one
|
||||||
fs.renameSync(path, movePath);
|
fs.renameSync(path, movePath);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// Nothing to do here
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
// Nothing to do here
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.mkdirSync(path);
|
fs.mkdirSync(path);
|
||||||
|
|
||||||
let zipfile = pathtools.join(path, "archive.zip");
|
let zipfile = pathtools.join(path, "archive.zip");
|
||||||
req.files.archive.mv(zipfile, (err) => {
|
req.files.archive.mv(zipfile, (err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
fs.createReadStream(zipfile)
|
fs.createReadStream(zipfile)
|
||||||
.pipe(unzip.Extract({path}))
|
.pipe(unzip.Extract({path}))
|
||||||
.on('close', () => {
|
.on('close', () => {
|
||||||
// Trigger python_run
|
// Trigger python_run
|
||||||
runPython();
|
runPython();
|
||||||
res.send('yo');
|
res.send('yo');
|
||||||
})
|
})
|
||||||
.on('error', () => {
|
.on('error', () => {
|
||||||
res.send('an error occured while extracting the archive')
|
res.send('an error occured while extracting the archive')
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
app.use('/static', express.static('static'));
|
app.use('/static', express.static('static'));
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server listening on port ${port}!`)
|
console.log(`Server listening on port ${port}!`)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
switch (process.argv[2]) {
|
||||||
|
case 'start':
|
||||||
|
startServer();
|
||||||
|
break;
|
||||||
|
case 'python':
|
||||||
|
runPython();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log("Unknown option: " + process.argv[2]);
|
||||||
|
console.log("Usage: node server.js start|python")
|
||||||
|
process.exit(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
Loading…
Reference in New Issue