Added more error-messages on crashing because of missing files. Closes #227

This commit is contained in:
Kasper Rynning-Tønnesen
2017-11-10 16:23:45 +01:00
parent f047ae1e1f
commit 814e1eaac9
5 changed files with 121 additions and 107 deletions

View File

@@ -1,5 +1,11 @@
var path = require('path'); var path = require('path');
var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js')); try {
var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js'));
} catch(e) {
console.log("Error - missing file");
console.log("Seems you forgot to create the file mongo_config.js in /server/config/. Have a look at mongo_config.example.js.");
process.exit();
}
var mongojs = require('mongojs'); var mongojs = require('mongojs');
var db = mongojs(mongo_config.config); var db = mongojs(mongo_config.config);

View File

@@ -1,6 +1,12 @@
var path = require('path'); var path = require('path');
var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/; var time_regex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/;
var key = require(path.join(__dirname, '../config/api_key.js')); try {
var key = require(path.join(__dirname, '../config/api_key.js'));
} catch(e) {
console.log("Error - missing file");
console.log("Seems you forgot to create the file api_key.js in /server/config/. Have a look at api_key.example.js.");
process.exit();
}
function get_correct_info(song_generated, channel, broadcast) { function get_correct_info(song_generated, channel, broadcast) {
request({ request({

View File

@@ -8,7 +8,7 @@ pathThumbnails = __dirname;
var express = require('express'); var express = require('express');
var app = express(); var app = express();
var exphbs = require('express-handlebars'); var exphbs = require('express-handlebars');
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var hbs = exphbs.create({ var hbs = exphbs.create({
defaultLayout: publicPath + '/layouts/main', defaultLayout: publicPath + '/layouts/main',
layoutsDir: publicPath + '/layouts', layoutsDir: publicPath + '/layouts',
@@ -23,6 +23,7 @@ app.enable('view cache');
app.set('views', publicPath); app.set('views', publicPath);
try{ try{
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var fs = require('fs'); var fs = require('fs');
var privateKey = fs.readFileSync(cert_config.privateKey).toString(); var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString(); var certificate = fs.readFileSync(cert_config.certificate).toString();

View File

@@ -2,118 +2,122 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
var path = require('path'); var path = require('path');
var nodemailer = require('nodemailer'); var nodemailer = require('nodemailer');
var mailconfig = require(path.join(__dirname, '../config/mailconfig.js')); try {
var mongo_db_cred = {config: 'mydb'}; var mailconfig = require(path.join(__dirname, '../config/mailconfig.js'));
var mongojs = require('mongojs'); } catch(e) {
var db = mongojs(mongo_db_cred.config); console.log("Error - missing file");
console.log("Seems you forgot to create a mailconfig.js in /server/config/. Have a look at the mailconfig.example.js.");
process.exit();
}
router.use(function(req, res, next) { router.use(function(req, res, next) {
next(); // make sure we go to the next routes and don't stop here next(); // make sure we go to the next routes and don't stop here
}); });
router.route('/api/frontpages').get(function(req, res) { router.route('/api/frontpages').get(function(req, res) {
db.collection("frontpage_lists").find({frontpage: true, count: {$gt: 0}}, function(err, docs) { db.collection("frontpage_lists").find({frontpage: true, count: {$gt: 0}}, function(err, docs) {
db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) { db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({channels: docs, viewers: tot[0].total_users})); res.send(JSON.stringify({channels: docs, viewers: tot[0].total_users}));
}); });
}); });
}); });
router.route('/api/list/:channel_name').get(function(req, res) { router.route('/api/list/:channel_name').get(function(req, res) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
var channel_name = req.params.channel_name; var channel_name = req.params.channel_name;
db.collection(channel_name).find({views: {$exists: false}}, {start: 1, end: 1, added: 1, id: 1, title: 1, votes: 1, duration: 1, type: 1, _id: 0}, function(err, docs) { db.collection(channel_name).find({views: {$exists: false}}, {start: 1, end: 1, added: 1, id: 1, title: 1, votes: 1, duration: 1, type: 1, _id: 0}, function(err, docs) {
if(docs.length > 0) { if(docs.length > 0) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs)); res.send(JSON.stringify(docs));
} else {
res.status(404);
res.send(404);
}
});
});
router.route('/api/conf/:channel_name').get(function(req, res) {
var channel_name = req.params.channel_name;
db.collection(channel_name).find({views: {$exists: true}},
{
addsongs: 1,
adminpass: 1,
allvideos: 1,
frontpage: 1,
longsongs: 1,
removeplay: 1,
shuffle: 1,
skip: 1,
startTime: 1,
userpass: 1,
vote: 1,
_id: 0
}, function(err, docs) {
if(docs.length > 0) {
var conf = docs[0];
if(conf.adminpass != "") {
conf.adminpass = true;
} else {
conf.adminpass = false;
}
if(conf.userpass != "") {
conf.userpass = true;
} else {
conf.userpass = false;
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(conf));
} else {
res.status(404);
res.send(404);
}
});
});
router.route('/api/imageblob').post(function(req, res) {
var Jimp = require("jimp");
Jimp.read('https://img.youtube.com/vi/' + req.body.id + '/mqdefault.jpg', function (err, image) {
if (err) console.log(err);
image.blur(50)
.write(path.join(pathThumbnails, '/public/assets/images/thumbnails/' + req.body.id + '.jpg'), function(e, r) {
res.send(req.body.id + ".jpg");
});
});
});
router.route('/api/mail').post(function(req, res) {
let transporter = nodemailer.createTransport(mailconfig);
transporter.verify(function(error, success) {
if (error) {
res.sendStatus(500);
return;
} else { } else {
var from = req.body.from; res.status(404);
var message = req.body.message; res.send(404);
var msg = {
from: 'no-reply@zoff.no',
to: 'contact@zoff.no',
subject: 'ZOFF: Contact form webpage',
text: message,
html: message,
replyTo: from
}
transporter.sendMail(msg, (error, info) => {
if (error) {
res.send("failed");
return;
}
res.send("success");
transporter.close();
});
} }
}); });
}); });
module.exports = router; router.route('/api/conf/:channel_name').get(function(req, res) {
var channel_name = req.params.channel_name;
db.collection(channel_name).find({views: {$exists: true}},
{
addsongs: 1,
adminpass: 1,
allvideos: 1,
frontpage: 1,
longsongs: 1,
removeplay: 1,
shuffle: 1,
skip: 1,
startTime: 1,
userpass: 1,
vote: 1,
_id: 0
}, function(err, docs) {
if(docs.length > 0) {
var conf = docs[0];
if(conf.adminpass != "") {
conf.adminpass = true;
} else {
conf.adminpass = false;
}
if(conf.userpass != "") {
conf.userpass = true;
} else {
conf.userpass = false;
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(conf));
} else {
res.status(404);
res.send(404);
}
});
});
router.route('/api/imageblob').post(function(req, res) {
var Jimp = require("jimp");
Jimp.read('https://img.youtube.com/vi/' + req.body.id + '/mqdefault.jpg', function (err, image) {
if (err) console.log(err);
image.blur(50)
.write(path.join(pathThumbnails, '/public/assets/images/thumbnails/' + req.body.id + '.jpg'), function(e, r) {
res.send(req.body.id + ".jpg");
});
});
});
router.route('/api/mail').post(function(req, res) {
let transporter = nodemailer.createTransport(mailconfig);
transporter.verify(function(error, success) {
if (error) {
res.sendStatus(500);
return;
} else {
var from = req.body.from;
var message = req.body.message;
var msg = {
from: 'no-reply@zoff.no',
to: 'contact@zoff.no',
subject: 'ZOFF: Contact form webpage',
text: message,
html: message,
replyTo: from
}
transporter.sendMail(msg, (error, info) => {
if (error) {
res.send("failed");
return;
}
res.send("success");
transporter.close();
});
}
});
});
module.exports = router;

View File

@@ -1,9 +1,6 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var path = require('path'); var path = require('path');
var mongo_db_cred = {config: 'mydb'};
var mongojs = require('mongojs');
var db = mongojs(mongo_db_cred.config);
router.use(function(req, res, next) { router.use(function(req, res, next) {
next(); // make sure we go to the next routes and don't stop here next(); // make sure we go to the next routes and don't stop here