Speech as a package #416

Merged
bones7242 merged 89 commits from speech-as-a-package into master 2018-04-18 21:47:34 +02:00
26 changed files with 439 additions and 353 deletions
Showing only changes of commit 9523890b65 - Show all commits

View file

@ -0,0 +1,5 @@
const multipart = require('connect-multiparty');
const { publishing: { uploadDirectory } } = require('siteConfig.js');
const multipartMiddleware = multipart({uploadDir: uploadDirectory});
module.exports = multipartMiddleware;

View file

@ -8,8 +8,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const channelAvailability = () => {
return ({ ip, originalUrl, params: { name } }, res) => {
const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => {
const gaStartTime = Date.now();
checkChannelAvailability(name)
.then(availableName => {
@ -19,7 +18,6 @@ const channelAvailability = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = channelAvailability;

View file

@ -9,8 +9,7 @@ const NO_CHANNEL = 'NO_CHANNEL';
*/
const channelClaims = () => {
return ({ ip, originalUrl, body, params }, res) => {
const channelClaims = ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
@ -25,7 +24,6 @@ const channelClaims = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = channelClaims;

View file

@ -9,8 +9,7 @@ const NO_CHANNEL = 'NO_CHANNEL';
*/
const channelData = () => {
return ({ ip, originalUrl, body, params }, res) => {
const channelData = ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
@ -24,7 +23,6 @@ const channelData = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = channelData;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/*
@ -6,8 +7,7 @@ route to get a short channel id from long channel Id
*/
const channelShortIdRoute = (db) => {
return ({ ip, originalUrl, params }, res) => {
const channelShortIdRoute = ({ ip, originalUrl, params }, res) => {
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
res.status(200).json(shortId);
@ -15,7 +15,6 @@ const channelShortIdRoute = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = channelShortIdRoute;

View file

@ -8,8 +8,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimAvailability = () => {
return ({ ip, originalUrl, params: { name } }, res) => {
const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => {
const gaStartTime = Date.now();
claimNameIsAvailable(name)
.then(result => {
@ -19,7 +18,6 @@ const claimAvailability = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimAvailability;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/*
@ -6,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimData = (db) => {
return ({ ip, originalUrl, body, params }, res) => {
const claimData = ({ ip, originalUrl, body, params }, res) => {
const claimName = params.claimName;
let claimId = params.claimId;
if (claimId === 'none') claimId = null;
@ -21,7 +21,6 @@ const claimData = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimData;

View file

@ -1,6 +1,7 @@
const { getClaim } = require('helpers/lbryApi.js');
const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js');
const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/*
@ -8,8 +9,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimGet = (db) => {
return ({ ip, originalUrl, params }, res) => {
const claimGet = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
// resolve the claim
@ -33,7 +33,6 @@ const claimGet = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimGet;

View file

@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimList = (db) => {
return ({ ip, originalUrl, params }, res) => {
const claimList = ({ ip, originalUrl, params }, res) => {
getClaimList(params.name)
.then(claimsList => {
res.status(200).json(claimsList);
@ -16,7 +15,6 @@ const claimList = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimList;

View file

@ -10,8 +10,7 @@ const NO_CLAIM = 'NO_CLAIM';
*/
const claimLongId = () => {
return ({ ip, originalUrl, body, params }, res) => {
const claimLongId = ({ ip, originalUrl, body, params }, res) => {
const channelName = body.channelName;
const channelClaimId = body.channelClaimId;
const claimName = body.claimName;
@ -29,7 +28,6 @@ const claimLongId = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimLongId;

View file

@ -11,8 +11,7 @@ const { details: { host } } = require('siteConfig.js');
*/
const claimPublish = (db) => {
return ({ body, files, headers, ip, originalUrl, user }, res) => {
const claimPublish = ({ body, files, headers, ip, originalUrl, user }, res) => {
// define variables
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
// record the start time of the request
@ -64,7 +63,6 @@ const claimPublish = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimPublish;

View file

@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimResolve = () => {
return ({ headers, ip, originalUrl, params }, res) => {
const claimResolve = ({ headers, ip, originalUrl, params }, res) => {
resolveUri(`${params.name}#${params.claimId}`)
.then(resolvedUri => {
res.status(200).json(resolvedUri);
@ -16,7 +15,6 @@ const claimResolve = () => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimResolve;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/*
@ -6,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const claimShortId = (db) => {
return ({ ip, originalUrl, body, params }, res) => {
const claimShortId = ({ ip, originalUrl, body, params }, res) => {
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
.then(shortId => {
res.status(200).json({success: true, data: shortId});
@ -15,7 +15,6 @@ const claimShortId = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = claimShortId;

View file

@ -1,5 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/*
@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/
const fileAvailability = (db) => {
return ({ ip, originalUrl, params }, res) => {
const fileAvailability = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
db.File
@ -27,7 +26,6 @@ const fileAvailability = (db) => {
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
};
};
module.exports = fileAvailability;

View file

@ -12,18 +12,23 @@ const claimShortId = require('./claimShortId');
const claimList = require('./claimList');
const fileAvailability = require('./fileAvailability');
module.exports = {
channelAvailability,
channelClaims,
channelData,
channelShortId,
claimAvailability,
claimData,
claimGet,
claimLongId,
claimPublish,
claimResolve,
claimShortId,
claimList,
fileAvailability,
const multipartMiddleware = require('helpers/multipartMiddleware');
module.exports = (app) => {
// channel routes
app.get('/api/channel/availability/:name', channelAvailability);
app.get('/api/channel/short-id/:longId/:name', channelShortId);
app.get('/api/channel/data/:channelName/:channelClaimId', channelData);
app.get('/api/channel/claims/:channelName/:channelClaimId/:page', channelClaims);
// claim routes
app.get('/api/claim/list/:name', claimList);
app.get('/api/claim/get/:name/:claimId', claimGet);
app.get('/api/claim/availability/:name', claimAvailability);
app.get('/api/claim/resolve/:name/:claimId', claimResolve);
app.post('/api/claim/publish', multipartMiddleware, claimPublish);
app.get('/api/claim/short-id/:longId/:name', claimShortId);
app.post('/api/claim/long-id', claimLongId);
app.get('/api/claim/data/:claimName/:claimId', claimData);
// file routes
app.get('/api/file/availability/:name/:claimId', fileAvailability);
};

View file

@ -1,44 +0,0 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const { determineResponseType, logRequestData, getClaimIdAndServeAsset } = require('helpers/serveHelpers.js');
const lbryUri = require('helpers/lbryUri.js');
const handleShowRender = require('helpers/handleShowRender.jsx');
const SERVE = 'SERVE';
/*
route to serve an asset or the react app via the claim name only
*/
const claim = () => {
return (req, res) => {
const { headers, ip, originalUrl, params } = req;
// decide if this is a show request
let hasFileExtension;
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
return handleShowRender(req, res);
}
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
// parse the claim
let claimName;
try {
({claimName} = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// log the request data for debugging
logRequestData(responseType, claimName, null, null);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
};
};
module.exports = claim;

View file

@ -1,55 +0,0 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('helpers/serveHelpers.js');
const lbryUri = require('helpers/lbryUri.js');
const handleShowRender = require('helpers/handleShowRender.jsx');
const SERVE = 'SERVE';
/*
route to serve an asset or the react app via the claim name and an identifier
*/
const identifierClaim = () => {
return (req, res) => {
const { headers, ip, originalUrl, params } = req;
// decide if this is a show request
let hasFileExtension;
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
return handleShowRender(req, res);
}
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
// parse the claim
let claimName;
try {
({ claimName } = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// parse the identifier
let isChannel, channelName, channelClaimId, claimId;
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
}
// log the request data for debugging
logRequestData(responseType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};
};
module.exports = identifierClaim;

View file

@ -1,8 +1,7 @@
const serveAssetByClaim = require('./serveAssetByClaim');
const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim');
module.exports = {
serveAssetByClaim,
serveAssetByIdentifierAndClaim,
module.exports = (app, db) => {
app.get('/:identifier/:claim', serveAssetByIdentifierAndClaim);
app.get('/:claim', serveAssetByClaim);
};

View file

@ -0,0 +1,42 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const { determineResponseType, logRequestData, getClaimIdAndServeAsset } = require('helpers/serveHelpers.js');
const lbryUri = require('helpers/lbryUri.js');
const handleShowRender = require('helpers/handleShowRender.jsx');
const SERVE = 'SERVE';
/*
route to serve an asset or the react app via the claim name only
*/
const serverAssetByClaim = (req, res) => {
const { headers, ip, originalUrl, params } = req;
// decide if this is a show request
let hasFileExtension;
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
return handleShowRender(req, res);
}
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
// parse the claim
let claimName;
try {
({claimName} = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// log the request data for debugging
logRequestData(responseType, claimName, null, null);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
};
module.exports = serverAssetByClaim;

View file

@ -0,0 +1,58 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const {
determineResponseType,
flipClaimNameAndIdForBackwardsCompatibility,
logRequestData,
getClaimIdAndServeAsset,
} = require('helpers/serveHelpers.js');
const lbryUri = require('helpers/lbryUri.js');
const handleShowRender = require('helpers/handleShowRender.jsx');
const SERVE = 'SERVE';
/*
route to serve an asset or the react app via the claim name and an identifier
*/
const serverAssetByIdentifierAndClaim = (req, res) => {
const { headers, ip, originalUrl, params } = req;
// decide if this is a show request
let hasFileExtension;
try {
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
let responseType = determineResponseType(hasFileExtension, headers);
if (responseType !== SERVE) {
return handleShowRender(req, res);
}
// handle serve request
// send google analytics
sendGAServeEvent(headers, ip, originalUrl);
// parse the claim
let claimName;
try {
({ claimName } = lbryUri.parseClaim(params.claim));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// parse the identifier
let isChannel, channelName, channelClaimId, claimId;
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
}
// log the request data for debugging
logRequestData(responseType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};
module.exports = serverAssetByIdentifierAndClaim;

98
server/server.js Normal file
View file

@ -0,0 +1,98 @@
// app dependencies
const express = require('express');
const bodyParser = require('body-parser');
const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const helmet = require('helmet');
const passport = require('passport');
const { serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
const cookieSession = require('cookie-session');
const http = require('http');
// logging dependencies
const logger = require('winston');
function Server () {
this.configureMysql = (mysqlConfig) => {
require('../config/mysqlConfig.js').configure(mysqlConfig);
};
this.configureSite = (siteConfig) => {
require('../config/siteConfig.js').configure(siteConfig);
this.sessionKey = siteConfig.auth.sessionKey;
this.PORT = siteConfig.details.port;
};
this.configureSlack = (slackConfig) => {
require('../config/slackConfig.js').configure(slackConfig);
};
this.createApp = () => {
// create an Express application
const app = express();
// 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 : [this.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');
// set the routes on the app
require('./routes/auth.js')(app);
require('./routes/api.js')(app);
require('./routes/pages.js')(app);
require('./routes/assets.js')(app);
require('./routes/fallback.js')(app);
this.app = app;
};
this.initialize = () => {
require('./helpers/configureLogger.js')(logger);
require('./helpers/configureSlack.js')(logger);
this.createApp();
this.server = http.Server(this.app);
};
this.start = () => {
const db = require('./models/index');
// sync sequelize
db.sequelize.sync()
// start the server
.then(() => {
this.server.listen(this.PORT, () => {
logger.info(`Server is listening on PORT ${this.PORT}`);
});
})
.catch((error) => {
logger.error(`Startup Error:`, error);
});
};
};
module.exports = Server;

View file

@ -4,9 +4,9 @@ import rootSaga from 'sagas';
import GAListener from 'components/GAListener';
const api = require('./server/routes/api/');
const asset = require('./server/routes/asset/');
const asset = require('./server/routes/assets/');
const auth = require('./server/routes/auth/');
const page = require('./server/routes/page/');
const page = require('./server/routes/pages/');
const logger = require('./config/loggerConfig.js');
const mysql = require('./config/mysqlConfig');
const site = require('./config/siteConfig');