updated how configs pass
This commit is contained in:
parent
22255ab713
commit
49cf44bc12
7 changed files with 204 additions and 202 deletions
|
@ -1,4 +1,3 @@
|
|||
// const db = require('../models'); // require our models for syncing
|
||||
const logger = require('winston');
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
const config = require('../config/speechConfig.js');
|
||||
const logger = require('winston');
|
||||
|
||||
module.exports = function () {
|
||||
module.exports = (config) => {
|
||||
// get the config file
|
||||
|
||||
for (let configCategoryKey in config) {
|
||||
if (config.hasOwnProperty(configCategoryKey)) {
|
||||
// get the final variables for each config category
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
const axios = require('axios');
|
||||
const logger = require('winston');
|
||||
const config = require('../config/speechConfig.js');
|
||||
const { apiHost, apiPort } = config.api;
|
||||
const { api: { apiHost, apiPort } } = require('../config/speechConfig.js');
|
||||
const lbryApiUri = 'http://' + apiHost + ':' + apiPort;
|
||||
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('./googleAnalytics.js');
|
||||
|
||||
|
|
|
@ -1,65 +1,66 @@
|
|||
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||
const db = require('../models');
|
||||
const logger = require('winston');
|
||||
|
||||
function returnUserAndChannelInfo (userInstance) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let userInfo = {};
|
||||
userInfo['id'] = userInstance.id;
|
||||
userInfo['userName'] = userInstance.userName;
|
||||
userInstance
|
||||
.getChannel()
|
||||
.then(({channelName, channelClaimId}) => {
|
||||
userInfo['channelName'] = channelName;
|
||||
userInfo['channelClaimId'] = channelClaimId;
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
resolve(userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = new PassportLocalStrategy(
|
||||
{
|
||||
usernameField: 'username',
|
||||
passwordField: 'password',
|
||||
},
|
||||
(username, password, done) => {
|
||||
return db.User
|
||||
.findOne({
|
||||
where: {userName: username},
|
||||
})
|
||||
.then(user => {
|
||||
if (!user) {
|
||||
logger.debug('no user found');
|
||||
return done(null, false, {message: 'Incorrect username or password'});
|
||||
}
|
||||
return user.comparePassword(password)
|
||||
.then(isMatch => {
|
||||
if (!isMatch) {
|
||||
logger.debug('incorrect password');
|
||||
return done(null, false, {message: 'Incorrect username or password'});
|
||||
}
|
||||
logger.debug('Password was a match, returning User');
|
||||
return returnUserAndChannelInfo(user)
|
||||
.then(userInfo => {
|
||||
return done(null, userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
return error;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
return error;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
return done(error);
|
||||
});
|
||||
module.exports = (db) => {
|
||||
const returnUserAndChannelInfo = (userInstance) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let userInfo = {};
|
||||
userInfo['id'] = userInstance.id;
|
||||
userInfo['userName'] = userInstance.userName;
|
||||
userInstance
|
||||
.getChannel()
|
||||
.then(({channelName, channelClaimId}) => {
|
||||
userInfo['channelName'] = channelName;
|
||||
userInfo['channelClaimId'] = channelClaimId;
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
resolve(userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
return new PassportLocalStrategy(
|
||||
{
|
||||
usernameField: 'username',
|
||||
passwordField: 'password',
|
||||
},
|
||||
(username, password, done) => {
|
||||
return db.User
|
||||
.findOne({
|
||||
where: {userName: username},
|
||||
})
|
||||
.then(user => {
|
||||
if (!user) {
|
||||
logger.debug('no user found');
|
||||
return done(null, false, {message: 'Incorrect username or password'});
|
||||
}
|
||||
return user.comparePassword(password)
|
||||
.then(isMatch => {
|
||||
if (!isMatch) {
|
||||
logger.debug('incorrect password');
|
||||
return done(null, false, {message: 'Incorrect username or password'});
|
||||
}
|
||||
logger.debug('Password was a match, returning User');
|
||||
return returnUserAndChannelInfo(user)
|
||||
.then(userInfo => {
|
||||
return done(null, userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
return error;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
return error;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
return done(error);
|
||||
});
|
||||
},
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,64 +1,65 @@
|
|||
const db = require('../models');
|
||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||
const lbryApi = require('../helpers/lbryApi.js');
|
||||
const logger = require('winston');
|
||||
|
||||
module.exports = new PassportLocalStrategy(
|
||||
{
|
||||
usernameField: 'username',
|
||||
passwordField: 'password',
|
||||
},
|
||||
(username, password, done) => {
|
||||
logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);
|
||||
let userInfo = {};
|
||||
// server-side validaton of inputs (username, password)
|
||||
module.exports = (db) => {
|
||||
return new PassportLocalStrategy(
|
||||
{
|
||||
usernameField: 'username',
|
||||
passwordField: 'password',
|
||||
},
|
||||
(username, password, done) => {
|
||||
logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);
|
||||
let userInfo = {};
|
||||
// server-side validaton of inputs (username, password)
|
||||
|
||||
// create the channel and retrieve the metadata
|
||||
return lbryApi.createChannel(`@${username}`)
|
||||
.then(tx => {
|
||||
// create user record
|
||||
const userData = {
|
||||
userName: username,
|
||||
password: password,
|
||||
};
|
||||
logger.verbose('userData >', userData);
|
||||
// create user record
|
||||
const channelData = {
|
||||
channelName : `@${username}`,
|
||||
channelClaimId: tx.claim_id,
|
||||
};
|
||||
logger.verbose('channelData >', channelData);
|
||||
// create certificate record
|
||||
const certificateData = {
|
||||
claimId: tx.claim_id,
|
||||
name : `@${username}`,
|
||||
// address,
|
||||
};
|
||||
logger.verbose('certificateData >', certificateData);
|
||||
// save user and certificate to db
|
||||
return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
|
||||
})
|
||||
.then(([newUser, newChannel, newCertificate]) => {
|
||||
logger.verbose('user and certificate successfully created');
|
||||
// store the relevant newUser info to be passed back for req.User
|
||||
userInfo['id'] = newUser.id;
|
||||
userInfo['userName'] = newUser.userName;
|
||||
userInfo['channelName'] = newChannel.channelName;
|
||||
userInfo['channelClaimId'] = newChannel.channelClaimId;
|
||||
// associate the instances
|
||||
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
|
||||
})
|
||||
.then(() => {
|
||||
logger.verbose('user and certificate successfully associated');
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
return done(null, userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('signup error', error);
|
||||
return done(error);
|
||||
});
|
||||
}
|
||||
);
|
||||
// create the channel and retrieve the metadata
|
||||
return lbryApi.createChannel(`@${username}`)
|
||||
.then(tx => {
|
||||
// create user record
|
||||
const userData = {
|
||||
userName: username,
|
||||
password: password,
|
||||
};
|
||||
logger.verbose('userData >', userData);
|
||||
// create user record
|
||||
const channelData = {
|
||||
channelName : `@${username}`,
|
||||
channelClaimId: tx.claim_id,
|
||||
};
|
||||
logger.verbose('channelData >', channelData);
|
||||
// create certificate record
|
||||
const certificateData = {
|
||||
claimId: tx.claim_id,
|
||||
name : `@${username}`,
|
||||
// address,
|
||||
};
|
||||
logger.verbose('certificateData >', certificateData);
|
||||
// save user and certificate to db
|
||||
return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
|
||||
})
|
||||
.then(([newUser, newChannel, newCertificate]) => {
|
||||
logger.verbose('user and certificate successfully created');
|
||||
// store the relevant newUser info to be passed back for req.User
|
||||
userInfo['id'] = newUser.id;
|
||||
userInfo['userName'] = newUser.userName;
|
||||
userInfo['channelName'] = newChannel.channelName;
|
||||
userInfo['channelClaimId'] = newChannel.channelClaimId;
|
||||
// associate the instances
|
||||
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
|
||||
})
|
||||
.then(() => {
|
||||
logger.verbose('user and certificate successfully associated');
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
return done(null, userInfo);
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('signup error', error);
|
||||
return done(error);
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
@ -14,7 +14,7 @@ const { getChannelData, getChannelClaims, getClaimId } = require('../controllers
|
|||
const NO_CHANNEL = 'NO_CHANNEL';
|
||||
const NO_CLAIM = 'NO_CLAIM';
|
||||
|
||||
module.exports = (app) => {
|
||||
module.exports = (app, siteConfig) => {
|
||||
// route to check whether site has published to a channel
|
||||
app.get('/api/channel/availability/:name', ({ ip, originalUrl, params }, res) => {
|
||||
checkChannelAvailability(params.name)
|
||||
|
|
154
server.js
154
server.js
|
@ -4,7 +4,7 @@ const bodyParser = require('body-parser');
|
|||
const expressHandlebars = require('express-handlebars');
|
||||
const Handlebars = require('handlebars');
|
||||
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
|
||||
const config = require('./config/speechConfig.js');
|
||||
const { logging: { logLevel } } = require('./config/speechConfig.js');
|
||||
const logger = require('winston');
|
||||
const helmet = require('helmet');
|
||||
const PORT = 3000; // set port
|
||||
|
@ -13,85 +13,89 @@ const passport = require('passport');
|
|||
const cookieSession = require('cookie-session');
|
||||
|
||||
// configure logging
|
||||
const logLevel = config.logging.logLevel;
|
||||
require('./config/loggerConfig.js')(logger, logLevel);
|
||||
require('./config/slackConfig.js')(logger);
|
||||
|
||||
// check for global config variables
|
||||
require('./helpers/configVarCheck.js')();
|
||||
|
||||
// trust the proxy to get ip address for us
|
||||
app.enable('trust proxy');
|
||||
|
||||
// add middleware
|
||||
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
|
||||
app.use(express.static(`${__dirname}/public`)); // 'express.static' to serve static files from public directory
|
||||
app.use(bodyParser.json()); // 'body parser' for parsing application/json
|
||||
app.use(bodyParser.urlencoded({ extended: true })); // 'body parser' for parsing application/x-www-form-urlencoded
|
||||
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
|
||||
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
||||
next();
|
||||
});
|
||||
|
||||
// configure passport
|
||||
passport.serializeUser(serializeSpeechUser);
|
||||
passport.deserializeUser(deserializeSpeechUser);
|
||||
const localSignupStrategy = require('./passport/local-signup.js');
|
||||
const localLoginStrategy = require('./passport/local-login.js');
|
||||
passport.use('local-signup', localSignupStrategy);
|
||||
passport.use('local-login', localLoginStrategy);
|
||||
// initialize passport
|
||||
app.use(cookieSession({
|
||||
name : 'session',
|
||||
keys : [config.session.sessionKey],
|
||||
maxAge: 24 * 60 * 60 * 1000, // 24 hours
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
// configure handlebars & register it with express app
|
||||
const hbs = expressHandlebars.create({
|
||||
defaultLayout: 'embed', // sets the default layout
|
||||
handlebars : Handlebars, // includes basic handlebars for access to that library
|
||||
});
|
||||
app.engine('handlebars', hbs.engine);
|
||||
app.set('view engine', 'handlebars');
|
||||
|
||||
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
||||
app.use(populateLocalsDotUser);
|
||||
|
||||
// start the server
|
||||
const start = (config) => {
|
||||
const { mysqlConfig } = config;
|
||||
const db = require('./models')(mysqlConfig); // require our models for syncing
|
||||
db.sequelize
|
||||
// sync sequelize
|
||||
.sync()
|
||||
// require routes
|
||||
.then(() => {
|
||||
require('./routes/auth-routes.js')(app);
|
||||
require('./routes/api-routes.js')(app);
|
||||
require('./routes/page-routes.js')(app);
|
||||
require('./routes/serve-routes.js')(app);
|
||||
require('./routes/fallback-routes.js')(app);
|
||||
const http = require('http');
|
||||
return http.Server(app);
|
||||
})
|
||||
// start the server
|
||||
.then(server => {
|
||||
server.listen(PORT, () => {
|
||||
logger.info('Trusting proxy?', app.get('trust proxy'));
|
||||
logger.info(`Server is listening on PORT ${PORT}`);
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(`Startup Error:`, error);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
speak (something) {
|
||||
console.log(something);
|
||||
},
|
||||
start,
|
||||
start (config) {
|
||||
// parse config parameter
|
||||
const { mysqlConfig, siteConfig, lbrynetConfig } = config;
|
||||
|
||||
// get models
|
||||
const db = require('./models')(mysqlConfig);
|
||||
|
||||
// check for global config variables
|
||||
require('./helpers/configVarCheck.js')(config);
|
||||
|
||||
// trust the proxy to get ip address for us
|
||||
app.enable('trust proxy');
|
||||
|
||||
// add middleware
|
||||
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
|
||||
app.use(express.static(`${__dirname}/public`)); // 'express.static' to serve static files from public directory
|
||||
app.use(bodyParser.json()); // 'body parser' for parsing application/json
|
||||
app.use(bodyParser.urlencoded({ extended: true })); // 'body parser' for parsing application/x-www-form-urlencoded
|
||||
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
|
||||
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
||||
next();
|
||||
});
|
||||
|
||||
// configure passport
|
||||
passport.serializeUser(serializeSpeechUser);
|
||||
passport.deserializeUser(deserializeSpeechUser);
|
||||
const localSignupStrategy = require('./passport/local-signup.js')(db);
|
||||
const localLoginStrategy = require('./passport/local-login.js')(db);
|
||||
passport.use('local-signup', localSignupStrategy);
|
||||
passport.use('local-login', localLoginStrategy);
|
||||
// initialize passport
|
||||
app.use(cookieSession({
|
||||
name : 'session',
|
||||
keys : [siteConfig.session.sessionKey],
|
||||
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
// configure handlebars & register it with express app
|
||||
const hbs = expressHandlebars.create({
|
||||
defaultLayout: 'embed',
|
||||
handlebars : Handlebars,
|
||||
});
|
||||
app.engine('handlebars', hbs.engine);
|
||||
app.set('view engine', 'handlebars');
|
||||
|
||||
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
||||
app.use(populateLocalsDotUser); // note: I don't think I need this any more?
|
||||
|
||||
// start the server
|
||||
module.exports.startServer(db, app, siteConfig, lbrynetConfig);
|
||||
},
|
||||
startServer (db, app, siteConfig, lbrynetConfig) {
|
||||
db.sequelize
|
||||
// sync sequelize
|
||||
.sync()
|
||||
// require routes
|
||||
.then(() => {
|
||||
require('./routes/auth-routes.js')(app, siteConfig, lbrynetConfig);
|
||||
require('./routes/api-routes.js')(app, siteConfig, lbrynetConfig);
|
||||
require('./routes/page-routes.js')(app, siteConfig, lbrynetConfig);
|
||||
require('./routes/serve-routes.js')(app, siteConfig, lbrynetConfig);
|
||||
require('./routes/fallback-routes.js')(app, siteConfig, lbrynetConfig);
|
||||
const http = require('http');
|
||||
return http.Server(app);
|
||||
})
|
||||
// start the server
|
||||
.then(server => {
|
||||
server.listen(PORT, () => {
|
||||
logger.info('Trusting proxy?', app.get('trust proxy'));
|
||||
logger.info(`Server is listening on PORT ${PORT}`);
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(`Startup Error:`, error);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue