Clean server
This commit is contained in:
		
							parent
							
								
									14cb50e1d2
								
							
						
					
					
						commit
						85ddb7b80f
					
				
							
								
								
									
										32
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								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,6 +100,8 @@ function parse(data) {
 | 
				
			|||||||
    return parsed;
 | 
					    return parsed;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function startServer() {
 | 
				
			||||||
 | 
					    const app = express();
 | 
				
			||||||
    app.set('view engine', 'pug');
 | 
					    app.set('view engine', 'pug');
 | 
				
			||||||
    app.use(fileUpload());
 | 
					    app.use(fileUpload());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -163,3 +168,22 @@ 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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user