Merge pull request #35 from lbryio/logging

Logging
This commit is contained in:
Bill Bittner 2017-06-19 20:24:31 -07:00 committed by GitHub
commit bc37532168
20 changed files with 159 additions and 115 deletions

View file

@ -8,5 +8,9 @@
"Database": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"PublishUploadPath": "none" "PublishUploadPath": "none"
},
"Logging": {
"LogLevel": "none",
"LogDirectory": "none"
} }
} }

View file

@ -8,5 +8,9 @@
"Database": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"PublishUploadPath": "C:\\lbry\\speech\\hosted_content" "PublishUploadPath": "C:\\lbry\\speech\\hosted_content"
},
"Logging": {
"LogLevel": "debug",
"LogDirectory": "C:\\lbry\\speech\\logs\\"
} }
} }

View file

@ -8,5 +8,9 @@
"Database": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"PublishUploadPath": "/home/lbry/Downloads/" "PublishUploadPath": "/home/lbry/Downloads/"
},
"Logging": {
"LogLevel": "debug",
"LogDirectory": "/home/lbry/Logs"
} }
} }

View file

@ -8,5 +8,9 @@
"Database": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"PublishUploadPath": "/home/ubuntu/Downloads/" "PublishUploadPath": "/home/ubuntu/Downloads/"
},
"Logging": {
"LogLevel": "debug",
"LogDirectory": "/home/ubuntu/Logs"
} }
} }

View file

