removed configs and moved logging and slack configuration to server/utils/

This commit is contained in:
bill bittner 2018-06-05 18:55:44 -07:00
parent 7ee0b14a09
commit c1f5552e99
21 changed files with 109 additions and 178 deletions

View file

@ -1,8 +0,0 @@
const lbryConfig = {
api: {
apiHost: 'localhost',
apiPort: '5279',
},
};
module.exports = lbryConfig;

View file

@ -1,36 +0,0 @@
const logger = require('winston');
function LoggerConfig () {
this.logLevel = 'debug';
this.update = (config) => {
if (!config) {
return logger.warn('No logger config received.');
}
logger.info('configuring winston logger...');
// update values with local config params
const {logLevel} = config;
this.logLevel = logLevel;
// configure the winston logger
logger.configure({
transports: [
new (logger.transports.Console)({
level : this.logLevel,
timestamp : false,
colorize : true,
prettyPrint : true,
handleExceptions : true,
humanReadableUnhandledException: true,
}),
],
});
// test all the log levels
logger.info('testing winston log levels...');
logger.warn('Testing: Log Level 1');
logger.info('Testing: Log Level 2');
logger.verbose('Testing: Log Level 3');
logger.debug('Testing: Log Level 4');
logger.silly('Testing: Log Level 5');
};
};
module.exports = new LoggerConfig();

View file

@ -1,20 +0,0 @@
const logger = require('winston');
function MysqlConfig () {
this.database = 'default';
this.username = 'default';
this.password = 'default';
this.update = (config) => {
if (!config) {
return logger.warn('No MySQL config received.');
}
// configure credentials
logger.info('configuring mysql...');
const { database, username, password } = config;
this.database = database;
this.username = username;
this.password = password;
};
}
module.exports = new MysqlConfig();

View file

@ -1,45 +0,0 @@
const logger = require('winston');
function SiteConfig () {
this.analytics = {
googleId: 'default',
};
this.assetDefaults = {
description: 'An asset published on Spee.ch',
thumbnail : 'https://spee.ch/assets/img/video_thumb_default.png',
title : 'A Spee.ch Implementation',
};
this.auth = {
sessionKey: 'default',
};
this.details = {
description: 'Welcome to my decentralized image and video sharing site.',
host : 'http://localhost:3000',
port : 3000,
title : 'My Spee.ch Site',
twitter : '@exampleTwitterHandle',
};
this.publishing = {
additionalClaimAddresses: [],
disabled : false,
disabledMessage : 'Please check back soon.',
primaryClaimAddress : 'default',
thumbnailChannel : 'default',
thumbnailChannelId : 'default',
uploadDirectory : '/home/lbry/Uploads',
};
this.update = (config) => {
if (!config) {
return console.log('No site config received.');
}
const { analytics, assetDefaults, auth, details, publishing } = config;
logger.info('configuring site details...');
this.analytics = analytics;
this.assetDefaults = assetDefaults;
this.auth = auth;
this.details = details;
this.publishing = publishing;
};
}
module.exports = new SiteConfig();

View file

@ -1,51 +0,0 @@
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
const winston = require('winston');
function SlackConfig () {
this.slackWebHook = 'default';
this.slackErrorChannel = 'default';
this.slackInfoChannel = 'default';
this.update = (config) => {
if (!config) {
return winston.warn('No slack config received');
}
// update variables
winston.info('configuring slack logger...');
const {slackWebHook, slackErrorChannel, slackInfoChannel} = config;
this.slackWebHook = slackWebHook;
this.slackErrorChannel = slackErrorChannel;
this.slackInfoChannel = slackInfoChannel;
// update slack webhook settings
if (this.slackWebHook) {
// add a transport for errors to slack
if (this.slackErrorChannel) {
winston.add(winstonSlackWebHook, {
name : 'slack-errors-transport',
level : 'warn',
webhookUrl: this.slackWebHook,
channel : this.slackErrorChannel,
username : 'spee.ch',
iconEmoji : ':face_with_head_bandage:',
});
};
if (this.slackInfoChannel) {
winston.add(winstonSlackWebHook, {
name : 'slack-info-transport',
level : 'info',
webhookUrl: this.slackWebHook,
channel : this.slackInfoChannel,
username : 'spee.ch',
iconEmoji : ':nerd_face:',
});
};
// send test messages
winston.info('testing slack logger...');
winston.error('Slack "error" logging is online.');
winston.info('Slack "info" logging is online.');
} else {
winston.warn('Slack logging is not enabled because no slackWebHook config var provided.');
}
};
};
module.exports = new SlackConfig();

