turned config files into constructors that can be modified
This commit is contained in:
parent
746ca8c527
commit
bdd2d45ab5
14 changed files with 58549 additions and 101 deletions
|
@ -1,6 +1,8 @@
|
||||||
module.exports = {
|
const lbryConfig = {
|
||||||
api: {
|
api: {
|
||||||
apiHost: 'localhost',
|
apiHost: 'localhost',
|
||||||
apiPort: '5279',
|
apiPort: '5279',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = lbryConfig;
|
||||||
|
|
|
@ -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
12
config/mysqlConfig.js
Normal 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();
|
|
@ -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
12
config/slackConfig.js
Normal 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();
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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
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
139
models/index.js
139
models/index.js
|
@ -1,78 +1,77 @@
|
||||||
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
|
||||||
logging : false,
|
logging : false,
|
||||||
pool : {
|
pool : {
|
||||||
max : 5,
|
max : 5,
|
||||||
min : 0,
|
min : 0,
|
||||||
idle : 10000,
|
idle : 10000,
|
||||||
acquire: 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
|
// manually add each model to the db object
|
||||||
sequelize
|
const Certificate = require('./certificate.js');
|
||||||
.authenticate()
|
const Channel = require('./channel.js');
|
||||||
.then(() => {
|
const Claim = require('./claim.js');
|
||||||
logger.info('Sequelize has established mysql connection successfully.');
|
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 => {
|
.then(obj => {
|
||||||
logger.error('Sequelize was unable to connect to the database:', err);
|
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;
|
||||||
|
|
47653
public/bundle/bundle.js
47653
public/bundle/bundle.js
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
34
server.js
34
server.js
|
@ -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, () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue