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 process = require('process');
|
||||
const pathtools = require('path');
|
||||
|
@ -6,14 +7,16 @@ const fileUpload = require("express-fileupload");
|
|||
const { spawn } = require('child_process');
|
||||
const pug = require('pug');
|
||||
const unzip = require('unzip');
|
||||
const app = express();
|
||||
|
||||
// Consts
|
||||
const port = 8000;
|
||||
const pythonPath = "/home/pytron/miniconda3/envs/pytron/bin/python"
|
||||
const aisPath = "pytron_run/ai_manager/"
|
||||
const aisPathOld = "pytron_run/ai_manager_old"
|
||||
|
||||
// Create the directories to store the files
|
||||
try { fs.mkdirSync('pytron_run/ai_manager'); } catch { }
|
||||
try { fs.mkdirSync('pytron_run/ai_manager_old'); } catch { }
|
||||
try { fs.mkdirSync(aisPath); } catch { }
|
||||
try { fs.mkdirSync(aisPathOld); } catch { }
|
||||
|
||||
let pythonRunning = false;
|
||||
let pythonShouldRun = false;
|
||||
|
@ -28,7 +31,7 @@ function runPython() {
|
|||
}
|
||||
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) => {
|
||||
let content = data.toString().split('\n');
|
||||
for (let line of content) {
|
||||
|
@ -97,69 +100,90 @@ function parse(data) {
|
|||
return parsed;
|
||||
}
|
||||
|
||||
app.set('view engine', 'pug');
|
||||
app.use(fileUpload());
|
||||
function startServer() {
|
||||
const app = express();
|
||||
app.set('view engine', 'pug');
|
||||
app.use(fileUpload());
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
|
||||
if (err != null) {
|
||||
console.log(err);
|
||||
}
|
||||
let parsed = parse(data);
|
||||
res.render('index', parsed);
|
||||
app.get('/', (req, res) => {
|
||||
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
|
||||
if (err != null) {
|
||||
console.log(err);
|
||||
}
|
||||
let parsed = parse(data);
|
||||
res.render('index', parsed);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/upload', (req, res) => {
|
||||
res.render('upload', {});
|
||||
});
|
||||
app.get('/upload', (req, res) => {
|
||||
res.render('upload', {});
|
||||
});
|
||||
|
||||
app.post('/upload-target', (req, res) => {
|
||||
let path = pathtools.join(aisPath, req.body.name)
|
||||
app.post('/upload-target', (req, res) => {
|
||||
let path = pathtools.join(aisPath, req.body.name)
|
||||
|
||||
try {
|
||||
if (fs.statSync(path).isDirectory()) {
|
||||
// Move it to old
|
||||
let version = 0;
|
||||
for(;;) {
|
||||
let movePath = pathtools.join(aisPathOld, req.body.name + '.' + version);
|
||||
try {
|
||||
fs.statSync(movePath);
|
||||
// If the sync succeded, it means that the directory already exists
|
||||
version++;
|
||||
} catch {
|
||||
// If we're here, it means that we found the right path. We
|
||||
// will move the old one to the old directories, and save
|
||||
// the new one
|
||||
fs.renameSync(path, movePath);
|
||||
break;
|
||||
try {
|
||||
if (fs.statSync(path).isDirectory()) {
|
||||
// Move it to old
|
||||
let version = 0;
|
||||
for(;;) {
|
||||
let movePath = pathtools.join(aisPathOld, req.body.name + '.' + version);
|
||||
try {
|
||||
fs.statSync(movePath);
|
||||
// If the sync succeded, it means that the directory already exists
|
||||
version++;
|
||||
} catch {
|
||||
// If we're here, it means that we found the right path. We
|
||||
// will move the old one to the old directories, and save
|
||||
// the new one
|
||||
fs.renameSync(path, movePath);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Nothing to do here
|
||||
}
|
||||
} catch {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
fs.mkdirSync(path);
|
||||
fs.mkdirSync(path);
|
||||
|
||||
let zipfile = pathtools.join(path, "archive.zip");
|
||||
req.files.archive.mv(zipfile, (err) => {
|
||||
console.log(err);
|
||||
fs.createReadStream(zipfile)
|
||||
.pipe(unzip.Extract({path}))
|
||||
.on('close', () => {
|
||||
// Trigger python_run
|
||||
runPython();
|
||||
res.send('yo');
|
||||
})
|
||||
.on('error', () => {
|
||||
res.send('an error occured while extracting the archive')
|
||||
});
|
||||
let zipfile = pathtools.join(path, "archive.zip");
|
||||
req.files.archive.mv(zipfile, (err) => {
|
||||
console.log(err);
|
||||
fs.createReadStream(zipfile)
|
||||
.pipe(unzip.Extract({path}))
|
||||
.on('close', () => {
|
||||
// Trigger python_run
|
||||
runPython();
|
||||
res.send('yo');
|
||||
})
|
||||
.on('error', () => {
|
||||
res.send('an error occured while extracting the archive')
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.use('/static', express.static('static'));
|
||||
app.use('/static', express.static('static'));
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}!`)
|
||||
})
|
||||
app.listen(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