Clean server

This commit is contained in:
Pytron 2019-03-26 13:27:30 +00:00
parent 14cb50e1d2
commit 85ddb7b80f
1 changed files with 81 additions and 57 deletions

View File

@ -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,10 +100,12 @@ 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) => {
app.get('/', (req, res) => {
fs.readFile('assets/data.json', 'utf-8', (err, data) => {
if (err != null) {
console.log(err);
@ -108,13 +113,13 @@ app.get('/', (req, res) => {
let parsed = parse(data);
res.render('index', parsed);
});
});
});
app.get('/upload', (req, res) => {
app.get('/upload', (req, res) => {
res.render('upload', {});
});
});
app.post('/upload-target', (req, res) => {
app.post('/upload-target', (req, res) => {
let path = pathtools.join(aisPath, req.body.name)
try {
@ -156,10 +161,29 @@ app.post('/upload-target', (req, res) => {
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}!`)
})
})
}
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();