View file

@ -12,30 +12,45 @@ const Path = require('path');
// load local modules
const requestLogger = require('./server/middleware/requestLogger.js');
const siteConfig = require('@config/siteConfig.js');
const PORT = siteConfig.details.port;
const createDatabaseIfNotExists = require('./server/models/utils/createDatabaseIfNotExists.js');
const { getWalletBalance } = require('./server/lbrynet/index');
const { getWalletBalance } = require('./server/lbrynet');
const db = require('./server/models');
// configure logging
const configureLogging = require('./server/utils/configureLogging.js');
configureLogging();
// configure slack logging
const configureSlack = require('./server/utils/configureSlack.js');
configureSlack();
/* create app */
// create an Express application
const app = express();
// trust the proxy to get ip address for us
app.enable('trust proxy');
/* add middleware */
// set HTTP headers to protect against well-known web vulnerabilties
app.use(helmet());
// 'express.static' to serve static files from public directory
const publicPath = Path.resolve(process.cwd(), 'public');
app.use(express.static(publicPath));
logger.info(`serving static files from default static path at ${publicPath}.`);
// 'body parser' for parsing application/json
app.use(bodyParser.json());
// 'body parser' for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// add custom middleware (note: build out to accept dynamically use what is in server/middleware/
app.use(requestLogger);
// configure passport
const speechPassport = require('./server/speechPassport/index');
// initialize passport
const sessionKey = siteConfig.auth.sessionKey;
app.use(cookieSession({
@ -44,6 +59,7 @@ app.use(cookieSession({
}));
app.use(speechPassport.initialize());
app.use(speechPassport.session());
// configure handlebars & register it with express app
const hbs = expressHandlebars.create({
defaultLayout: 'embed',
@ -51,6 +67,7 @@ const hbs = expressHandlebars.create({
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// set the routes on the app
require('./server/routes/auth/index')(app);
require('./server/routes/api/index')(app);
@ -62,8 +79,6 @@ require('./server/routes/fallback/index')(app);
const server = http.Server(app);
/* start the server */
const db = require('./server/models/index');
const PORT = siteConfig.details.port;
const startServer = () => {
return createDatabaseIfNotExists()

View file

@ -1,5 +1,5 @@
const db = require('../../../../models');
const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../../../config/siteConfig.js');
const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('@config/siteConfig.js');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

View file

@ -1,5 +1,5 @@
const logger = require('winston');
const { details, publishing } = require('../../../../../config/siteConfig.js');
const { details, publishing } = require('@config/siteConfig.js');
const createBasicPublishParams = (filePath, name, title, description, license, nsfw, thumbnail) => {
logger.debug(`Creating Publish Parameters`);

View file

@ -1,5 +1,5 @@
const logger = require('winston');
const { details, publishing } = require('../../../../../config/siteConfig.js');
const { details, publishing } = require('@config/siteConfig.js');
const createThumbnailPublishParams = (thumbnailFilePath, claimName, license, nsfw) => {
if (!thumbnailFilePath) {

View file

@ -1,4 +1,4 @@
const { details: { host } } = require('../../../../../config/siteConfig.js');
const { details: { host } } = require('@config/siteConfig.js');
const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js');

View file

@ -1,4 +1,4 @@
const { details: { host } } = require('../../../config/siteConfig.js');
const { details: { host } } = require('@config/siteConfig.js');
const sendEmbedPage = ({ params }, res) => {
const claimId = params.claimId;

View file

@ -1,6 +1,6 @@
const axios = require('axios');
const logger = require('winston');
const { api: { apiHost, apiPort } } = require('../../config/lbryConfig.js');
const { api: { apiHost, apiPort } } = require('@config/lbryConfig.js');
const lbrynetUri = 'http://' + apiHost + ':' + apiPort;
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('../utils/googleAnalytics.js');
const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js');

View file

@ -1,6 +1,6 @@
const logger = require('winston');
const returnShortId = require('./utils/returnShortId.js');
const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('../../config/siteConfig.js');
const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('@config/siteConfig.js');
const NO_CLAIM = 'NO_CLAIM';

View file

@ -9,7 +9,7 @@ const Blocked = require('./blocked.js');
const Sequelize = require('sequelize');
const logger = require('winston');
const {database, username, password} = require('../../config/mysqlConfig.js');
const {database, username, password} = require('@config/mysqlConfig.js');
// set sequelize options
const sequelize = new Sequelize(database, username, password, {

View file

@ -1,7 +1,7 @@
const Sequelize = require('sequelize');
const createDatabaseIfNotExists = () => {
const {database, username, password} = require('../../../config/mysqlConfig.js');
const {database, username, password} = require('@config/mysqlConfig.js');
const sequelize = new Sequelize('', username, password, {
dialect : 'mysql',
logging : false,

View file

@ -9,7 +9,7 @@ import App from '@app';
import renderFullPage from '../renderFullPage.js';
import Helmet from 'react-helmet';
const siteConfig = require('../../../config/siteConfig.js');
const siteConfig = require('@config/siteConfig.js');
module.exports = (req, res) => {
let context = {};

View file

@ -13,7 +13,7 @@ import Sagas from '@sagas';
import Actions from '@actions';
import Helmet from 'react-helmet';
const siteConfig = require('../../../config/siteConfig.js');
const siteConfig = require('@config/siteConfig.js');
const returnSagaWithParams = (saga, params) => {
return function * () {

View file

@ -1,5 +1,5 @@
const multipart = require('connect-multiparty');
const { publishing: { uploadDirectory } } = require('../../../config/siteConfig.js');
const { publishing: { uploadDirectory } } = require('@config/siteConfig.js');
const multipartMiddleware = multipart({uploadDir: uploadDirectory});
module.exports = multipartMiddleware;

View file

@ -0,0 +1,32 @@
const logger = require('winston');
const { logLevel } = require('@config/loggerConfig');
function configureLogging () {
if (!logLevel) {
return logger.warn('No logLevel config received.');
}
logger.info('configuring winston logger...');
// configure the winston logger
logger.configure({
transports: [
new (logger.transports.Console)({
level : this.logLevel,
timestamp : false,
colorize : true,
prettyPrint : true,
handleExceptions : true,
humanReadableUnhandledException: true,
}),
],
});
// test all the log levels
logger.info('testing winston log levels...');
logger.warn('Testing: Log Level 1');
logger.info('Testing: Log Level 2');
logger.verbose('Testing: Log Level 3');
logger.debug('Testing: Log Level 4');
logger.silly('Testing: Log Level 5');
}
module.exports = configureLogging;

View file

@ -0,0 +1,44 @@
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
const logger = require('winston');
const config = require('@config/loggerConfig');
function configureSlack () {
if (!config) {
return logger.warn('No slack config found');
}
const {slackWebHook, slackErrorChannel, slackInfoChannel} = config;
// update variables
logger.info('configuring slack logger...');
// update slack webhook settings
if (!slackWebHook) {
return logger.warn('Slack logging is not enabled because no slackWebHook config var provided.');
}
// add a transport for errors to slack
if (slackErrorChannel) {
logger.add(winstonSlackWebHook, {
name : 'slack-errors-transport',
level : 'warn',
webhookUrl: slackWebHook,
channel : slackErrorChannel,
username : 'spee.ch',
iconEmoji : ':face_with_head_bandage:',
});
}
// add a transport for info in slack
if (slackInfoChannel) {
logger.add(winstonSlackWebHook, {
name : 'slack-info-transport',
level : 'info',
webhookUrl: slackWebHook,
channel : slackInfoChannel,
username : 'spee.ch',
iconEmoji : ':nerd_face:',
});
}
// send test messages
logger.info('Slack logging is online.');
}
module.exports = configureSlack;

View file

@ -1,6 +1,6 @@
const logger = require('winston');
const ua = require('universal-analytics');
const { analytics : { googleId }, details: { title } } = require('../../config/siteConfig.js');
const { analytics : { googleId }, details: { title } } = require('@config/siteConfig.js');
const createServeEventParams = (headers, ip, originalUrl) => {
return {