Speech as a package #416
26 changed files with 439 additions and 353 deletions
5
server/helpers/multipartMiddleware.js
Normal file
5
server/helpers/multipartMiddleware.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
const multipart = require('connect-multiparty');
|
||||||
|
const { publishing: { uploadDirectory } } = require('siteConfig.js');
|
||||||
|
const multipartMiddleware = multipart({uploadDir: uploadDirectory});
|
||||||
|
|
||||||
|
module.exports = multipartMiddleware;
|
|
@ -8,8 +8,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const channelAvailability = () => {
|
const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => {
|
||||||
return ({ ip, originalUrl, params: { name } }, res) => {
|
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
checkChannelAvailability(name)
|
checkChannelAvailability(name)
|
||||||
.then(availableName => {
|
.then(availableName => {
|
||||||
|
@ -20,6 +19,5 @@ const channelAvailability = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = channelAvailability;
|
module.exports = channelAvailability;
|
||||||
|
|
|
@ -9,8 +9,7 @@ const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const channelClaims = () => {
|
const channelClaims = ({ ip, originalUrl, body, params }, res) => {
|
||||||
return ({ ip, originalUrl, body, params }, res) => {
|
|
||||||
const channelName = params.channelName;
|
const channelName = params.channelName;
|
||||||
let channelClaimId = params.channelClaimId;
|
let channelClaimId = params.channelClaimId;
|
||||||
if (channelClaimId === 'none') channelClaimId = null;
|
if (channelClaimId === 'none') channelClaimId = null;
|
||||||
|
@ -26,6 +25,5 @@ const channelClaims = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = channelClaims;
|
module.exports = channelClaims;
|
||||||
|
|
|
@ -9,8 +9,7 @@ const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const channelData = () => {
|
const channelData = ({ ip, originalUrl, body, params }, res) => {
|
||||||
return ({ ip, originalUrl, body, params }, res) => {
|
|
||||||
const channelName = params.channelName;
|
const channelName = params.channelName;
|
||||||
let channelClaimId = params.channelClaimId;
|
let channelClaimId = params.channelClaimId;
|
||||||
if (channelClaimId === 'none') channelClaimId = null;
|
if (channelClaimId === 'none') channelClaimId = null;
|
||||||
|
@ -25,6 +24,5 @@ const channelData = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = channelData;
|
module.exports = channelData;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
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) => {
|
const channelShortIdRoute = ({ ip, originalUrl, params }, res) => {
|
||||||
return ({ ip, originalUrl, params }, res) => {
|
|
||||||
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
||||||
.then(shortId => {
|
.then(shortId => {
|
||||||
res.status(200).json(shortId);
|
res.status(200).json(shortId);
|
||||||
|
@ -16,6 +16,5 @@ const channelShortIdRoute = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = channelShortIdRoute;
|
module.exports = channelShortIdRoute;
|
||||||
|
|
|
@ -8,8 +8,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimAvailability = () => {
|
const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => {
|
||||||
return ({ ip, originalUrl, params: { name } }, res) => {
|
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
claimNameIsAvailable(name)
|
claimNameIsAvailable(name)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
|
@ -20,6 +19,5 @@ const claimAvailability = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimAvailability;
|
module.exports = claimAvailability;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
const db = require('models');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -6,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimData = (db) => {
|
const claimData = ({ ip, originalUrl, body, params }, res) => {
|
||||||
return ({ ip, originalUrl, body, params }, res) => {
|
|
||||||
const claimName = params.claimName;
|
const claimName = params.claimName;
|
||||||
let claimId = params.claimId;
|
let claimId = params.claimId;
|
||||||
if (claimId === 'none') claimId = null;
|
if (claimId === 'none') claimId = null;
|
||||||
|
@ -22,6 +22,5 @@ const claimData = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimData;
|
module.exports = claimData;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const { getClaim } = require('helpers/lbryApi.js');
|
const { getClaim } = require('helpers/lbryApi.js');
|
||||||
const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js');
|
const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js');
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
const db = require('models');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -8,8 +9,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimGet = (db) => {
|
const claimGet = ({ ip, originalUrl, params }, res) => {
|
||||||
return ({ ip, originalUrl, params }, res) => {
|
|
||||||
const name = params.name;
|
const name = params.name;
|
||||||
const claimId = params.claimId;
|
const claimId = params.claimId;
|
||||||
// resolve the claim
|
// resolve the claim
|
||||||
|
@ -34,6 +34,5 @@ const claimGet = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimGet;
|
module.exports = claimGet;
|
||||||
|
|
|
@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimList = (db) => {
|
const claimList = ({ ip, originalUrl, params }, res) => {
|
||||||
return ({ ip, originalUrl, params }, res) => {
|
|
||||||
getClaimList(params.name)
|
getClaimList(params.name)
|
||||||
.then(claimsList => {
|
.then(claimsList => {
|
||||||
res.status(200).json(claimsList);
|
res.status(200).json(claimsList);
|
||||||
|
@ -17,6 +16,5 @@ const claimList = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimList;
|
module.exports = claimList;
|
||||||
|
|
|
@ -10,8 +10,7 @@ const NO_CLAIM = 'NO_CLAIM';
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimLongId = () => {
|
const claimLongId = ({ ip, originalUrl, body, params }, res) => {
|
||||||
return ({ ip, originalUrl, body, params }, res) => {
|
|
||||||
const channelName = body.channelName;
|
const channelName = body.channelName;
|
||||||
const channelClaimId = body.channelClaimId;
|
const channelClaimId = body.channelClaimId;
|
||||||
const claimName = body.claimName;
|
const claimName = body.claimName;
|
||||||
|
@ -30,6 +29,5 @@ const claimLongId = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimLongId;
|
module.exports = claimLongId;
|
||||||
|
|
|
@ -11,8 +11,7 @@ const { details: { host } } = require('siteConfig.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimPublish = (db) => {
|
const claimPublish = ({ body, files, headers, ip, originalUrl, user }, res) => {
|
||||||
return ({ body, files, headers, ip, originalUrl, user }, res) => {
|
|
||||||
// define variables
|
// define variables
|
||||||
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
|
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
|
||||||
// record the start time of the request
|
// record the start time of the request
|
||||||
|
@ -65,6 +64,5 @@ const claimPublish = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimPublish;
|
module.exports = claimPublish;
|
||||||
|
|
|
@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimResolve = () => {
|
const claimResolve = ({ headers, ip, originalUrl, params }, res) => {
|
||||||
return ({ headers, ip, originalUrl, params }, res) => {
|
|
||||||
resolveUri(`${params.name}#${params.claimId}`)
|
resolveUri(`${params.name}#${params.claimId}`)
|
||||||
.then(resolvedUri => {
|
.then(resolvedUri => {
|
||||||
res.status(200).json(resolvedUri);
|
res.status(200).json(resolvedUri);
|
||||||
|
@ -17,6 +16,5 @@ const claimResolve = () => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimResolve;
|
module.exports = claimResolve;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
const db = require('models');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -6,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const claimShortId = (db) => {
|
const claimShortId = ({ ip, originalUrl, body, params }, res) => {
|
||||||
return ({ ip, originalUrl, body, params }, res) => {
|
|
||||||
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
||||||
.then(shortId => {
|
.then(shortId => {
|
||||||
res.status(200).json({success: true, data: shortId});
|
res.status(200).json({success: true, data: shortId});
|
||||||
|
@ -16,6 +16,5 @@ const claimShortId = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = claimShortId;
|
module.exports = claimShortId;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
const db = require('models');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -7,8 +7,7 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const fileAvailability = (db) => {
|
const fileAvailability = ({ ip, originalUrl, params }, res) => {
|
||||||
return ({ ip, originalUrl, params }, res) => {
|
|
||||||
const name = params.name;
|
const name = params.name;
|
||||||
const claimId = params.claimId;
|
const claimId = params.claimId;
|
||||||
db.File
|
db.File
|
||||||
|
@ -28,6 +27,5 @@ const fileAvailability = (db) => {
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = fileAvailability;
|
module.exports = fileAvailability;
|
||||||
|
|
|
@ -12,18 +12,23 @@ const claimShortId = require('./claimShortId');
|
||||||
const claimList = require('./claimList');
|
const claimList = require('./claimList');
|
||||||
const fileAvailability = require('./fileAvailability');
|
const fileAvailability = require('./fileAvailability');
|
||||||
|
|
||||||
module.exports = {
|
const multipartMiddleware = require('helpers/multipartMiddleware');
|
||||||
channelAvailability,
|
|
||||||
channelClaims,
|
module.exports = (app) => {
|
||||||
channelData,
|
// channel routes
|
||||||
channelShortId,
|
app.get('/api/channel/availability/:name', channelAvailability);
|
||||||
claimAvailability,
|
app.get('/api/channel/short-id/:longId/:name', channelShortId);
|
||||||
claimData,
|
app.get('/api/channel/data/:channelName/:channelClaimId', channelData);
|
||||||
claimGet,
|
app.get('/api/channel/claims/:channelName/:channelClaimId/:page', channelClaims);
|
||||||
claimLongId,
|
// claim routes
|
||||||
claimPublish,
|
app.get('/api/claim/list/:name', claimList);
|
||||||
claimResolve,
|
app.get('/api/claim/get/:name/:claimId', claimGet);
|
||||||
claimShortId,
|
app.get('/api/claim/availability/:name', claimAvailability);
|
||||||
claimList,
|
app.get('/api/claim/resolve/:name/:claimId', claimResolve);
|
||||||
fileAvailability,
|
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);
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
|
|
@ -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;
|
|
|
@ -1,8 +1,7 @@
|
||||||
const serveAssetByClaim = require('./serveAssetByClaim');
|
const serveAssetByClaim = require('./serveAssetByClaim');
|
||||||
const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim');
|
const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim');
|
||||||
|
|
||||||
|
module.exports = (app, db) => {
|
||||||
module.exports = {
|
app.get('/:identifier/:claim', serveAssetByIdentifierAndClaim);
|
||||||
serveAssetByClaim,
|
app.get('/:claim', serveAssetByClaim);
|
||||||
serveAssetByIdentifierAndClaim,
|
|
||||||
};
|
};
|
42
server/routes/assets/serveAssetByClaim.js
Normal file
42
server/routes/assets/serveAssetByClaim.js
Normal 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;
|
58
server/routes/assets/serveAssetByIdentifierAndClaim.js
Normal file
58
server/routes/assets/serveAssetByIdentifierAndClaim.js
Normal 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
98
server/server.js
Normal 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;
|
|
@ -4,9 +4,9 @@ import rootSaga from 'sagas';
|
||||||
import GAListener from 'components/GAListener';
|
import GAListener from 'components/GAListener';
|
||||||
|
|
||||||
const api = require('./server/routes/api/');
|
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 auth = require('./server/routes/auth/');
|
||||||
const page = require('./server/routes/page/');
|
const page = require('./server/routes/pages/');
|
||||||
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');
|
||||||
|
|
Loading…
Reference in a new issue