mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-31 13:02:38 +00:00
Cleaning up the server file
This commit is contained in:
72
server/admin.api.js
Normal file
72
server/admin.api.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const _ = require('lodash');
|
||||
const auth = require('basic-auth');
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
const router = require('express').Router();
|
||||
const vitreumRender = require('vitreum/render');
|
||||
|
||||
|
||||
const mw = {
|
||||
adminOnly : (req, res, next)=>{
|
||||
if(req.query && req.query.admin_key == process.env.ADMIN_KEY) return next();
|
||||
return res.status(401).send('Access denied');
|
||||
}
|
||||
};
|
||||
|
||||
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
||||
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password';
|
||||
process.env.ADMIN_KEY = process.env.ADMIN_KEY || 'admin_key';
|
||||
|
||||
|
||||
|
||||
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
|
||||
router.get('/api/invalid', mw.adminOnly, (req, res)=>{
|
||||
const invalidBrewQuery = HomebrewModel.find({
|
||||
'$where' : "this.text.length < 140",
|
||||
createdAt: {
|
||||
$lt: Moment().subtract(3, 'days').toDate()
|
||||
}
|
||||
});
|
||||
|
||||
if(req.query.do_it){
|
||||
invalidBrewQuery.remove().exec((err, objs)=>{
|
||||
refreshCount();
|
||||
return res.send(200);
|
||||
})
|
||||
}else{
|
||||
invalidBrewQuery.exec((err, objs)=>{
|
||||
if(err) console.log(err);
|
||||
return res.json({
|
||||
count : objs.length
|
||||
})
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
//Admin route
|
||||
|
||||
|
||||
router.get('/admin', function(req, res){
|
||||
const credentials = auth(req)
|
||||
if (!credentials || credentials.name !== process.env.ADMIN_USER || credentials.pass !== process.env.ADMIN_PASS) {
|
||||
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
|
||||
return res.status(401).send('Access denied')
|
||||
}
|
||||
vitreumRender({
|
||||
page: './build/admin/bundle.dot',
|
||||
prerenderWith : './client/admin/admin.jsx',
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
admin_key : process.env.ADMIN_KEY,
|
||||
},
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,36 +1,33 @@
|
||||
var _ = require('lodash');
|
||||
var Moment = require('moment');
|
||||
var HomebrewModel = require('./homebrew.model.js').model;
|
||||
const _ = require('lodash');
|
||||
const Moment = require('moment');
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
const router = require('express').Router();
|
||||
|
||||
var homebrewTotal = 0;
|
||||
var refreshCount = function(){
|
||||
HomebrewModel.count({}, function(err, total){
|
||||
|
||||
|
||||
//TODO: Possiblity remove
|
||||
let homebrewTotal = 0;
|
||||
const refreshCount = ()=>{
|
||||
HomebrewModel.count({}, (err, total)=>{
|
||||
homebrewTotal = total;
|
||||
});
|
||||
};
|
||||
refreshCount()
|
||||
|
||||
var mw = {
|
||||
adminOnly : function(req, res, next){
|
||||
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
|
||||
next();
|
||||
}else{
|
||||
return res.status(401).send('Access denied');
|
||||
}
|
||||
}
|
||||
};
|
||||
refreshCount();
|
||||
|
||||
|
||||
var getTopBrews = function(cb){
|
||||
|
||||
|
||||
|
||||
const getTopBrews = (cb)=>{
|
||||
HomebrewModel.find().sort({views: -1}).limit(5).exec(function(err, brews) {
|
||||
cb(brews);
|
||||
});
|
||||
}
|
||||
|
||||
var getGoodBrewTitle = (text) => {
|
||||
var titlePos = text.indexOf('# ');
|
||||
const getGoodBrewTitle = (text) => {
|
||||
const titlePos = text.indexOf('# ');
|
||||
if(titlePos !== -1){
|
||||
var ending = text.indexOf('\n', titlePos);
|
||||
const ending = text.indexOf('\n', titlePos);
|
||||
return text.substring(titlePos + 2, ending);
|
||||
}else{
|
||||
return _.find(text.split('\n'), (line)=>{
|
||||
@@ -40,70 +37,58 @@ var getGoodBrewTitle = (text) => {
|
||||
};
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
app.post('/api', function(req, res){
|
||||
var newHomebrew = new HomebrewModel(req.body);
|
||||
if(!newHomebrew.title){
|
||||
newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
|
||||
router.post('/api', (req, res)=>{
|
||||
const newHomebrew = new HomebrewModel(req.body);
|
||||
if(!newHomebrew.title){
|
||||
newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
|
||||
}
|
||||
newHomebrew.save((err, obj)=>{
|
||||
if(err){
|
||||
console.error(err, err.toString(), err.stack);
|
||||
return res.status(500).send(`Error while creating new brew, ${err.toString()}`);
|
||||
}
|
||||
newHomebrew.save(function(err, obj){
|
||||
if(err){
|
||||
console.error(err, err.toString(), err.stack);
|
||||
return res.status(500).send(`Error while creating new brew, ${err.toString()}`);
|
||||
}
|
||||
return res.json(obj);
|
||||
return res.json(obj);
|
||||
})
|
||||
});
|
||||
|
||||
router.put('/api/update/:id', (req, res)=>{
|
||||
HomebrewModel.find({editId : req.params.id}, (err, objs)=>{
|
||||
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
|
||||
var resEntry = objs[0];
|
||||
resEntry.text = req.body.text;
|
||||
resEntry.title = req.body.title;
|
||||
resEntry.updatedAt = new Date();
|
||||
resEntry.save((err, obj)=>{
|
||||
if(err) return res.status(500).send("Error while saving");
|
||||
return res.status(200).send(obj);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.put('/api/update/:id', function(req, res){
|
||||
HomebrewModel.find({editId : req.params.id}, function(err, objs){
|
||||
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
|
||||
var resEntry = objs[0];
|
||||
resEntry.text = req.body.text;
|
||||
resEntry.title = req.body.title;
|
||||
resEntry.updatedAt = new Date();
|
||||
resEntry.save(function(err, obj){
|
||||
if(err) return res.status(500).send("Error while saving");
|
||||
return res.status(200).send(obj);
|
||||
})
|
||||
});
|
||||
router.get('/api/remove/:id', (req, res)=>{
|
||||
HomebrewModel.find({editId : req.params.id}, (err, objs)=>{
|
||||
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
|
||||
var resEntry = objs[0];
|
||||
resEntry.remove((err)=>{
|
||||
if(err) return res.status(500).send("Error while removing");
|
||||
return res.status(200).send();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/remove/:id', function(req, res){
|
||||
HomebrewModel.find({editId : req.params.id}, function(err, objs){
|
||||
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
|
||||
var resEntry = objs[0];
|
||||
resEntry.remove(function(err){
|
||||
if(err) return res.status(500).send("Error while removing");
|
||||
return res.status(200).send();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
|
||||
app.get('/api/invalid', mw.adminOnly, function(req, res){
|
||||
var invalidBrewQuery = HomebrewModel.find({
|
||||
'$where' : "this.text.length < 140",
|
||||
createdAt: {
|
||||
$lt: Moment().subtract(3, 'days').toDate()
|
||||
}
|
||||
});
|
||||
module.exports = router;
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
app;
|
||||
|
||||
|
||||
if(req.query.do_it){
|
||||
invalidBrewQuery.remove().exec((err, objs)=>{
|
||||
refreshCount();
|
||||
return res.send(200);
|
||||
})
|
||||
}else{
|
||||
invalidBrewQuery.exec((err, objs)=>{
|
||||
if(err) console.log(err);
|
||||
return res.json({
|
||||
count : objs.length
|
||||
})
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.get('/api/search', mw.adminOnly, function(req, res){
|
||||
@@ -143,4 +128,5 @@ module.exports = function(app){
|
||||
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user