diff --git a/config/lbryConfig.js b/config/lbryConfig.js deleted file mode 100644 index 0e3a5c82..00000000 --- a/config/lbryConfig.js +++ /dev/null @@ -1,8 +0,0 @@ -const lbryConfig = { - api: { - apiHost: 'localhost', - apiPort: '5279', - }, -}; - -module.exports = lbryConfig; diff --git a/config/loggerConfig.js b/config/loggerConfig.js deleted file mode 100644 index df81d7e5..00000000 --- a/config/loggerConfig.js +++ /dev/null @@ -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(); diff --git a/config/mysqlConfig.js b/config/mysqlConfig.js deleted file mode 100644 index 07301eb8..00000000 --- a/config/mysqlConfig.js +++ /dev/null @@ -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(); diff --git a/config/siteConfig.js b/config/siteConfig.js deleted file mode 100644 index 0f92cbbe..00000000 --- a/config/siteConfig.js +++ /dev/null @@ -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(); diff --git a/config/slackConfig.js b/config/slackConfig.js deleted file mode 100644 index 56008a27..00000000 --- a/config/slackConfig.js +++ /dev/null @@ -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(); diff --git a/index.js b/index.js index a5cac8e2..efb9f616 100644 --- a/index.js +++ b/index.js @@ -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() diff --git a/server/controllers/api/claim/availability/checkClaimAvailability.js b/server/controllers/api/claim/availability/checkClaimAvailability.js index 68c31056..2a969551 100644 --- a/server/controllers/api/claim/availability/checkClaimAvailability.js +++ b/server/controllers/api/claim/availability/checkClaimAvailability.js @@ -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; diff --git a/server/controllers/api/claim/publish/createBasicPublishParams.js b/server/controllers/api/claim/publish/createBasicPublishParams.js index 7ede4ad7..3fac2c8f 100644 --- a/server/controllers/api/claim/publish/createBasicPublishParams.js +++ b/server/controllers/api/claim/publish/createBasicPublishParams.js @@ -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`); diff --git a/server/controllers/api/claim/publish/createThumbnailPublishParams.js b/server/controllers/api/claim/publish/createThumbnailPublishParams.js index c10d9cd9..1d89dc99 100644 --- a/server/controllers/api/claim/publish/createThumbnailPublishParams.js +++ b/server/controllers/api/claim/publish/createThumbnailPublishParams.js @@ -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) { diff --git a/server/controllers/api/claim/publish/index.js b/server/controllers/api/claim/publish/index.js index 0a748753..8e62b402 100644 --- a/server/controllers/api/claim/publish/index.js +++ b/server/controllers/api/claim/publish/index.js @@ -1,4 +1,4 @@ -const { details: { host } } = require('../../../../../config/siteConfig.js'); +const { details: { host } } = require('@config/siteConfig.js'); const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js'); diff --git a/server/controllers/pages/sendEmbedPage.js b/server/controllers/pages/sendEmbedPage.js index ae3979b1..99eb89df 100644 --- a/server/controllers/pages/sendEmbedPage.js +++ b/server/controllers/pages/sendEmbedPage.js @@ -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; diff --git a/server/lbrynet/index.js b/server/lbrynet/index.js index 90a45875..7b27e423 100644 --- a/server/lbrynet/index.js +++ b/server/lbrynet/index.js @@ -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'); diff --git a/server/models/claim.js b/server/models/claim.js index 75a221f5..cf41e273 100644 --- a/server/models/claim.js +++ b/server/models/claim.js @@ -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'; diff --git a/server/models/index.js b/server/models/index.js index 748db1b8..d6ba3b6a 100644 --- a/server/models/index.js +++ b/server/models/index.js @@ -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, { diff --git a/server/models/utils/createDatabaseIfNotExists.js b/server/models/utils/createDatabaseIfNotExists.js index 4a717067..21c968a3 100644 --- a/server/models/utils/createDatabaseIfNotExists.js +++ b/server/models/utils/createDatabaseIfNotExists.js @@ -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, diff --git a/server/render/src/handlePageRender.jsx b/server/render/src/handlePageRender.jsx index ae17bae0..e4179674 100644 --- a/server/render/src/handlePageRender.jsx +++ b/server/render/src/handlePageRender.jsx @@ -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 = {}; diff --git a/server/render/src/handleShowRender.jsx b/server/render/src/handleShowRender.jsx index 28cd2bb0..6ffdd5a6 100644 --- a/server/render/src/handleShowRender.jsx +++ b/server/render/src/handleShowRender.jsx @@ -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 * () { diff --git a/server/routes/utils/multipartMiddleware.js b/server/routes/utils/multipartMiddleware.js index bd2e98f6..9304fc18 100644 --- a/server/routes/utils/multipartMiddleware.js +++ b/server/routes/utils/multipartMiddleware.js @@ -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; diff --git a/server/utils/configureLogging.js b/server/utils/configureLogging.js new file mode 100644 index 00000000..e877d89f --- /dev/null +++ b/server/utils/configureLogging.js @@ -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; diff --git a/server/utils/configureSlack.js b/server/utils/configureSlack.js new file mode 100644 index 00000000..88806acc --- /dev/null +++ b/server/utils/configureSlack.js @@ -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; diff --git a/server/utils/googleAnalytics.js b/server/utils/googleAnalytics.js index 67abb91e..48f5f9b1 100644 --- a/server/utils/googleAnalytics.js +++ b/server/utils/googleAnalytics.js @@ -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 {