So much better now

This commit is contained in:
Thomas Forgione 2019-03-28 16:16:51 +01:00
parent bf925cfb88
commit eb364a0ce6
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
11 changed files with 58 additions and 21 deletions

5
package-lock.json generated
View File

@ -361,6 +361,11 @@
"busboy": "^0.2.14" "busboy": "^0.2.14"
} }
}, },
"express-useragent": {
"version": "1.0.12",
"resolved": "https://registry.npmjs.org/express-useragent/-/express-useragent-1.0.12.tgz",
"integrity": "sha1-W64BCakl7Js1QX8xpOitE/GRJTo="
},
"finalhandler": { "finalhandler": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz",

View File

@ -9,6 +9,7 @@
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"express": "^4.16.4", "express": "^4.16.4",
"express-fileupload": "^1.1.3-alpha.1", "express-fileupload": "^1.1.3-alpha.1",
"express-useragent": "^1.0.12",
"pug": "^2.0.3", "pug": "^2.0.3",
"touch": "^3.1.0", "touch": "^3.1.0",
"unzip": "^0.1.11" "unzip": "^0.1.11"

View File

@ -9,6 +9,7 @@ const pug = require('pug');
const unzip = require('unzip'); const unzip = require('unzip');
const touch = require('touch'); const touch = require('touch');
const bcrypt = require('bcryptjs'); const bcrypt = require('bcryptjs');
const useragent = require('express-useragent');
const { port, pythonPath, aisPath, aisPathOld, hashPath } = require('./config'); const { port, pythonPath, aisPath, aisPathOld, hashPath } = require('./config');
// Create the directories to store the files // Create the directories to store the files
@ -141,7 +142,8 @@ function readData(req, res, callback) {
fs.readFile('pytron_run/assets/data.json', 'utf-8', (err, data) => { fs.readFile('pytron_run/assets/data.json', 'utf-8', (err, data) => {
if (err != null) { if (err != null) {
console.log(err); console.log(err);
res.render('error', {message: 'It seems like the site is broken :\'('}); res.status(400);
render(req, res, 'error', {message: 'It seems like the site is broken :\'('});
return; return;
} }
let parsed = parse(data); let parsed = parse(data);
@ -161,7 +163,8 @@ function saveArchiveAndRun(req, res) {
if (err != null) { if (err != null) {
console.log("Failed to save archive"); console.log("Failed to save archive");
console.log(err); console.log(err);
res.render('error', {message: 'Unable to save the ZIP archive to the server'}); res.status(400);
render(req, res, 'error', {message: 'Unable to save the ZIP archive to the server'});
return; return;
} }
fs.createReadStream(zipfile) fs.createReadStream(zipfile)
@ -171,23 +174,36 @@ function saveArchiveAndRun(req, res) {
touch(pathtools.join(path, '__init__.py'), () => { touch(pathtools.join(path, '__init__.py'), () => {
// Trigger python_run // Trigger python_run
runPython(); runPython();
res.redirect('/'); if (req.useragent.isCurl) {
res.send('Success');
} else {
res.redirect('/');
}
}); });
}) })
.on('error', () => { .on('error', () => {
res.render('error', {message: 'Failed to unzip the archive'}); render(req, res, 'error', {message: 'Failed to unzip the archive'});
}); });
}); });
} }
function render(req, res, template, context) {
if (req.useragent.isCurl) {
res.render("txt/" + template + "_txt", context);
} else {
res.render("html/" + template + "_html", context);
}
}
function startServer() { function startServer() {
const app = express(); const app = express();
app.set('view engine', 'pug'); app.set('view engine', 'pug');
app.use(fileUpload()); app.use(fileUpload());
app.use(useragent.express());
app.get('/', (req, res) => { app.get('/', (req, res) => {
readData(req, res, (parsed) => { readData(req, res, (parsed) => {
res.render('index', { render(req, res, 'index', {
data: parsed, data: parsed,
running: pythonRunning, running: pythonRunning,
}); });
@ -204,7 +220,7 @@ function startServer() {
app.get('/leaderboard', (req, res) => { app.get('/leaderboard', (req, res) => {
readData(req, res, (parsed) => { readData(req, res, (parsed) => {
res.render('leaderboard', { render(req, res, 'leaderboard', {
data: parsed, data: parsed,
running: pythonRunning, running: pythonRunning,
}); });
@ -213,7 +229,7 @@ function startServer() {
app.get('/battles', (req, res) => { app.get('/battles', (req, res) => {
readData(req, res, (parsed) => { readData(req, res, (parsed) => {
res.render('battles', { render(req, res, 'battles', {
data: parsed, data: parsed,
running: pythonRunning, running: pythonRunning,
}); });
@ -221,28 +237,37 @@ function startServer() {
}); });
app.get('/upload', (req, res) => { app.get('/upload', (req, res) => {
res.render('upload', {}); render(req, res, 'upload', {});
}); });
app.post('/upload-target', (req, res) => { app.post('/upload-target', (req, res) => {
console.log("/upload-target"); console.log("/upload-target");
if (!req.body) {
res.status(400);
render(req, res, 'error', {message: "You have to send the form"});
}
if (!req.body.name) { if (!req.body.name) {
res.render('error', {message: "You have to enter a name in the form"}); res.status(400);
render(req, res, 'error', {message: "You have to enter a name in the form"});
return; return;
} }
if (!req.body.password) { if (!req.body.password) {
res.render('error', {message: "You have to enter a password in the form"}); res.status(400);
render(req, res, 'error', {message: "You have to enter a password in the form"});
} }
if (req.body.name.indexOf('.') !== -1) { if (req.body.name.indexOf('.') !== -1) {
res.render('error', {message: "The name of your AI can't contain dots"}); res.status(400);
render(req, res, 'error', {message: "The name of your AI can't contain dots"});
} }
if (!req.files.archive) { if (!req.files.archive) {
res.render('error', {message: "You have to send a ZIP archive"}); res.status(400);
render(req, res, 'error', {message: "You have to send a ZIP archive"});
return; return;
} }
@ -256,7 +281,6 @@ function startServer() {
fs.mkdirSync(path); fs.mkdirSync(path);
} }
if (aiExisted) { if (aiExisted) {
console.log("Ai existed, try to verify password"); console.log("Ai existed, try to verify password");
@ -264,7 +288,8 @@ function startServer() {
fs.readFile(pathtools.join(path, hashPath), 'utf-8', function(err, data) { fs.readFile(pathtools.join(path, hashPath), 'utf-8', function(err, data) {
if (err != null) { if (err != null) {
res.render('error', {message: "Couldn't read hashed password"}); res.status(400);
render(req, res, 'error', {message: "Couldn't read hashed password"});
console.log(err); console.log(err);
return; return;
} }
@ -272,13 +297,15 @@ function startServer() {
// If the AI already existed, verify the password // If the AI already existed, verify the password
bcrypt.compare(req.body.password, data, function(err, success) { bcrypt.compare(req.body.password, data, function(err, success) {
if (err != null) { if (err != null) {
res.render('error', {message: "Couldn't compare password"}); res.status(400);
render(req, res, 'error', {message: "Couldn't compare password"});
return; return;
} }
if (!success) { if (!success) {
console.log("Authentication failed"); console.log("Authentication failed");
res.render('error', {message: "Authentication failed"}); res.status(400);
render(req, res, 'error', {message: "Authentication failed"});
return; return;
} }
@ -331,7 +358,8 @@ function startServer() {
if (err != null) { if (err != null) {
console.log(err); console.log(err);
res.render('error', {message: "Couldn't hash password"}); res.status(400);
render(req, res, 'error', {message: "Couldn't hash password"});
return; return;
} }
@ -342,7 +370,8 @@ function startServer() {
if (err != null) { if (err != null) {
console.log(err); console.log(err);
res.render('error', {message: "Couldn't save hashed password"}); res.status(400);
render(req, res, 'error', {message: "Couldn't save hashed password"});
return; return;
} }

View File

@ -1,4 +1,4 @@
extends base extends base_html
block content block content
section.section section.section

View File

@ -1,4 +1,4 @@
extends base extends base_html
block extracss block extracss
link(rel="stylesheet", href="https://use.fontawesome.com/releases/v5.8.1/css/all.css", integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf", crossorigin="anonymous") link(rel="stylesheet", href="https://use.fontawesome.com/releases/v5.8.1/css/all.css", integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf", crossorigin="anonymous")

View File

@ -1,4 +1,4 @@
extends base extends base_html
block content block content
section.section section.section

0
views/txt/base_txt.pug Normal file
View File

2
views/txt/error_txt.pug Normal file
View File

@ -0,0 +1,2 @@
| #{message}
|