moved passport info into package

This commit is contained in:
bill bittner 2018-03-27 16:08:01 -07:00
parent 955a2f5e14
commit 195f193259
17 changed files with 376 additions and 311 deletions

View file

@ -1,16 +1,95 @@
const Sequelize = require('sequelize');
const logger = require('winston');
function mysql () { function mysql () {
this.database = 'default'; this.database = 'default';
this.username = 'default'; this.username = 'default';
this.password = 'default'; this.password = 'default';
this.db = {};
this.configure = (config) => { this.configure = (config) => {
if (!config) { if (!config) {
return console.log('No MySQL config received.'); return console.log('No MySQL config received.');
} }
// configure credentials
console.log('configuring mysql credentials...'); console.log('configuring mysql credentials...');
const {database, username, password} = config; const {database, username, password} = config;
this.database = database; this.database = database;
this.username = username; this.username = username;
this.password = password; this.password = password;
// configure db
// set sequelize options
const sequelize = new Sequelize(database, username, password, {
host : 'localhost',
dialect : 'mysql',
dialectOptions: {decimalNumbers: true},
logging : false,
pool : {
max : 5,
min : 0,
idle : 10000,
acquire: 10000,
},
});
// establish mysql connection
sequelize
.authenticate()
.then(() => {
logger.info('Sequelize has established mysql connection successfully.');
})
.catch(err => {
logger.debug('mysqlconfig', { database, username, password });
logger.error('Sequelize was unable to connect to the database:', err);
});
// manually add each model to the db object (note: make this dynamic)
const db = {};
const Certificate = require('models/certificate.js');
const Channel = require('models/channel.js');
const Claim = require('models/claim.js');
const File = require('models/file.js');
const Request = require('models/request.js');
const User = require('models/user.js');
db['Certificate'] = sequelize.import('Certificate', Certificate);
db['Channel'] = sequelize.import('Channel', Channel);
db['Claim'] = sequelize.import('Claim', Claim);
db['File'] = sequelize.import('File', File);
db['Request'] = sequelize.import('Request', Request);
db['User'] = sequelize.import('User', User);
// run model.association for each model in the db object that has an association
logger.info('associating db models...');
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
logger.info('Associating model:', modelName);
db[modelName].associate(db);
}
});
// add sequelize/Sequelize to db
db.sequelize = sequelize;
db.Sequelize = Sequelize;
// add an 'upsert' method to the db object
db.upsert = (Model, values, condition, tableName) => {
return Model
.findOne({
where: condition,
})
.then(obj => {
if (obj) { // update
logger.debug(`updating record in db.${tableName}`);
return obj.update(values);
} else { // insert
logger.debug(`creating record in db.${tableName}`);
return Model.create(values);
}
})
.catch(function (error) {
logger.error(`${tableName}.upsert error`, error);
throw error;
});
};
this.db = db;
}; };
}; };

340
index.js

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
const db = require('../models'); const { db } = require('mysqlConfig');
const logger = require('winston'); const logger = require('winston');
module.exports = { module.exports = {

View file

@ -1,5 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); const { db } = require('mysqlConfig');
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const publishHelpers = require('../helpers/publishHelpers.js'); const publishHelpers = require('../helpers/publishHelpers.js');
const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../config/siteConfig.js'); const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../config/siteConfig.js');

View file

@ -1,4 +1,4 @@
const db = require('../models/index'); const { db } = require('mysqlConfig');
const logger = require('winston'); const logger = require('winston');
const { returnPaginatedChannelClaims } = require('../helpers/channelPagination.js'); const { returnPaginatedChannelClaims } = require('../helpers/channelPagination.js');

View file

