mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-06 16:32:40 +00:00
Move "get brew" logic to common function
Also add centralized error handling middleware
This commit is contained in:
179
server.js
179
server.js
@@ -9,6 +9,45 @@ const GoogleActions = require('./server/googleActions.js');
|
|||||||
const serveCompressedStaticAssets = require('./server/static-assets.mv.js');
|
const serveCompressedStaticAssets = require('./server/static-assets.mv.js');
|
||||||
const sanitizeFilename = require('sanitize-filename');
|
const sanitizeFilename = require('sanitize-filename');
|
||||||
|
|
||||||
|
// //Custom Error
|
||||||
|
// class ErrorHandler extends Error {
|
||||||
|
// constructor(statusCode, message) {
|
||||||
|
// super();
|
||||||
|
// this.statusCode = statusCode;
|
||||||
|
// this.message = message;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
//Format brew source for viewing in the browser as plain text
|
||||||
|
// const sanitizeSource = (text)=>{
|
||||||
|
// const replaceStrings = { '&' : '&',
|
||||||
|
// '<' : '<',
|
||||||
|
// '>' : '>' };
|
||||||
|
// for (const replaceStr in replaceStrings) {
|
||||||
|
// text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
||||||
|
// }
|
||||||
|
// return `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
||||||
|
// };
|
||||||
|
|
||||||
|
//Get the brew object from the HB database or Google Drive
|
||||||
|
const getBrewFromId = async (id, accessType)=>{
|
||||||
|
if(accessType !== 'edit' && accessType !== 'share')
|
||||||
|
throw ('Invalid Access Type when getting brew');
|
||||||
|
let brew;
|
||||||
|
if(id.length > 12) {
|
||||||
|
const googleId = id.slice(0, -12);
|
||||||
|
id = id.slice(-12);
|
||||||
|
brew = await GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, id, accessType)
|
||||||
|
.catch((err)=>{throw err;});
|
||||||
|
} else {
|
||||||
|
brew = await HomebrewModel.get(accessType == 'edit' ? { editId: id } : { shareId: id })
|
||||||
|
.catch((err)=>{throw err;});
|
||||||
|
brew.sanatize(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return brew;
|
||||||
|
};
|
||||||
|
|
||||||
app.use('/', serveCompressedStaticAssets(`${__dirname}/build`));
|
app.use('/', serveCompressedStaticAssets(`${__dirname}/build`));
|
||||||
|
|
||||||
process.chdir(__dirname);
|
process.chdir(__dirname);
|
||||||
@@ -65,6 +104,10 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
return res.sendFile(`${__dirname}/robots.txt`);
|
return res.sendFile(`${__dirname}/robots.txt`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// app.get('/error', (req, res)=>{
|
||||||
|
// throw new ErrorHandler(404, 'User with the specified email does not exist');
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
//Source page
|
//Source page
|
||||||
app.get('/source/:id', (req, res)=>{
|
app.get('/source/:id', (req, res)=>{
|
||||||
@@ -170,119 +213,48 @@ app.get('/user/:username', async (req, res, next)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Edit Page
|
//Edit Page
|
||||||
app.get('/edit/:id', (req, res, next)=>{
|
app.get('/edit/:id', async (req, res, next)=>{
|
||||||
res.header('Cache-Control', 'no-cache, no-store'); //reload the latest saved brew when pressing back button, not the cached version before save.
|
res.header('Cache-Control', 'no-cache, no-store'); //reload the latest saved brew when pressing back button, not the cached version before save.
|
||||||
if(req.params.id.length > 12) {
|
const brew = await getBrewFromId(req.params.id, 'edit')
|
||||||
const googleId = req.params.id.slice(0, -12);
|
.catch((err)=>{next(err);});
|
||||||
const editId = req.params.id.slice(-12);
|
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, editId, 'edit')
|
req.brew = brew;
|
||||||
.then((brew)=>{
|
return next();
|
||||||
req.brew = brew; //TODO Need to sanitize later
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send('Can\'t get brew from Google');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
HomebrewModel.get({ editId: req.params.id })
|
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew.sanatize();
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send(`Can't get that`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//New Page
|
//New Page
|
||||||
app.get('/new/:id', (req, res, next)=>{
|
app.get('/new/:id', async (req, res, next)=>{
|
||||||
if(req.params.id.length > 12) {
|
const brew = await getBrewFromId(req.params.id, 'share')
|
||||||
const googleId = req.params.id.slice(0, -12);
|
.catch((err)=>{next(err);});
|
||||||
const shareId = req.params.id.slice(-12);
|
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
req.brew = brew;
|
||||||
.then((brew)=>{
|
return next();
|
||||||
req.brew = brew; //TODO Need to sanitize later
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send('Can\'t get brew from Google');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
HomebrewModel.get({ shareId: req.params.id })
|
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew;
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send(`Can't get that`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Share Page
|
//Share Page
|
||||||
app.get('/share/:id', (req, res, next)=>{
|
app.get('/share/:id', async (req, res, next)=>{
|
||||||
|
const brew = await getBrewFromId(req.params.id, 'share')
|
||||||
|
.catch((err)=>{next(err);});
|
||||||
|
|
||||||
if(req.params.id.length > 12) {
|
if(req.params.id.length > 12) {
|
||||||
const googleId = req.params.id.slice(0, -12);
|
const googleId = req.params.id.slice(0, -12);
|
||||||
const shareId = req.params.id.slice(-12);
|
const shareId = req.params.id.slice(-12);
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
await GoogleActions.increaseView(googleId, shareId, 'share', brew)
|
||||||
.then((brew)=>{
|
.catch((err)=>{next(err);});
|
||||||
GoogleActions.increaseView(googleId, shareId, 'share', brew);
|
|
||||||
return brew;
|
|
||||||
})
|
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew; //TODO Need to sanitize later
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send('Can\'t get brew from Google');
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
HomebrewModel.get({ shareId: req.params.id })
|
await brew.increaseView();
|
||||||
.then((brew)=>{
|
|
||||||
return brew.increaseView();
|
|
||||||
})
|
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew.sanatize(true);
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send(`Can't get that`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.brew = brew;
|
||||||
|
return next();
|
||||||
});
|
});
|
||||||
|
|
||||||
//Print Page
|
//Print Page
|
||||||
app.get('/print/:id', (req, res, next)=>{
|
app.get('/print/:id', async (req, res, next)=>{
|
||||||
if(req.params.id.length > 12) {
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
const googleId = req.params.id.slice(0, -12);
|
|
||||||
const shareId = req.params.id.slice(-12);
|
req.brew = brew;
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
return next();
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew; //TODO Need to sanitize later
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send('Can\'t get brew from Google');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
HomebrewModel.get({ shareId: req.params.id })
|
|
||||||
.then((brew)=>{
|
|
||||||
req.brew = brew.sanatize(true);
|
|
||||||
return next();
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(400).send(`Can't get that`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Render the page
|
//Render the page
|
||||||
@@ -308,6 +280,17 @@ app.use((req, res)=>{
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Error Handling Middleware
|
||||||
|
app.use((err, req, res, next)=>{
|
||||||
|
const { statusCode, message } = err;
|
||||||
|
console.log('CUSTOM ERROR HANDLER');
|
||||||
|
console.error(err);
|
||||||
|
res.status(statusCode).json({
|
||||||
|
status : 'error',
|
||||||
|
statusCode,
|
||||||
|
message
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const PORT = process.env.PORT || config.get('web_port') || 8000;
|
const PORT = process.env.PORT || config.get('web_port') || 8000;
|
||||||
app.listen(PORT);
|
app.listen(PORT);
|
||||||
|
|||||||
@@ -240,6 +240,7 @@ GoogleActions = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
readFileMetadata : async (auth, id, accessId, accessType)=>{
|
readFileMetadata : async (auth, id, accessId, accessType)=>{
|
||||||
|
|
||||||
const drive = google.drive({ version: 'v3', auth: auth });
|
const drive = google.drive({ version: 'v3', auth: auth });
|
||||||
|
|
||||||
const obj = await drive.files.get({
|
const obj = await drive.files.get({
|
||||||
@@ -248,7 +249,7 @@ GoogleActions = {
|
|||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.log('Error loading from Google');
|
console.log('Error loading from Google');
|
||||||
console.error(err);
|
throw (err);
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -344,7 +345,10 @@ GoogleActions = {
|
|||||||
increaseView : async (id, accessId, accessType, brew)=>{
|
increaseView : async (id, accessId, accessType, brew)=>{
|
||||||
//service account because this is modifying another user's file properties
|
//service account because this is modifying another user's file properties
|
||||||
//so we need extended scope
|
//so we need extended scope
|
||||||
const keys = JSON.parse(config.get('service_account'));
|
const keys = typeof(config.get('service_account')) == 'string' ?
|
||||||
|
JSON.parse(config.get('service_account')) :
|
||||||
|
config.get('service_account');
|
||||||
|
|
||||||
const auth = google.auth.fromJSON(keys);
|
const auth = google.auth.fromJSON(keys);
|
||||||
auth.scopes = ['https://www.googleapis.com/auth/drive'];
|
auth.scopes = ['https://www.googleapis.com/auth/drive'];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user