turned config files into constructors that can be modified

This commit is contained in:
bill bittner 2018-03-09 16:45:24 -08:00
parent 746ca8c527
commit bdd2d45ab5
14 changed files with 58549 additions and 101 deletions

View file

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

View file

@ -1,3 +1,5 @@
module.exports = { const loggerConfig = {
logLevel: 'debug', // options: silly, debug, verbose, info logLevel: 'debug', // options: silly, debug, verbose, info
}; };
module.exports = loggerConfig;

12
config/mysqlConfig.js Normal file
View file

@ -0,0 +1,12 @@
function MysqlConfig () {
this.database = 'default';
this.username = 'default';
this.password = 'default';
this.configure = ({database, username, password}) => {
if (database) this.database = database;
if (username) this.username = username;
if (password) this.password = password;
};
};
module.exports = new MysqlConfig();

View file

@ -1,4 +1,4 @@
module.exports = { const sequelizeCliConfig = {
development: { development: {
username: 'lbry', username: 'lbry',
password: 'yYa5B6f7WuGq1613q9D7UWP3HT', password: 'yYa5B6f7WuGq1613q9D7UWP3HT',
@ -21,3 +21,5 @@ module.exports = {
dialect : 'mysql', dialect : 'mysql',
}, },
}; };
module.exports = sequelizeCliConfig;

12
config/slackConfig.js Normal file
View file

@ -0,0 +1,12 @@
function SlackConfig () {
this.slackWebHook = 'default';
this.slackErrorChannel = 'default';
this.slackInfoChannel = 'default';
this.configure = ({slackWebHook, slackErrorChannel, slackInfoChannel}) => {
if (slackWebHook) this.slackWebHook = slackWebHook;
if (slackErrorChannel) this.slackErrorChannel = slackErrorChannel;
if (slackInfoChannel) this.slackInfoChannel = slackInfoChannel;
};
};
module.exports = new SlackConfig();

View file

@ -1,4 +1,4 @@
module.exports = { const speechConfig = {
analytics: { analytics: {
googleId: 'UA-60403362-6', // google id for analytics tracking; leave `null` if not applicable googleId: 'UA-60403362-6', // google id for analytics tracking; leave `null` if not applicable
}, },
@ -26,3 +26,5 @@ module.exports = {
defaultDescription: 'Open-source, decentralized image and video sharing.', defaultDescription: 'Open-source, decentralized image and video sharing.',
}, },
}; };
module.exports = speechConfig;

View file

@ -1,5 +1,7 @@
module.exports = { const testConfig = module.exports = {
testChannel : '@test', testChannel : '@test',
testChannelId : '3b5bc6b6819172c6e2f3f90aa855b14a956b4a82', testChannelId : '3b5bc6b6819172c6e2f3f90aa855b14a956b4a82',
testChannelPassword: '1234', testChannelPassword: '1234',
}; };
module.exports = testConfig;

View file

@ -1,6 +1,7 @@
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook; const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
const slackConfig = require('../config/slackConfig.js');
module.exports = (winston, slackConfig) => { module.exports = (winston) => {
const {slackWebHook, slackErrorChannel, slackInfoChannel} = slackConfig; const {slackWebHook, slackErrorChannel, slackInfoChannel} = slackConfig;
if (slackWebHook) { if (slackWebHook) {
// add a transport for errors to slack // add a transport for errors to slack

10773
index.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -7,28 +7,35 @@ const helmet = require('helmet');
const passport = require('passport'); const passport = require('passport');
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js'); const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
const cookieSession = require('cookie-session'); const cookieSession = require('cookie-session');
const http = require('http');
// logging dependencies // logging dependencies
const logger = require('winston'); const logger = require('winston');
function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) { function SpeechServer ({ mysqlConfig, siteConfig, slackConfig }) {
this.mysqlConfig = mysqlConfig;
this.siteConfig = siteConfig;
this.slackConfig = slackConfig;
this.lbrynetConfig = lbrynetConfig;
this.db = require('./models')(mysqlConfig);
this.PORT = 3000; this.PORT = 3000;
this.speak = (something) => { this.speak = (something) => {
console.log(something); console.log(something);
}; };
this.start = () => { this.start = () => {
this.configureConfigFiles();
this.configureLogging(); this.configureLogging();
this.configureApp(); this.configureApp();
this.configureServer(); this.configureServer();
this.startServer(); this.startServer();
}; };
this.configureConfigFiles = () => {
const mysqlAppConfig = require('./config/mysqlConfig.js');
mysqlAppConfig.configure(mysqlConfig);
const slackAppConfig = require('./config/slackConfig.js');
slackAppConfig.configure(slackConfig);
// print the config variables
console.log('configured config files');
require('./helpers/configVarCheck.js')(mysqlAppConfig);
require('./helpers/configVarCheck.js')(slackAppConfig);
};
this.configureLogging = () => { this.configureLogging = () => {
require('./helpers/configureLogger.js')(logger); require('./helpers/configureLogger.js')(logger);
require('./helpers/configureSlack.js')(logger, this.slackConfig); require('./helpers/configureSlack.js')(logger);
}; };
this.configureApp = () => { this.configureApp = () => {
const app = express(); // create an Express application const app = express(); // create an Express application
@ -49,14 +56,14 @@ function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) {
// configure passport // configure passport
passport.serializeUser(serializeSpeechUser); passport.serializeUser(serializeSpeechUser);
passport.deserializeUser(deserializeSpeechUser); passport.deserializeUser(deserializeSpeechUser);
const localSignupStrategy = require('./passport/local-signup.js')(this.db); const localSignupStrategy = require('./passport/local-signup.js');
const localLoginStrategy = require('./passport/local-login.js')(this.db); const localLoginStrategy = require('./passport/local-login.js');
passport.use('local-signup', localSignupStrategy); passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy); passport.use('local-login', localLoginStrategy);
// initialize passport // initialize passport
app.use(cookieSession({ app.use(cookieSession({
name : 'session', name : 'session',
keys : [this.siteConfig.session.sessionKey], keys : [siteConfig.session.sessionKey],
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
})); }));
app.use(passport.initialize()); app.use(passport.initialize());
@ -83,15 +90,12 @@ function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) {
this.app = app; this.app = app;
}; };
this.configureServer = () => { this.configureServer = () => {
const http = require('http');
this.server = http.Server(this.app); this.server = http.Server(this.app);
}; };
this.startServer = () => { this.startServer = () => {
// print config variables const db = require('./models');
require('./helpers/configVarCheck.js')(this.config);
this.db.sequelize
// sync sequelize // sync sequelize
.sync() db.sequelize.sync()
// start the server // start the server
.then(() => { .then(() => {
this.server.listen(this.PORT, () => { this.server.listen(this.PORT, () => {