narrowed down to one api route and got it working

This commit is contained in:
bill bittner 2018-03-27 18:25:23 -07:00
parent 195f193259
commit 2cc261f072
7 changed files with 626 additions and 1704 deletions

View file

@ -1,94 +1,11 @@
const Sequelize = require('sequelize');
const logger = require('winston');
function mysql () { function mysql () {
this.database = 'default';
this.username = 'default';
this.password = 'default';
this.db = {}; this.db = {};
this.configure = (config) => { this.configure = (db) => {
if (!config) { if (!db) {
return console.log('No MySQL config received.'); return console.log('No MySQL config received.');
} }
// configure credentials // configure credentials
console.log('configuring mysql credentials...'); console.log('configuring mysql...');
const {database, username, password} = config;
this.database = database;
this.username = username;
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; this.db = db;
}; };
}; };

2148
index.js

File diff suppressed because one or more lines are too long

15
server/models/index.js Normal file
View file

@ -0,0 +1,15 @@
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');
module.exports = {
Certificate,
Channel,
Claim,
File,
Request,
User,
}

View file

@ -0,0 +1,26 @@
const logger = require('winston');
// const { details: { host } } = require('siteConfig.js');
// const { db } = require('mysqlConfig.js');
const { handleErrorResponse } = require('helpers/errorHandlers.js');
/*
route to get a short channel id from long channel Id
*/
const channelShortIdRoute = (db, host) => {
return ({ ip, originalUrl, params }, res) => {
console.log('hello from channelShortIdRoute');
logger.debug('host:', host);
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
res.status(200).json(shortId);
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = channelShortIdRoute;

View file

@ -0,0 +1,5 @@
const channelShortId = require('./channelShortId');
module.exports = {
channelShortId,
}

View file

@ -1,18 +1,18 @@
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('../../config/mysqlConfig.js'); 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');
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../../helpers/errorHandlers.js');
const { sendGATimingEvent } = require('../helpers/googleAnalytics.js'); const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js');
const { authenticateUser } = require('../auth/authentication.js'); const { authenticateUser } = require('../../auth/authentication.js');
const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js'); const { getChannelData, getChannelClaims, getClaimId } = require('../../controllers/serveController.js');
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
const apiRoutes = { const routes = {
// 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();
@ -25,19 +25,6 @@ const apiRoutes = {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res); errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
}); });
}, },
// route to get a short channel id from long channel Id
channelShortIdRoute ({ ip, originalUrl, params }, res) {
console.log('hello');
logger.debug('host:', host);
logger.debug('db:', db);
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
res.status(200).json(shortId);
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
});
},
channelDataRoute ({ ip, originalUrl, body, params }, res) { channelDataRoute ({ ip, originalUrl, body, params }, res) {
const channelName = params.channelName; const channelName = params.channelName;
let channelClaimId = params.channelClaimId; let channelClaimId = params.channelClaimId;
@ -242,4 +229,4 @@ const apiRoutes = {
}, },
}; };
module.exports = apiRoutes; module.exports = routes;

View file

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