1
Fork 0
mirror of https://github.com/Steffo99/registro-fermi.git synced 2024-11-21 23:54:28 +00:00
registro-steffo/server/main.js
2016-09-15 16:37:26 +02:00

712 lines
15 KiB
JavaScript

/* Copyright © 2016 Stefano Pigozzi, Emiliano Maccaferri
* Released under the GPL v3 license
*/
//API Registro
console.log("Inizializzazione API Registro...");
var fs = require("fs");
//Tanto serve una volta sola
var config = JSON.parse(fs.readFileSync("config.json", "utf8"));
var express = require("express");
var router = express.Router();
var body = require("body-parser");
var mysql = require("mysql");
var bcrypt = require("bcrypt-nodejs");
var path = require("path");
var swig = require("swig");
var auth = require("./auth");
var marks = require("./marks");
var wu = require("./write-ups");
var utils = require("./utils");
var tests = require("./tests")
var app = express(),
swig;
console.log("Render engine caricato!");
var response;
var date = new Date();
app.use(body.urlencoded({ extended: false }));
app.use(body.json());
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'views/css')));
console.log("OK!");
console.log("Pulizia delle sessioni programmata! (" + config.timeout + " ore)");
//
app.post("/register", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
console.log(" + Richiesta da: " + req.ip);
console.log(" + Data: " + date + "\n");
var username = req.body.username;
var password = req.body.password;
var firstname = req.body.firstname;
var lastname = req.body.lastname;
var rank = req.body.rank;
var classname = req.body.classname;
if(username && password && firstname && lastname && rank && classname){
console.log("All fields inserted!");
auth.register(username, password, firstname, lastname, rank, response, classname, function(err, r){
if(!err){
res.end(JSON.stringify(r));
}else{
res.end(JSON.stringify(r));
}
});
}else{
response = {
success: false,
error: "missing_parameter"
}
console.log("Parametri mancanti!");
res.end(JSON.stringify(response));
}
});
app.post("/login", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
console.log(" + Richiesta di /login");
console.log(" + Data: " + date + "\n");
var username = req.body.username;
var password = req.body.password;
if(username && password){
console.log("Campi inseriti");
auth.login(username, password, response, function(err, r){
if(err) throw err;
res.end(JSON.stringify(r));
setTimeout(function(){
utils.flushSession(sessionid);
}, config.timeout * 60 * 60 * 1000);
console.log("Programmata la distruzione della sessione in " + config.timeout + " ore" )
});
}else{
response = {
success: false,
error: "missing_parameter"
}
console.log("Specificare tutti i campi");
console.log("RIP.");
}
setTimeout(function(){
res.end(JSON.stringify(response));
}, 1500);
});
app.post("/addMark", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionID = req.body.sessionid;
var username = req.body.username;
var mark = req.body.mark;
var subject = req.body.subject;
var desc = req.body.description;
var term = req.body.term;
if(sessionID, username, mark, subject, desc, term){
marks.addMark(sessionID, username, mark, subject, desc, term, response, function(err, r){
if(err) throw err;
res.end(JSON.stringify(r));
});
}else{
response = {
success: false,
error: "missing_parameter"
}
res.end(JSON.stringify(response));
}
});
app.post("/getMarks", function(req,res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionid = req.body.sessionid;
var student = req.body.student;
if(sessionid && student){
marks.getMarks(sessionid, student, response, function(err, resp){
if(err){
throw err;
res.end(JSON.stringify(resp));
console.log("Error!\n" + err);
}
console.log("/getMarks completato!");
res.end(JSON.stringify(resp));
});
}else{
response = {
success: false,
error: "missing_parameter"
}
res.end(JSON.stringify(response));
console.log("Parametro/i mancanti");
}
});
app.post("/alterMark", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessid = req.body.sessionid;
var markid = req.body.markid;
var newmark = req.body.newmark;
var newdesc = req.body.newdesc;
if(sessid && markid && newmark && newdesc){
marks.alterMark(sessid, newmark, newdesc, markid, response, function(err, resp){
if(err){
throw err;
res.end(JSON.stringify(resp));
}
res.end(JSON.stringify(resp));
});
}else{
response = {
success: false,
error: "missing_parameter"
}
res.end(JSON.stringify(response));
console.log("Parametro/i mancanti");
}
});
app.post("/addWriteUp", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessid = req.body.sessionid;
var w = req.body.writeup;
var username = req.body.username;
var classname = req.body.class;
if(sessid && w && username && classname){
wu.addWriteUp(sessid, w, classname, username, response, function(err, result){
if(!err){
res.end(JSON.stringify(result));
}else{
console.log("Errore fatale");
res.end(JSON.stringify(result));
}
});
}else{
response = {
success: false,
error: "missing_parameter"
}
res.end(JSON.stringify(response));
}
});
app.post("/addClassWriteUp", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessid = req.body.sessionid;
var classWriteup = req.body.writeup;
var classname = req.body.classname;
if(sessid && classWriteup && classname){
wu.addClassWriteUp(sessid, classWriteup, classname, response, function(err, resp){
if(err){
console.log("Errore fatale!");
res.end(JSON.stringify(resp));
return false;
}
console.log("OK!");
res.end(JSON.stringify(resp));
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
});
app.post("/getClassWriteUps", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var classname = req.body.classname;
var sessionid = req.body.sessionid;
if(classname && sessionid){
wu.getClassWriteUps(sessionid, classname, response, function(err, resp){
if(err){
console.log("Errore fatale!");
res.end(JSON.stringify(resp));
return false;
}
res.end(JSON.stringify(resp));
console.log("OK!");
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
});
app.post("/getWriteUps", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var student = req.body.student;
var sessionid = req.body.sessionid;
if(student && sessionid){
wu.getClassWriteUps(sessionid, student, response, function(err, resp){
if(err){
console.log("Errore fatale!");
res.end(JSON.stringify(resp));
return false;
}
res.end(JSON.stringify(resp));
console.log("OK!");
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
});
app.post("/getClassData", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var classname = req.body.class;
if(classname){
utils.getClass(classname, response, function(err, resp){
if(err){
console.log("Fatal error!");
res.end(JSON.stringify(resp));
return false;
}
res.end(JSON.stringify(resp));
});
return true;
}
console.log("Parametri mancanti");
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
});
app.post("/getClasses", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var username = req.body.username;
if(!username){
console.log("Parametri mancanti");
response = {
success: false,
errordesc: 'missing_parameter'
}
res.end(JSON.stringify(response));
}else{
utils.getProfClasses(username, response, function(err, resp){
if(err){
res.end(JSON.stringify(resp));
}else{
res.end(JSON.stringify(resp));
}
});
}
});
app.post("/addTest", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionid = req.body.sessionid;
var subject = req.body.subject;
var description = req.body.description;
var date = req.body.date;
var classname = req.body.classname;
if(sessionid, subject, description, date, classname){
tests.addTest(sessionid, classname, subject, description, date, response, function(err, response){
res.end(JSON.stringify(response));
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
return false;
});
app.post("/removeTest", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionid = req.body.sessionid;
var testid = req.body.testid;
if(sessionid && testid){
tests.removeTest(sessionid, testid, response, function(err, response){
res.end(JSON.stringify(response));
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
return false;
});
app.post("/alterTest", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionid = req.body.sessionid;
var testid = req.body.testid;
var classname = req.body.classname;
var description = req.body.description;
var date = req.body.date;
if(sessionid && testid && classname && description && date){
tests.alterTest(sessionid, classname, testid, description, date, response, function(err, response){
res.end(JSON.stringify(response));
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
return false;
});
app.post("/getTests", function(req, res){
res.header("Access-Control-Allow-Origin", "http://generalapathy.github.io");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Content-Type', "application/json");
var sessionid = req.body.sessionid;
var classname = req.body.classname;
if(sessionid && classname){
tests.getTests(sessionid, classname, response, function(err, response){
res.end(JSON.stringify(response));
});
return true;
}
response = {
success: false,
errordesc: "missing_parameter"
}
res.end(JSON.stringify(response));
return false;
});
Array.prototype.contains = function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
var server = app.listen(25000, function(){
console.log("Listening on 127.0.0.1:" + server.address().port);
});
process.on('uncaughtException', function(err){
console.error(err);
console.log("Uncaught exception... No problem!");
});
//Roba dei docs (swig è bello)
app.get("/docs", function(req, res){
res.render('docs', {
//parametri che magari potranno servire (un giorno, forse, chissà)
});
});