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: {
apiHost: 'localhost',
apiPort: '5279',
},
};
module.exports = lbryConfig;

View file

@ -1,3 +1,5 @@
module.exports = {
const loggerConfig = {
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: {
username: 'lbry',
password: 'yYa5B6f7WuGq1613q9D7UWP3HT',
@ -21,3 +21,5 @@ module.exports = {
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: {
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.',
},
};
module.exports = speechConfig;

View file

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

View file

@ -1,6 +1,7 @@
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;
if (slackWebHook) {
// 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,78 +1,77 @@
const Sequelize = require('sequelize');
const logger = require('winston');
module.exports = (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,
},
console.log('exporting sequelize models');
const { database, username, password } = require('../config/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);
});
// establish mysql connection
sequelize
.authenticate()
.then(() => {
logger.info('Sequelize has established mysql connection successfully.');
// 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,
})
.catch(err => {
logger.error('Sequelize was unable to connect to the database:', err);
.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;
});
// 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;
});
};
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 { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
const cookieSession = require('cookie-session');
const http = require('http');
// logging dependencies
const logger = require('winston');
function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) {
this.mysqlConfig = mysqlConfig;
this.siteConfig = siteConfig;
this.slackConfig = slackConfig;
this.lbrynetConfig = lbrynetConfig;
this.db = require('./models')(mysqlConfig);
function SpeechServer ({ mysqlConfig, siteConfig, slackConfig }) {
this.PORT = 3000;
this.speak = (something) => {
console.log(something);
};
this.start = () => {
this.configureConfigFiles();
this.configureLogging();
this.configureApp();
this.configureServer();
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 = () => {
require('./helpers/configureLogger.js')(logger);
require('./helpers/configureSlack.js')(logger, this.slackConfig);
require('./helpers/configureSlack.js')(logger);
};
this.configureApp = () => {
const app = express(); // create an Express application
@ -49,14 +56,14 @@ function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) {
// configure passport
passport.serializeUser(serializeSpeechUser);
passport.deserializeUser(deserializeSpeechUser);
const localSignupStrategy = require('./passport/local-signup.js')(this.db);
const localLoginStrategy = require('./passport/local-login.js')(this.db);
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 : [this.siteConfig.session.sessionKey],
keys : [siteConfig.session.sessionKey],
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
}));
app.use(passport.initialize());
@ -83,15 +90,12 @@ function SpeechServer ({mysqlConfig, siteConfig, slackConfig, lbrynetConfig}) {
this.app = app;
};
this.configureServer = () => {
const http = require('http');
this.server = http.Server(this.app);
};
this.startServer = () => {
// print config variables
require('./helpers/configVarCheck.js')(this.config);
this.db.sequelize
const db = require('./models');
// sync sequelize
.sync()
db.sequelize.sync()
// start the server
.then(() => {
this.server.listen(this.PORT, () => {