@ -1,5 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../models/index'); const { db } = require('mysqlConfig');
module.exports = { module.exports = {
getRecentClaims () { getRecentClaims () {

View file

@ -0,0 +1,10 @@
module.exports = {
serializeSpeechUser (user, done) { // returns user data to be serialized into session
console.log('serializing user');
done(null, user);
},
deserializeSpeechUser (user, done) { // deserializes session and populates additional info to req.user
console.log('deserializing user');
done(null, user);
},
};

View file

@ -1,6 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../models/index'); const { db } = require('mysqlConfig');
module.exports = { module.exports = {
postToStats (action, url, ipAddress, name, claimId, result) { postToStats (action, url, ipAddress, name, claimId, result) {
logger.debug('action:', action); logger.debug('action:', action);

View file

@ -1,79 +0,0 @@
const Sequelize = require('sequelize');
const logger = require('winston');
logger.info('exporting sequelize models');
const mysqlConfig = require('../../config/mysqlConfig');
const { database, username, password } = mysqlConfig;
const db = {};
// set sequelize options
const sequelize = new Sequelize(database, username, password, {
host : 'localhost',
dialect : 'mysql',
dialectOptions: {decimalNumbers: true}, // fix to ensure DECIMAL will not be stored as a string
logging : false,
pool : {
max : 5,
min : 0,
idle : 10000,
acquire: 10000,
},
});
// establish mysql connection
sequelize
.authenticate()
.then(() => {
logger.info('Sequelize has established mysql connection successfully.');
})
.catch(err => {
logger.error('Sequelize was unable to connect to the database:', err);
});
// manually add each model to the db object
const Certificate = require('./certificate.js');
const Channel = require('./channel.js');
const Claim = require('./claim.js');
const File = require('./file.js');
const Request = require('./request.js');
const User = require('./user.js');
db['Certificate'] = sequelize.import('Certificate', Certificate);
db['Channel'] = sequelize.import('Channel', Channel);
db['Claim'] = sequelize.import('Claim', Claim);
db['File'] = sequelize.import('File', File);
db['Request'] = sequelize.import('Request', Request);
db['User'] = sequelize.import('User', User);
// run model.association for each model in the db object that has an association
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
logger.info('Associating model:', modelName);
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
// add an 'upsert' method to the db object
db.upsert = (Model, values, condition, tableName) => {
return Model
.findOne({
where: condition,
})
.then(obj => {
if (obj) { // update
logger.debug(`updating record in db.${tableName}`);
return obj.update(values);
} else { // insert
logger.debug(`creating record in db.${tableName}`);
return Model.create(values);
}
})
.catch(function (error) {
logger.error(`${tableName}.upsert error`, error);
throw error;
});
};
module.exports = db;

11
server/passport/index.js Normal file
View file

@ -0,0 +1,11 @@
const passport = require('passport');
const localLoginStrategy = require('./local-login.js');
const localSignupStrategy = require('./local-signup.js');
const { serializeSpeechUser, deserializeSpeechUser } = require('../helpers/authHelpers.js');
passport.deserializeUser(deserializeSpeechUser);
passport.serializeUser(serializeSpeechUser);
passport.use('local-login', localLoginStrategy);
passport.use('local-signup', localSignupStrategy);
module.exports = passport;

View file

@ -1,6 +1,6 @@
const PassportLocalStrategy = require('passport-local').Strategy; const PassportLocalStrategy = require('passport-local').Strategy;
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); const { db } = require('mysqlConfig');
const returnUserAndChannelInfo = (userInstance) => { const returnUserAndChannelInfo = (userInstance) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View file

@ -1,7 +1,7 @@
const PassportLocalStrategy = require('passport-local').Strategy; const PassportLocalStrategy = require('passport-local').Strategy;
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); const { db } = require('mysqlConfig');
module.exports = new PassportLocalStrategy( module.exports = new PassportLocalStrategy(
{ {

View file

@ -1,6 +1,6 @@
const logger = require('winston'); const logger = require('winston');
const { details: { host } } = require('../../config/siteConfig.js'); const { details: { host } } = require('../../config/siteConfig.js');
const db = require('../models'); const { db } = require('../../config/mysqlConfig.js');
const { claimNameIsAvailable, checkChannelAvailability, publish } = require('../controllers/publishController.js'); const { claimNameIsAvailable, checkChannelAvailability, publish } = require('../controllers/publishController.js');
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js'); const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../helpers/publishHelpers.js'); const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../helpers/publishHelpers.js');
@ -12,7 +12,7 @@ const { getChannelData, getChannelClaims, getClaimId } = require('../controllers
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
module.exports = { const apiRoutes = {
// route to check whether site has published to a channel // route to check whether site has published to a channel
channelAvailabilityRoute ({ ip, originalUrl, params: { name } }, res) { channelAvailabilityRoute ({ ip, originalUrl, params: { name } }, res) {
const gaStartTime = Date.now(); const gaStartTime = Date.now();
@ -27,6 +27,9 @@ module.exports = {
}, },
// route to get a short channel id from long channel Id // route to get a short channel id from long channel Id
channelShortIdRoute ({ ip, originalUrl, params }, res) { channelShortIdRoute ({ ip, originalUrl, params }, res) {
console.log('hello');
logger.debug('host:', host);
logger.debug('db:', db);
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name) db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => { .then(shortId => {
res.status(200).json(shortId); res.status(200).json(shortId);
@ -238,3 +241,5 @@ module.exports = {
}); });
}, },
}; };
module.exports = apiRoutes;

View file

@ -1,6 +1,6 @@
// load dependencies // load dependencies
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); // require our models for syncing const { db } = require('mysqlConfig');
// configure logging // configure logging
require('../helpers/configureLogger.js')(logger); require('../helpers/configureLogger.js')(logger);

View file

@ -1,6 +1,6 @@
// load dependencies // load dependencies
const logger = require('winston'); const logger = require('winston');
const db = require('../models/index'); const { db } = require('mysqlConfig');
// configure logging // configure logging
require('../helpers/configureLogger.js')(logger); require('../helpers/configureLogger.js')(logger);

View file

@ -1,31 +1,23 @@
// const Server = require('./server/server.js');
// const Components = require('./client/components'); // const Components = require('./client/components');
// const Containers = require('./client/containers'); // const Containers = require('./client/containers');
// const Pages = require('./client/pages'); // const Pages = require('./client/pages');
const apiRoutes = require('./server/routes/apiRoutes.js'); const apiRoutes = require('./server/routes/apiRoutes.js');
const logger = require('./config/loggerConfig.js'); const logger = require('./config/loggerConfig.js');
const mysql = require('./config/mysqlConfig.js'); const mysql = require('./config/mysqlConfig');
const site = require('./config/siteConfig');
const slack = require('./config/slackConfig.js'); const slack = require('./config/slackConfig.js');
const database = require('./server/models'); const passport = require('./server/passport/');
const localLoginStrategy = require('./server/passport/local-login.js');
const localSignupStrategy = require('./server/passport/local-signup.js');
const exports = { const exports = {
// Server,
// Components, // Components,
// Containers, // Containers,
// Pages, // Pages,
apiRoutes, apiRoutes,
config: {
logger, logger,
mysql, mysql,
site,
slack, slack,
}, passport,
database,
passport: {
localLoginStrategy,
localSignupStrategy,
},
}; };
module.exports = exports; module.exports = exports;

View file

@ -1,6 +1,7 @@
const Path = require('path'); const Path = require('path');
const nodeExternals = require('webpack-node-externals'); const nodeExternals = require('webpack-node-externals');
const CLIENT_ROOT = Path.resolve(__dirname, 'client/'); const CLIENT_ROOT = Path.resolve(__dirname, 'client/');
const CONFIG_ROOT = Path.resolve(__dirname, 'config/');
const SERVER_ROOT = Path.resolve(__dirname, 'server/'); const SERVER_ROOT = Path.resolve(__dirname, 'server/');
module.exports = { module.exports = {
@ -36,6 +37,7 @@ module.exports = {
resolve: { resolve: {
modules: [ modules: [
CLIENT_ROOT, CLIENT_ROOT,
CONFIG_ROOT,
SERVER_ROOT, SERVER_ROOT,
'node_modules', 'node_modules',
__dirname, __dirname,