//connect to mysql: mysql --host=localhost --user=root --password=xampp
var bodyParser = require('body-parser');
var session = require("express-session");
var express = require("express");
var mysql = require("mysql");
var linereader = require("line-reader");
var app = express();
var db_config = {
host: 'localhost',
user: 'root',
password: 'xampp',
database: 'RT_Grabber_Users'
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if (err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
function athenticate(email, password, cb) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
connection.query("SELECT * FROM Login", function(err, rows) {
if (err) {
throw err;
} else {
if (re.test(email) == true) {
for (var i = 0; i < rows.length; i++) {
if (rows[i].email == email) {
if (rows[i].password == password) {
return cb(null, rows[i].id, rows[i].callsign);
} else {
return cb(new Error("Wrong Password"));
}
} else {
return cb(new Error("Email not found in our Database"));
}
} //for
} else { //if email
return cb(new Error("No valide email"));
}
}
});
}
function getGrabberSettings(callsign, GrabberName, cb) {
//as first get the user id by callsign
connection.query("SELECT * FROM Login WHERE callsign = ? ", [callsign], function(err, rows) {
var query = "SELECT * FROM Grabbers WHERE UserID = '" + rows[0].id + "' AND GrabberName = '" + GrabberName + "'";
connection.query(query, function(err, rows) {
if (err) {
return console.log(err.message);
}
connection.query("SELECT * FROM GrabberSettings WHERE GrabberID = ?", [rows[0].GrabberID], function(err, rows) {
//letzter mysql query connection wird beendet
connection.end();
var obj = {};
obj["Windows"] = {};
for (var i = 0; i < rows.length; i++) {
obj["Windows"][rows[i].WindowName] = {};
obj["Windows"][rows[i].WindowName]["UserRate"] = rows[i].UserRate;
obj["Windows"][rows[i].WindowName]["DSP_Window"] = rows[i].DSP_Window;
obj["Windows"][rows[i].WindowName]["ScrollInter"] = rows[i].ScollInter;
obj["Windows"][rows[i].WindowName]["FFTInputLength"] = rows[i].FFTInputLength;
obj["Windows"][rows[i].WindowName]["F_min"] = rows[i].F_min;
obj["Windows"][rows[i].WindowName]["F_max"] = rows[i].F_max;
obj["Windows"][rows[i].WindowName]["Offs"] = rows[i].Offset;
obj["Windows"][rows[i].WindowName]["db_from"] = rows[i].dB_from;
obj["Windows"][rows[i].WindowName]["db_to"] = rows[i].dB_to;
obj["Windows"][rows[i].WindowName]["colors"] = rows[i].WF_colors;
obj["Windows"][rows[i].WindowName]["brightness"] = rows[i].brighness;
obj["Windows"][rows[i].WindowName]["contrast"] = rows[i].contrast;
obj["Windows"][rows[i].WindowName]["wWidth"] = rows[i].wWidth;
obj["Windows"][rows[i].WindowName]["wHeight"] = rows[i].wHeight;
obj["Windows"][rows[i].WindowName]["wBgColor"] = rows[i].wBgColor;
} //ende for
return cb(obj);
});
}); //query Grabbers
});
}
function getGrabberWebsiteSettings(callsign, GrabberName, cb) {
//at first get the user id by callsign
connection.query("SELECT * FROM Login WHERE callsign = ? ", [callsign], function(err, rows) {
var query = "SELECT * FROM Grabbers WHERE UserID = '" + rows[0].id + "' AND GrabberName = '" + GrabberName + "'";
connection.query(query, function(err, rows) {
if (err) {
return console.log(err.message);
}
connection.query("SELECT * FROM WebsiteSettings WHERE GrabberID = ?", [rows[0].GrabberID], function(err, rows) {
var obj = {
"MainBgColor": rows[0].BGColor,
"GrabberID": GrabberName,
"FontFamaly": rows[0].FontFamily,
"StreamURL": rows[0].StreamPort
}
return cb(obj);
});
}); //query Grabbers
});
}
app.use(bodyParser());
app.use(session({
secret: "secretCat"
}));
app.use("/grabber/css", express.static(__dirname + "/css"));
app.use("/css", express.static(__dirname + "/css"));
app.use("/img", express.static(__dirname + "/img"));
app.use("/grabber/lib", express.static(__dirname + "/scripts"));
app.get("/", function(req, res) {
handleDisconnect();
var html = "";
if (!req.session.uid) {
html += "<!DOCTYPE html>";
html += "<head> <title> Realtime Grabber main page</title></head>";
html += "<body>";
html += "<p>Please login to view the links</p>";
html += '<a href="login">Login</a>';
html += '</body></html>';
res.send(html);
} else { //logged in
//html+= "<p>Succesfull logged in!</p>";
//html+= '<p><a href="adim"></a><br>';
//html+= '<a href="logout">Logout</a></p>';
var content = null;
renderPage(content, function(html) {
res.send(html);
});
}
});
app.get("/grabber/grabberData/:route", function(req, res) {
handleDisconnect();
var route = req.params.route;
if (route == "general") {
getGrabberWebsiteSettings(req.query.callsign, req.query.GrabberName, function(objWebsitesettings) {
res.header("content-type", "application/json");
res.header("Charset", "utf8");
res.send(JSON.stringify(objWebsitesettings));
});
}
if (route == "GrabberSettings") {
getGrabberSettings(req.query.callsign, req.query.GrabberName, function(GrabberSettings) {
res.header("content-type", "application/json");
res.header("Charset", "utf8");
res.send(JSON.stringify(GrabberSettings));
});
}
});
app.post("/login", function(req, res) {
var userName = req.body.email;
var pass = req.body.password;
athenticate(req.body.email, req.body.password, function(err, id, callsign) {
if (err) {
modifyLoginForm(err.message, function(err, result) {
if (err) {
res.send(err)
} else {
res.send(result);
}
})
} else {
if (id) {
req.session.callsign = callsign;
req.session.uid = id;
res.redirect('/');
} else {
req.session.error = "Login failed, try again";
res.redirect('login')
} //if id
}
});
});
app.listen(8000);