@ -1,12 +1,12 @@
const fs = require('fs'); const fs = require('fs');
const logger = require('winston');
const lbryApi = require('../helpers/libraries/lbryApi.js'); const lbryApi = require('../helpers/libraries/lbryApi.js');
const config = require('config'); const config = require('config');
const walledAddress = config.get('WalletConfig.lbryAddress');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const walledAddress = config.get('WalletConfig.lbryAddress');
function createPublishParams (claim, filePath, license, nsfw) { function createPublishParams (claim, filePath, license, nsfw) {
console.log('nsfw:', nsfw, typeof nsfw); logger.debug(`Creating Publish Parameters for "${claim}"`);
const publishParams = { const publishParams = {
name : claim, name : claim,
file_path: filePath, file_path: filePath,
@ -28,7 +28,7 @@ function createPublishParams (claim, filePath, license, nsfw) {
function deleteTemporaryFile (filePath) { function deleteTemporaryFile (filePath) {
fs.unlink(filePath, err => { fs.unlink(filePath, err => {
if (err) throw err; if (err) throw err;
console.log(`successfully deleted ${filePath}`); logger.debug(`successfully deleted ${filePath}`);
}); });
} }
@ -44,13 +44,12 @@ module.exports = {
lbryApi lbryApi
.publishClaim(publishParams, fileName, fileType) .publishClaim(publishParams, fileName, fileType)
.then(result => { .then(result => {
logger.info(`Successfully published ${fileName}`);
visitor.event('Publish Route', 'Publish Success', filePath).send(); visitor.event('Publish Route', 'Publish Success', filePath).send();
console.log('publish promise success. Tx info:', result);
socket.emit('publish-complete', { name: claim, result }); socket.emit('publish-complete', { name: claim, result });
}) })
.catch(error => { .catch(error => {
visitor.event('Publish Route', 'Publish Failure', filePath).send(); visitor.event('Publish Route', 'Publish Failure', filePath).send();
console.log('error:', error);
socket.emit('publish-failure', errorHandlers.handlePublishError(error)); socket.emit('publish-failure', errorHandlers.handlePublishError(error));
deleteTemporaryFile(filePath); deleteTemporaryFile(filePath);
}); });

View file

@ -1,6 +1,6 @@
const lbryApi = require('../helpers/libraries/lbryApi.js'); const lbryApi = require('../helpers/libraries/lbryApi.js');
const db = require('../models'); const db = require('../models');
const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
@ -9,9 +9,9 @@ function getClaimAndHandleResponse (claimUri, resolve, reject) {
.getClaim(claimUri) .getClaim(claimUri)
.then(({ file_name, download_path, mime_type }) => { .then(({ file_name, download_path, mime_type }) => {
resolve({ resolve({
file_name, fileName: file_name,
file_path: download_path, filePath: download_path,
file_type: mime_type, fileType: mime_type,
}); });
}) })
.catch(error => { .catch(error => {
@ -22,34 +22,31 @@ function getClaimAndHandleResponse (claimUri, resolve, reject) {
module.exports = { module.exports = {
getClaimByName (claimName) { getClaimByName (claimName) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryHelpers >> getClaim BasedOnNameOnly:', claimName);
// get all free public claims // get all free public claims
getAllFreePublicClaims(claimName) getAllFreePublicClaims(claimName)
.then(freePublicClaimList => { .then(freePublicClaimList => {
const claimId = freePublicClaimList[0].claim_id; const claimId = freePublicClaimList[0].claim_id;
const name = freePublicClaimList[0].name; const name = freePublicClaimList[0].name;
const freePublicClaimOutpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`; const freePublicClaimOutpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
const freePublicClaimUri = name + '#' + claimId; const freePublicClaimUri = `${name}#${claimId}`;
console.log('>> Decided on public claim id:', claimId);
// check to see if the file is available locally // check to see if the file is available locally
db.File db.File
.findOne({ where: { name: name, claim_id: claimId } }) .findOne({ where: { name: name, claimId: claimId } })
.then(claim => { .then(claim => {
// if a matching claim is found locally... // if a matching claim is found locally...
if (claim) { if (claim) {
console.log('>> A matching claim_id was found locally'); logger.debug(`A mysql record was found for ${claimId}`);
// if the outpoint's match return it // if the outpoint's match return it
if (claim.dataValues.outpoint === freePublicClaimOutpoint) { if (claim.dataValues.outpoint === freePublicClaimOutpoint) {
console.log('>> Local outpoint matched'); logger.debug(`local outpoint matched for ${claimId}`);
resolve(claim.dataValues); resolve(claim.dataValues);
// if the outpoint's don't match, fetch updated claim // if the outpoint's don't match, fetch updated claim
} else { } else {
console.log('>> local outpoint did not match'); logger.debug(`local outpoint did not match for ${claimId}`);
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject); getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
} }
// ... otherwise use daemon to retrieve it // ... otherwise use daemon to retrieve it
} else { } else {
// 'get' the claim
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject); getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
} }
}) })
@ -66,35 +63,30 @@ module.exports = {
getClaimByClaimId (claimName, claimId) { getClaimByClaimId (claimName, claimId) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
const uri = `${claimName}#${claimId}`; const uri = `${claimName}#${claimId}`;
console.log('>> lbryHelpers >> getClaimBasedOnUri:', uri);
// resolve the Uri // resolve the Uri
lbryApi lbryApi
.resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne() .resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne()
.then(result => { .then(result => {
// note should just be 'result' returned.
// get the outpoint
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`; const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
// check locally for the claim // check locally for the claim
db.File db.File
.findOne({ where: { claim_id: claimId } }) .findOne({ where: { name: claimName, claimId: claimId } })
.then(claim => { .then(claim => {
// if a found locally... // if a found locally...
if (claim) { if (claim) {
console.log('>> A matching claim_id was found locally'); logger.debug(`A mysql record was found for ${claimId}`);
// if the outpoint's match return it // if the outpoint's match return it
if (claim.dataValues.outpoint === resolvedOutpoint) { if (claim.dataValues.outpoint === resolvedOutpoint) {
console.log('>> Local outpoint matched'); logger.debug(`local outpoint matched for ${claimId}`);
resolve(claim.dataValues); resolve(claim.dataValues);
// if the outpoint's don't match, fetch updated claim // if the outpoint's don't match, fetch updated claim
} else { } else {
console.log('>> Local outpoint did not match'); logger.debug(`local outpoint did not match for ${claimId}`);
getClaimAndHandleResponse(uri, resolve, reject); getClaimAndHandleResponse(uri, resolve, reject);
} }
// ... otherwise use daemon to retrieve it // ... otherwise use daemon to retrieve it
} else { } else {
// check to make sure it is free and public (note: no need for another resolve?)
if (isFreePublicClaim(result[uri].claim)) { if (isFreePublicClaim(result[uri].claim)) {
// 'get' the claim
getClaimAndHandleResponse(uri, resolve, reject); getClaimAndHandleResponse(uri, resolve, reject);
} else { } else {
reject('NO_FREE_PUBLIC_CLAIMS'); reject('NO_FREE_PUBLIC_CLAIMS');

View file

@ -1,8 +1,9 @@
const isFreePublicClaim = require('./isFreePublicClaim.js'); const isFreePublicClaim = require('./isFreePublicClaim.js');
const lbryApi = require('../libraries/lbryApi.js'); const lbryApi = require('../libraries/lbryApi.js');
const logger = require('winston');
function filterForFreePublicClaims (claimsListArray) { function filterForFreePublicClaims (claimsListArray) {
// console.log("claims list:", claimsListArray) logger.debug('filtering claims list for free, public claims');
if (!claimsListArray) { if (!claimsListArray) {
return null; return null;
} }
@ -16,12 +17,12 @@ function filterForFreePublicClaims (claimsListArray) {
} }
function orderTopClaims (claimsListArray) { function orderTopClaims (claimsListArray) {
console.log('>> orderTopClaims'); logger.debug('ordering the top claims');
claimsListArray.sort(({ amountA, heightA }, { amountB, heightB }) => { claimsListArray.sort((a, b) => {
if (amountA === amountB) { if (a.amount === b.amount) {
return heightA > heightB; return a.height < b.height;
} else { } else {
return amountA < amountB; return a.amount < b.amount;
} }
}); });
return claimsListArray; return claimsListArray;
@ -34,11 +35,11 @@ module.exports = claimName => {
.getClaimsList(claimName) .getClaimsList(claimName)
.then(({ claims }) => { .then(({ claims }) => {
const claimsList = claims; const claimsList = claims;
console.log('>> Number of claims:', claimsList.length); logger.debug(`Number of claims: ${claimsList.length}`);
// return early if no claims were found // return early if no claims were found
if (claimsList.length === 0) { if (claimsList.length === 0) {
reject('NO_CLAIMS'); reject('NO_CLAIMS');
console.log('exiting due to lack of claims'); logger.debug('exiting due to lack of claims');
return; return;
} }
// filter the claims to return only free, public claims // filter the claims to return only free, public claims
@ -46,7 +47,7 @@ module.exports = claimName => {
// return early if no free, public claims were found // return early if no free, public claims were found
if (!freePublicClaims || freePublicClaims.length === 0) { if (!freePublicClaims || freePublicClaims.length === 0) {
reject('NO_FREE_PUBLIC_CLAIMS'); reject('NO_FREE_PUBLIC_CLAIMS');
console.log('exiting due to lack of free or public claims'); logger.debug('exiting due to lack of free or public claims');
return; return;
} }
// order the claims // order the claims
@ -55,7 +56,7 @@ module.exports = claimName => {
resolve(orderedPublicClaims); resolve(orderedPublicClaims);
}) })
.catch(error => { .catch(error => {
console.log(">> 'claim_list' error"); logger.error('error received from lbryApi.getClaimsList', error);
reject(error); reject(error);
}); });
}); });

View file

@ -1,5 +1,7 @@
const logger = require('winston');
module.exports = ({ value }) => { module.exports = ({ value }) => {
console.log('>> check: isFreePublicClaim?'); logger.debug('checking isFreePublicClaim ?');
if ( if (
(value.stream.metadata.license.indexOf('Public Domain') !== -1 || value.stream.metadata.license.indexOf('Creative Commons') !== -1) && (value.stream.metadata.license.indexOf('Public Domain') !== -1 || value.stream.metadata.license.indexOf('Creative Commons') !== -1) &&
(!value.stream.metadata.fee || value.stream.metadata.fee.amount === 0) (!value.stream.metadata.fee || value.stream.metadata.fee.amount === 0)

View file

@ -1,5 +1,8 @@
const logger = require('winston');
module.exports = { module.exports = {
handleRequestError (error, res) { handleRequestError (error, res) {
logger.error('Request Error,', error);
if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') { if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') {
res.status(307).render('noClaims'); res.status(307).render('noClaims');
} else if (error.response) { } else if (error.response) {
@ -11,6 +14,7 @@ module.exports = {
} }
}, },
handlePublishError (error) { handlePublishError (error) {
logger.error('Publish Error,', error);
if (error.code === 'ECONNREFUSED') { if (error.code === 'ECONNREFUSED') {
return 'Connection refused. The daemon may not be running.'; return 'Connection refused. The daemon may not be running.';
} else if (error.response.data.error) { } else if (error.response.data.error) {

View file

@ -1,51 +1,49 @@
const axios = require('axios'); const axios = require('axios');
const db = require('../../models'); const db = require('../../models');
const logger = require('winston');
function createFilesRecord (name, claimId, outpoint, fileName, filePath, fileType, nsfw) {
db.File
.create({ name, claimId, outpoint, fileName, filePath, fileType, nsfw })
.catch(error => {
logger.error(`Sequelize File.create error`, error);
});
}
module.exports = { module.exports = {
publishClaim (publishParams, fileName, fileType) { publishClaim (publishParams, fileName, fileType) {
logger.debug(`Publishing claim for "${fileName}"`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> publishClaim:', publishParams);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'publish', method: 'publish',
params: publishParams, params: publishParams,
}) })
.then(response => { .then(response => {
console.log(">> 'publish' success", response);
const result = response.data.result; const result = response.data.result;
db.File createFilesRecord(
.create({ publishParams.name, result.claim_id, `${result.txid}:${result.nout}`, fileName, publishParams.file_path, fileType, publishParams.metadata.nsfw);
name : publishParams.name,
claim_id : result.claim_id,
outpoint : `${result.txid}:${result.nout}`,
file_name: fileName,
file_path: publishParams.file_path,
file_type: fileType,
nsfw : publishParams.metadata.nsfw,
})
.catch(error => {
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result); resolve(result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'publish' error");
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
getClaim (uri) { getClaim (uri) {
logger.debug(`Getting Claim for "${uri}"`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> getClaim:', uri);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'get', method: 'get',
params: { uri, timeout: 20 }, params: { uri, timeout: 30 },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'get' success");
// check to make sure the daemon didn't just time out // check to make sure the daemon didn't just time out
if (!data.result) {
reject(JSON.stringify(data));
}
if (data.result.error) { if (data.result.error) {
reject(data.result.error); reject(data.result.error);
} }
@ -54,61 +52,45 @@ module.exports = {
*/ */
// save a record of the file to the Files table // save a record of the file to the Files table
const result = data.result; const result = data.result;
db.File createFilesRecord(
.create({ result.name, result.claim_id, result.outpoint, result.file_name, result.download_path, result.mime_type, result.metadata.stream.metadata.nsfw);
name : result.name,
claim_id : result.claim_id,
outpoint : result.outpoint,
file_name: result.file_name,
file_path: result.download_path,
file_type: result.mime_type,
nsfw : result.metadata.stream.metadata.nsfw,
})
.catch(error => {
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result); resolve(result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'get' error");
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
getClaimsList (claimName) { getClaimsList (claimName) {
logger.debug(`Getting Claim List for "${claimName}"`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> getClaimList:', claimName);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'claim_list', method: 'claim_list',
params: { name: claimName }, params: { name: claimName },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'claim_list' success");
resolve(data.result); resolve(data.result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'claim_list' error");
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
resolveUri (uri) { resolveUri (uri) {
logger.debug(`Resolving URI for "${uri}"`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> resolveUri:', uri);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'resolve', method: 'resolve',
params: { uri }, params: { uri },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'resolve' success");
resolve(data.result); resolve(data.result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'resolve' error");
reject(error); reject(error);
}); });
}); });

View file

@ -0,0 +1,35 @@
const fs = require('fs');
module.exports = (winston, logLevel, logDir) => {
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
winston.configure({
transports: [
new (winston.transports.Console)({
level : logLevel,
timestamp : false,
colorize : true,
prettyPrint: true,
}),
new (winston.transports.File)({
filename : `${logDir}speechLogs.log`,
level : logLevel,
json : false,
timestamp : true,
colorize : true,
prettyPrint : true,
handleExceptions : true,
humanReadableUnhandledException: true,
}),
],
});
winston.error('Level 0');
winston.warn('Level 1');
winston.info('Level 2');
winston.verbose('Level 3');
winston.debug('Level 4');
winston.silly('Level 5');
};

View file

@ -6,7 +6,7 @@ module.exports = (sequelize, { STRING, BOOLEAN }) => {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
claim_id: { claimId: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
@ -14,15 +14,15 @@ module.exports = (sequelize, { STRING, BOOLEAN }) => {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_name: { fileName: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_path: { filePath: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_type: { fileType: {
type: STRING, type: STRING,
}, },
nsfw: { nsfw: {

View file

@ -4,17 +4,20 @@ const Sequelize = require('sequelize');
const basename = path.basename(module.filename); const basename = path.basename(module.filename);
const config = require('config'); const config = require('config');
const db = {}; const db = {};
const logger = require('winston');
const connectionUri = config.get('Database.MySqlConnectionUri'); const connectionUri = config.get('Database.MySqlConnectionUri');
const sequelize = new Sequelize(connectionUri); const sequelize = new Sequelize(connectionUri, {
logging: false,
});
sequelize sequelize
.authenticate() .authenticate()
.then(() => { .then(() => {
console.log('Connection has been established successfully.'); logger.info('Sequelize has has been established mysql connection successfully.');
}) })
.catch(err => { .catch(err => {
console.error('Unable to connect to the database:', err); logger.error('Sequelize was nable to connect to the database:', err);
}); });
fs.readdirSync(__dirname).filter(file => file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js').forEach(file => { fs.readdirSync(__dirname).filter(file => file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js').forEach(file => {

View file

@ -36,7 +36,8 @@
"sequelize": "^4.1.0", "sequelize": "^4.1.0",
"socket.io": "^2.0.1", "socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0", "socketio-file-upload": "^0.6.0",
"universal-analytics": "^0.4.13" "universal-analytics": "^0.4.13",
"winston": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "3.19.0", "eslint": "3.19.0",

View file

@ -1,26 +1,26 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const lbryApi = require('../helpers/libraries/lbryApi.js'); const lbryApi = require('../helpers/libraries/lbryApi.js');
const logger = require('winston');
module.exports = app => { module.exports = app => {
// route to run a claim_list request on the daemon // route to run a claim_list request on the daemon
app.get('/api/claim_list/:claim', ({ params }, res) => { app.get('/api/claim_list/:claim', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
lbryApi lbryApi
.getClaimsList(params.claim) .getClaimsList(params.claim)
.then(claimsList => { .then(claimsList => {
console.log('/api/claim_list/:claim success.');
res.status(200).json(claimsList); res.status(200).json(claimsList);
}) })
.catch(error => { .catch(error => {
console.log('/api/claim_list/:name error:', error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to run a resolve request on the daemon // route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ params }, res) => { app.get('/api/resolve/:uri', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
lbryApi lbryApi
.resolveUri(params.uri) .resolveUri(params.uri)
.then(resolvedUri => { .then(resolvedUri => {
console.log('/api/resolve/:claim success.');
res.status(200).json(resolvedUri); res.status(200).json(resolvedUri);
}) })
.catch(error => { .catch(error => {

View file

@ -1,3 +1,5 @@
const logger = require('winston');
module.exports = app => { module.exports = app => {
// route for the home page // route for the home page
app.get('/', (req, res) => { app.get('/', (req, res) => {
@ -5,6 +7,7 @@ module.exports = app => {
}); });
// a catch-all route if someone visits a page that does not exist // a catch-all route if someone visits a page that does not exist
app.use('*', (req, res) => { app.use('*', (req, res) => {
logger.error(`Get request on ${req.originalUrl} which was 404`);
res.status(404).render('fourOhFour'); res.status(404).render('fourOhFour');
}); });
}; };

View file

@ -1,17 +1,19 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const serveController = require('../controllers/serveController.js'); const serveController = require('../controllers/serveController.js');
const logger = require('winston');
function serveFile ({ file_type, file_path }, res) { function serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`);
// set default options // set default options
const options = { const options = {
headers: { headers: {
'X-Content-Type-Options': 'nosniff', 'X-Content-Type-Options': 'nosniff',
'Content-Type' : file_type, 'Content-Type' : fileType,
}, },
}; };
// adjust default options as needed // adjust default options as needed
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
switch (file_type) { switch (fileType) {
case 'image/jpeg': case 'image/jpeg':
break; break;
case 'image/gif': case 'image/gif':
@ -21,47 +23,43 @@ function serveFile ({ file_type, file_path }, res) {
case 'video/mp4': case 'video/mp4':
break; break;
default: default:
console.log('sending unknown file type as .jpeg'); logger.warn('sending unknown file type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg'; options['headers']['Content-Type'] = 'image/jpeg';
break; break;
} }
// send file // send file
res.status(200).sendFile(file_path, options); res.status(200).sendFile(filePath, options);
} }
module.exports = (app, ua, googleAnalyticsId) => { module.exports = (app, ua, googleAnalyticsId) => {
// route to fetch one free public claim // route to fetch one free public claim
app.get('/:name/:claim_id', ({ params }, res) => { app.get('/:name/:claim_id', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
const routeString = `${params.name}/${params.claim_id}`; const routeString = `${params.name}/${params.claim_id}`;
// google analytics // google analytics
ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name/claimId', routeString).send(); ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name/claimId', routeString).send();
// begin image-serve processes // begin image-serve processes
console.log(`>> GET request on /${routeString}`);
serveController serveController
.getClaimByClaimId(params.name, params.claim_id) .getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then(fileInfo => {
console.log('/:name/:claim_id success.', fileInfo.file_name);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
console.log('/:name/:claim_id error:', error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to fetch one free public claim // route to fetch one free public claim
app.get('/:name', ({ params }, res) => { app.get('/:name', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
// google analytics // google analytics
ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name', params.name).send(); ua(googleAnalyticsId, { https: true }).event('Serve Route', '/name', params.name).send();
// begin image-serve processes // begin image-serve processes
console.log(`>> GET request on /${params.name}`);
serveController serveController
.getClaimByName(params.name) .getClaimByName(params.name)
.then(fileInfo => { .then(fileInfo => {
console.log('/:name success.', fileInfo.file_name);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
console.log('/:name error:', error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });

View file

@ -1,21 +1,20 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const showController = require('../controllers/showController.js'); const showController = require('../controllers/showController.js');
const logger = require('winston');
module.exports = (app, ua, googleAnalyticsId) => { module.exports = (app, ua, googleAnalyticsId) => {
// route to fetch all free public claims // route to fetch all free public claims
app.get('/:name/all', ({ params }, res) => { app.get('/:name/all', ({ originalUrl, params }, res) => {
console.log(`>> GET request on /${params.name}/all`); logger.debug(`GET request on ${originalUrl}`);
// google analytics // google analytics
ua(googleAnalyticsId, { https: true }).event('Show Routes', '/name/all', `${params.name}/all`).send(); ua(googleAnalyticsId, { https: true }).event('Show Routes', '/name/all', `${params.name}/all`).send();
// fetch all free public claims // fetch all free public claims
showController showController
.getAllClaims(params.name) .getAllClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
console.log('/:name/all success.');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
}) })
.catch(error => { .catch(error => {
console.log('/:name/all error:', error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });

View file

@ -1,11 +1,12 @@
const publishController = require('../controllers/publishController.js'); const publishController = require('../controllers/publishController.js');
const logger = require('winston');
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => { module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
const http = require('http').Server(app); const http = require('http').Server(app);
const io = require('socket.io')(http); const io = require('socket.io')(http);
io.on('connection', socket => { io.on('connection', socket => {
console.log('a user connected via sockets'); logger.silly('a user connected via sockets');
// create visitor record // create visitor record
const visitor = ua(googleAnalyticsId, { https: true }); const visitor = ua(googleAnalyticsId, { https: true });
// attach upload listeners // attach upload listeners
@ -13,28 +14,30 @@ module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
uploader.dir = hostedContentPath; uploader.dir = hostedContentPath;
uploader.listen(socket); uploader.listen(socket);
uploader.on('start', ({ file }) => { uploader.on('start', ({ file }) => {
logger.info('client started an upload:', file.name);
// server side test to make sure file is not a bad file type // server side test to make sure file is not a bad file type
if (/\.exe$/.test(file.name)) { if (/\.exe$/.test(file.name)) {
uploader.abort(file.id, socket); uploader.abort(file.id, socket);
} }
}); });
uploader.on('error', ({ error }) => { uploader.on('error', ({ error }) => {
console.log('an error occured while uploading', error); logger.error('an error occured while uploading', error);
socket.emit('publish-status', error); socket.emit('publish-status', error);
}); });
uploader.on('saved', ({ file }) => { uploader.on('saved', ({ file }) => {
console.log('uploaded ', file.name);
if (file.success) { if (file.success) {
logger.debug(`Client successfully uploaded ${file.name}`);
socket.emit('publish-status', 'file upload successfully completed'); socket.emit('publish-status', 'file upload successfully completed');
publishController.publish(file.meta.name, file.name, file.pathName, file.meta.type, file.meta.license, file.meta.nsfw, socket, visitor); publishController.publish(file.meta.name, file.name, file.pathName, file.meta.type, file.meta.license, file.meta.nsfw, socket, visitor);
} else { } else {
logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'file uploaded, but with errors'); socket.emit('publish-failure', 'file uploaded, but with errors');
// to-do: remove the file // to-do: remove the file if not done automatically
} }
}); });
// handle disconnect // handle disconnect
socket.on('disconnect', () => { socket.on('disconnect', () => {
console.log('user disconnected'); logger.silly('a user disconnected via sockets');
}); });
}); });

View file

@ -6,10 +6,16 @@ const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars'); const Handlebars = require('handlebars');
const config = require('config'); const config = require('config');
const ua = require('universal-analytics'); const ua = require('universal-analytics');
const winston = require('winston');
const googleAnalyticsId = config.get('AnalyticsConfig.googleId'); const googleAnalyticsId = config.get('AnalyticsConfig.googleId');
const hostedContentPath = config.get('Database.PublishUploadPath'); const hostedContentPath = config.get('Database.PublishUploadPath');
// configure logging
const logLevel = config.get('Logging.LogLevel');
const logDir = config.get('Logging.LogDirectory');
require('./helpers/logging/loggerSetup.js')(winston, logLevel, logDir);
// set port // set port
const PORT = 3000; const PORT = 3000;
// initialize express app // initialize express app
@ -63,6 +69,6 @@ const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath
// start server // start server
db.sequelize.sync({}).then(() => { db.sequelize.sync({}).then(() => {
http.listen(PORT, () => { http.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`); winston.info(`Server is listening on PORT ${PORT}`);
}); });
}); });