Project rename
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
var mysql = require('mysql');
|
||||
|
||||
var app = null
|
||||
var pool = null
|
||||
const connectionLimit = 66
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
app = express
|
||||
|
||||
app.get('LOG').log('Database | Server to connect to is ' + app.get('CONF').database.host + '')
|
||||
|
||||
pool = mysql.createPool({
|
||||
connectionLimit : connectionLimit,
|
||||
host : app.get('CONF').database.host,
|
||||
port : app.get('CONF').database.port,
|
||||
database : app.get('CONF').database.database,
|
||||
user : app.get('CONF').database.user,
|
||||
password : app.get('CONF').database.password,
|
||||
waitForConnections : true
|
||||
});
|
||||
|
||||
var connectionCountTemp = 0;
|
||||
setInterval(function() {
|
||||
const connectionCount = pool._freeConnections.length
|
||||
if(connectionCount != connectionCountTemp) {
|
||||
connectionCountTemp = connectionCount
|
||||
|
||||
if(connectionCount > (connectionLimit / 2)) {
|
||||
app.get('LOG').error(`Database | Connection count: ${connectionCount}`)
|
||||
} else {
|
||||
app.get('LOG').debug(`Connection count: ${connectionCount}`)
|
||||
}
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.getItem = function(identifier, callback) {
|
||||
const sql = `SELECT * FROM items WHERE identifier = '${identifier}'`
|
||||
|
||||
pool.query(sql, function (error, results, fields) {
|
||||
if (error) throw error;
|
||||
|
||||
callback(results[0])
|
||||
});
|
||||
}
|
||||
|
||||
exports.getItemList = function(callback) {
|
||||
const sql = `SELECT * FROM items i ORDER BY i.order DESC`
|
||||
|
||||
pool.query(sql, function (error, results, fields) {
|
||||
if (error) throw error;
|
||||
|
||||
callback(results)
|
||||
});
|
||||
}
|
||||
|
||||
exports.getNotAvailableList = function(itemID, callback) {
|
||||
const sql = `SELECT a.item, a.from AS 'start', a.to as 'end' FROM available a WHERE a.item = ${itemID} ORDER BY a.from ASC`
|
||||
|
||||
pool.query(sql, function (error, results, fields) {
|
||||
if (error) throw error;
|
||||
|
||||
callback(results)
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* File: app/helper.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Swiss-knife.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var logger = require('./logging')
|
||||
, jsonlint = require("jsonlint");
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.timeSince = function(date) {
|
||||
|
||||
var seconds = Math.floor((new Date() - date) / 1000);
|
||||
|
||||
var interval = Math.floor(seconds / 31536000);
|
||||
|
||||
if (interval > 1) {
|
||||
return interval + " years";
|
||||
}
|
||||
interval = Math.floor(seconds / 2592000);
|
||||
if (interval > 1) {
|
||||
return interval + " months";
|
||||
}
|
||||
interval = Math.floor(seconds / 86400);
|
||||
if (interval > 1) {
|
||||
return interval + " days";
|
||||
}
|
||||
interval = Math.floor(seconds / 3600);
|
||||
if (interval > 1) {
|
||||
return interval + " hours";
|
||||
}
|
||||
interval = Math.floor(seconds / 60);
|
||||
if (interval > 1) {
|
||||
return interval + " minutes";
|
||||
}
|
||||
return Math.floor(seconds) + " seconds";
|
||||
}
|
||||
|
||||
exports.isOlderThanXMinutes = function(date, minutes) {
|
||||
var seconds = Math.floor((new Date() - date) / 1000)
|
||||
interval = seconds / 60
|
||||
|
||||
return interval > minutes
|
||||
}
|
||||
|
||||
exports.dayOfYear = function() {
|
||||
var now = new Date();
|
||||
var start = new Date(now.getFullYear(), 0, 0);
|
||||
var diff = now - start;
|
||||
var oneDay = 1000 * 60 * 60 * 24;
|
||||
var day = Math.floor(diff / oneDay);
|
||||
|
||||
logger.trace('Helper | day of year: ' + day);
|
||||
return day;
|
||||
}
|
||||
|
||||
exports.isJSON = function(jsonString) {
|
||||
try {
|
||||
return jsonlint.parse(jsonString)
|
||||
} catch (e) {
|
||||
if(logger.isDebug()) {
|
||||
logger.error(e.message)
|
||||
//logger.inspect('Error checking JSON', e)
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.isArray = function(toCheck) {
|
||||
return (Object.prototype.toString.call( toCheck ) === '[object Array]');
|
||||
}
|
||||
|
||||
exports.isDefinedAndNotNull = function(toCheck) {
|
||||
return (
|
||||
typeof toCheck !== 'undefined' &&
|
||||
toCheck != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.getRandomInt = function(min, max) {
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
|
||||
exports.bytesToSize = function(bytes, precision){
|
||||
var kilobyte = 1024;
|
||||
var megabyte = kilobyte * 1024;
|
||||
var gigabyte = megabyte * 1024;
|
||||
var terabyte = gigabyte * 1024;
|
||||
|
||||
if ((bytes >= 0) && (bytes < kilobyte)) {
|
||||
return bytes + ' B';
|
||||
|
||||
} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
|
||||
return (bytes / kilobyte).toFixed(precision) + ' KB';
|
||||
|
||||
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
|
||||
return (bytes / megabyte).toFixed(precision) + ' MB';
|
||||
|
||||
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
|
||||
return (bytes / gigabyte).toFixed(precision) + ' GB';
|
||||
|
||||
} else if (bytes >= terabyte) {
|
||||
return (bytes / terabyte).toFixed(precision) + ' TB';
|
||||
|
||||
} else {
|
||||
return bytes + ' B';
|
||||
}
|
||||
}
|
||||
|
||||
/* private */
|
||||
function bytesToGB(bytes, precision) {
|
||||
var kilobyte = 1024;
|
||||
var megabyte = kilobyte * 1024;
|
||||
var gigabyte = megabyte * 1024;
|
||||
var terabyte = gigabyte * 1024;
|
||||
|
||||
return (bytes / gigabyte).toFixed(precision);
|
||||
}
|
||||
|
||||
/* private */
|
||||
function bytesToMB(bytes, precision) {
|
||||
var kilobyte = 1024;
|
||||
var megabyte = kilobyte * 1024;
|
||||
var gigabyte = megabyte * 1024;
|
||||
var terabyte = gigabyte * 1024;
|
||||
|
||||
return (bytes / megabyte).toFixed(precision);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* File: app/logging.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Logging with several log-level.
|
||||
* - debug: Printed only in development-state.
|
||||
* - log: Needed log-output.
|
||||
* - error: Prints log in error-level.
|
||||
* - inspect: Prints a message and details of an object.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var Conf = require('../config.json')
|
||||
, Inspect = require('eyespect').inspector()
|
||||
, Helper = require('./helper.js')
|
||||
, DateFormat = require('dateformat');
|
||||
|
||||
|
||||
var eyeFriendly = (Conf.logging.mode === 'friendly');
|
||||
var isTrace = (Conf.logging.trace === 'yes');
|
||||
var isDebug = true;
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.isEyeFriendlyMode = function() {
|
||||
return eyeFriendly;
|
||||
}
|
||||
|
||||
exports.isTrace = function() {
|
||||
return isTrace;
|
||||
}
|
||||
|
||||
exports.isDebug = function() {
|
||||
return isDebug;
|
||||
}
|
||||
|
||||
exports.trace = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " TRACE " + msg;
|
||||
if(isTrace) {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.debug = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg;
|
||||
|
||||
if(isDebug) {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.log = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg;
|
||||
consoleOut(msg);
|
||||
}
|
||||
|
||||
exports.error = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg;
|
||||
consoleErr(msg);
|
||||
}
|
||||
|
||||
exports.inspect = function(msg, content) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INSPECT " + msg;
|
||||
|
||||
if(eyeFriendly) {
|
||||
Inspect(content, msg);
|
||||
} else {
|
||||
var msgOut = msg + (Helper.isDefinedAndNotNull(content) ? ": "+JSON.stringify(content) : "");
|
||||
consoleOut(msgOut);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* private */
|
||||
function consoleOut(msg, content) {
|
||||
var hasContent = Helper.isDefinedAndNotNull(content);
|
||||
|
||||
if(hasContent) {
|
||||
var msgAddon = (hasContent ? " | " + content : "");
|
||||
if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg + msgAddon);
|
||||
}
|
||||
} else if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/* private */
|
||||
function consoleErr(msg, content) {
|
||||
var hasContent = Helper.isDefinedAndNotNull(content);
|
||||
|
||||
if(hasContent) {
|
||||
var msgAddon = (hasContent ? " | " + content : "");
|
||||
if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.error(msg + msgAddon);
|
||||
}
|
||||
} else if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.error(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
const nodemailer = require("nodemailer");
|
||||
const mailValidator = require("email-validator");
|
||||
|
||||
var app = null
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
app = express
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.mail = function(data) {
|
||||
const account = {
|
||||
user: app.get('CONF').mail.smtp.auth.user,
|
||||
pass: app.get('CONF').mail.smtp.auth.pass
|
||||
}
|
||||
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: app.get('CONF').mail.smtp.host,
|
||||
port: app.get('CONF').mail.smtp.port,
|
||||
secure: app.get('CONF').mail.smtp.secure, // true for 465, false for other ports
|
||||
auth: {
|
||||
user: account.user, // generated ethereal user
|
||||
pass: account.pass // generated ethereal password
|
||||
}
|
||||
});
|
||||
|
||||
// setup email data with unicode symbols
|
||||
let mailOptions = {
|
||||
from: extractFrom(data), // sender address
|
||||
to: app.get('CONF').mail.request.to, // comma separated list of receivers
|
||||
subject: "Equipment Anfrage", // Subject line
|
||||
text: "Hello world?", // plain text body
|
||||
html: "<b>Hello world?</b>" // html body
|
||||
};
|
||||
|
||||
let info = transporter.sendMail(mailOptions)
|
||||
|
||||
app.get('LOG').log("Mailer | Message sent: %s", info.messageId);
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
function extractFrom(data) {
|
||||
if(mailValidator.validate(data.contact)) {
|
||||
var name = data.name && data.name.length > 0 ? data.name : "👻"
|
||||
var mail = data.contact
|
||||
return `"${name}" ${mail}`
|
||||
} else {
|
||||
return `"👻" ${app.get('CONF').mail.from}`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user