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,6 +100,8 @@ function parse(data) {
return parsed;
}
function startServer() {
const app = express();
app.set('view engine', 'pug');
app.use(fileUpload());
@ -163,3 +168,22 @@ app.use('/static', express.static('static'));
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();