mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 06:22:43 +00:00
Newlines
This commit is contained in:
@@ -1,133 +1,133 @@
|
||||
var _ = require('lodash');
|
||||
var Moment = require('moment');
|
||||
var HomebrewModel = require('./homebrew.model.js').model;
|
||||
|
||||
var homebrewTotal = 0;
|
||||
var refreshCount = function(){
|
||||
HomebrewModel.count({}, function(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');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var getTopBrews = function(cb){
|
||||
HomebrewModel.find().sort({views: -1}).limit(5).exec(function(err, brews) {
|
||||
cb(brews);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
app.get('/homebrew/top', function(req, res){
|
||||
getTopBrews(function(topBrews){
|
||||
return res.json(topBrews);
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/homebrew/api', function(req, res){
|
||||
var newHomebrew = new HomebrewModel(req.body);
|
||||
newHomebrew.save(function(err, obj){
|
||||
if(err) return;
|
||||
return res.json(obj);
|
||||
})
|
||||
});
|
||||
|
||||
app.put('/homebrew/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);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/homebrew/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('/homebrew/api/invalid', mw.adminOnly, function(req, res){
|
||||
var 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
|
||||
})
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.get('/homebrew/api/search', mw.adminOnly, function(req, res){
|
||||
var page = req.query.page || 0;
|
||||
var count = req.query.count || 20;
|
||||
|
||||
var query = {};
|
||||
if(req.query && req.query.id){
|
||||
query = {
|
||||
"$or" : [{
|
||||
editId : req.query.id
|
||||
},{
|
||||
shareId : req.query.id
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
HomebrewModel.find(query, {
|
||||
text : 0 //omit the text
|
||||
}, {
|
||||
skip: page*count,
|
||||
limit: count*1
|
||||
}, function(err, objs){
|
||||
if(err) console.log(err);
|
||||
return res.json({
|
||||
page : page,
|
||||
count : count,
|
||||
total : homebrewTotal,
|
||||
brews : objs
|
||||
});
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
return app;
|
||||
var _ = require('lodash');
|
||||
var Moment = require('moment');
|
||||
var HomebrewModel = require('./homebrew.model.js').model;
|
||||
|
||||
var homebrewTotal = 0;
|
||||
var refreshCount = function(){
|
||||
HomebrewModel.count({}, function(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');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var getTopBrews = function(cb){
|
||||
HomebrewModel.find().sort({views: -1}).limit(5).exec(function(err, brews) {
|
||||
cb(brews);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
app.get('/homebrew/top', function(req, res){
|
||||
getTopBrews(function(topBrews){
|
||||
return res.json(topBrews);
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/homebrew/api', function(req, res){
|
||||
var newHomebrew = new HomebrewModel(req.body);
|
||||
newHomebrew.save(function(err, obj){
|
||||
if(err) return;
|
||||
return res.json(obj);
|
||||
})
|
||||
});
|
||||
|
||||
app.put('/homebrew/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);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/homebrew/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('/homebrew/api/invalid', mw.adminOnly, function(req, res){
|
||||
var 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
|
||||
})
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.get('/homebrew/api/search', mw.adminOnly, function(req, res){
|
||||
var page = req.query.page || 0;
|
||||
var count = req.query.count || 20;
|
||||
|
||||
var query = {};
|
||||
if(req.query && req.query.id){
|
||||
query = {
|
||||
"$or" : [{
|
||||
editId : req.query.id
|
||||
},{
|
||||
shareId : req.query.id
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
HomebrewModel.find(query, {
|
||||
text : 0 //omit the text
|
||||
}, {
|
||||
skip: page*count,
|
||||
limit: count*1
|
||||
}, function(err, objs){
|
||||
if(err) console.log(err);
|
||||
return res.json({
|
||||
page : page,
|
||||
count : count,
|
||||
total : homebrewTotal,
|
||||
brews : objs
|
||||
});
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -1,24 +1,24 @@
|
||||
var mongoose = require('mongoose');
|
||||
var shortid = require('shortid');
|
||||
var _ = require('lodash');
|
||||
|
||||
var HomebrewSchema = mongoose.Schema({
|
||||
shareId : {type : String, default: shortid.generate, index: { unique: true }},
|
||||
editId : {type : String, default: shortid.generate, index: { unique: true }},
|
||||
title : {type : String, default : ""},
|
||||
text : {type : String, default : ""},
|
||||
|
||||
createdAt : { type: Date, default: Date.now },
|
||||
updatedAt : { type: Date, default: Date.now},
|
||||
lastViewed : { type: Date, default: Date.now},
|
||||
views : {type:Number, default:0}
|
||||
});
|
||||
|
||||
|
||||
|
||||
var Homebrew = mongoose.model('Homebrew', HomebrewSchema);
|
||||
|
||||
module.exports = {
|
||||
schema : HomebrewSchema,
|
||||
model : Homebrew,
|
||||
var mongoose = require('mongoose');
|
||||
var shortid = require('shortid');
|
||||
var _ = require('lodash');
|
||||
|
||||
var HomebrewSchema = mongoose.Schema({
|
||||
shareId : {type : String, default: shortid.generate, index: { unique: true }},
|
||||
editId : {type : String, default: shortid.generate, index: { unique: true }},
|
||||
title : {type : String, default : ""},
|
||||
text : {type : String, default : ""},
|
||||
|
||||
createdAt : { type: Date, default: Date.now },
|
||||
updatedAt : { type: Date, default: Date.now},
|
||||
lastViewed : { type: Date, default: Date.now},
|
||||
views : {type:Number, default:0}
|
||||
});
|
||||
|
||||
|
||||
|
||||
var Homebrew = mongoose.model('Homebrew', HomebrewSchema);
|
||||
|
||||
module.exports = {
|
||||
schema : HomebrewSchema,
|
||||
model : Homebrew,
|
||||
}
|
||||
@@ -1,135 +1,135 @@
|
||||
var _ = require('lodash');
|
||||
var vitreumRender = require('vitreum/render');
|
||||
var HomebrewModel = require('./homebrew.model.js').model;
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
/*
|
||||
app.get('/homebrew/new', function(req, res){
|
||||
var newHomebrew = new HomebrewModel();
|
||||
newHomebrew.save(function(err, obj){
|
||||
return res.redirect('/homebrew/edit/' + obj.editId);
|
||||
})
|
||||
})
|
||||
*/
|
||||
|
||||
|
||||
//Edit Page
|
||||
app.get('/homebrew/edit/:id', function(req, res){
|
||||
HomebrewModel.find({editId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var resObj = null;
|
||||
var errObj = {text: "# oops\nCould not find the homebrew."}
|
||||
if(objs.length){
|
||||
resObj = objs[0];
|
||||
}
|
||||
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
brew : resObj || errObj
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
//Share Page
|
||||
app.get('/homebrew/share/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var resObj = null;
|
||||
var errObj = {text: "# oops\nCould not find the homebrew."}
|
||||
|
||||
if(objs.length){
|
||||
resObj = objs[0];
|
||||
resObj.lastViewed = new Date();
|
||||
resObj.views = resObj.views + 1;
|
||||
resObj.save();
|
||||
}
|
||||
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
brew : resObj || errObj
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
//Print Page
|
||||
var Markdown = require('marked');
|
||||
var PHBStyle = '<style>' + require('fs').readFileSync('./phb.standalone.css', 'utf8') + '</style>'
|
||||
app.get('/homebrew/print/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var brew = null;
|
||||
if(objs.length){
|
||||
brew = objs[0];
|
||||
}
|
||||
|
||||
var content = _.map(brew.text.split('\\page'), function(pageText){
|
||||
return '<div class="phb print">' + Markdown(pageText) + '</div>';
|
||||
}).join('\n');
|
||||
|
||||
var dialog = '';
|
||||
if(req.query && req.query.dialog) dialog = 'onload="window.print()"';
|
||||
|
||||
var title = '<title>' + brew.title + '</title>';
|
||||
var page = `<html><head>${title} ${PHBStyle}</head><body ${dialog}>${content}</body></html>`
|
||||
|
||||
return res.send(page)
|
||||
});
|
||||
});
|
||||
|
||||
//Source page
|
||||
String.prototype.replaceAll = function(s,r){return this.split(s).join(r)}
|
||||
app.get('/homebrew/source/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
var brew = null;
|
||||
if(objs.length) brew = objs[0];
|
||||
var text = brew.text.replaceAll('<', '<').replaceAll('>', '>');
|
||||
return res.send(`<code><pre>${text}</pre></code>`);
|
||||
});
|
||||
});
|
||||
|
||||
//Home and 404, etc.
|
||||
var welcomeText = require('fs').readFileSync('./client/homebrew/pages/homePage/welcome_msg.txt', 'utf8');
|
||||
var changelogText = require('fs').readFileSync('./changelog.md', 'utf8');
|
||||
app.get('/homebrew*', function (req, res) {
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
welcomeText : welcomeText,
|
||||
changelog : changelogText
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
var _ = require('lodash');
|
||||
var vitreumRender = require('vitreum/render');
|
||||
var HomebrewModel = require('./homebrew.model.js').model;
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = function(app){
|
||||
|
||||
/*
|
||||
app.get('/homebrew/new', function(req, res){
|
||||
var newHomebrew = new HomebrewModel();
|
||||
newHomebrew.save(function(err, obj){
|
||||
return res.redirect('/homebrew/edit/' + obj.editId);
|
||||
})
|
||||
})
|
||||
*/
|
||||
|
||||
|
||||
//Edit Page
|
||||
app.get('/homebrew/edit/:id', function(req, res){
|
||||
HomebrewModel.find({editId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var resObj = null;
|
||||
var errObj = {text: "# oops\nCould not find the homebrew."}
|
||||
if(objs.length){
|
||||
resObj = objs[0];
|
||||
}
|
||||
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
brew : resObj || errObj
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
//Share Page
|
||||
app.get('/homebrew/share/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var resObj = null;
|
||||
var errObj = {text: "# oops\nCould not find the homebrew."}
|
||||
|
||||
if(objs.length){
|
||||
resObj = objs[0];
|
||||
resObj.lastViewed = new Date();
|
||||
resObj.views = resObj.views + 1;
|
||||
resObj.save();
|
||||
}
|
||||
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
brew : resObj || errObj
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
//Print Page
|
||||
var Markdown = require('marked');
|
||||
var PHBStyle = '<style>' + require('fs').readFileSync('./phb.standalone.css', 'utf8') + '</style>'
|
||||
app.get('/homebrew/print/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
|
||||
var brew = null;
|
||||
if(objs.length){
|
||||
brew = objs[0];
|
||||
}
|
||||
|
||||
var content = _.map(brew.text.split('\\page'), function(pageText){
|
||||
return '<div class="phb print">' + Markdown(pageText) + '</div>';
|
||||
}).join('\n');
|
||||
|
||||
var dialog = '';
|
||||
if(req.query && req.query.dialog) dialog = 'onload="window.print()"';
|
||||
|
||||
var title = '<title>' + brew.title + '</title>';
|
||||
var page = `<html><head>${title} ${PHBStyle}</head><body ${dialog}>${content}</body></html>`
|
||||
|
||||
return res.send(page)
|
||||
});
|
||||
});
|
||||
|
||||
//Source page
|
||||
String.prototype.replaceAll = function(s,r){return this.split(s).join(r)}
|
||||
app.get('/homebrew/source/:id', function(req, res){
|
||||
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
|
||||
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
|
||||
var brew = null;
|
||||
if(objs.length) brew = objs[0];
|
||||
var text = brew.text.replaceAll('<', '<').replaceAll('>', '>');
|
||||
return res.send(`<code><pre>${text}</pre></code>`);
|
||||
});
|
||||
});
|
||||
|
||||
//Home and 404, etc.
|
||||
var welcomeText = require('fs').readFileSync('./client/homebrew/pages/homePage/welcome_msg.txt', 'utf8');
|
||||
var changelogText = require('fs').readFileSync('./changelog.md', 'utf8');
|
||||
app.get('/homebrew*', function (req, res) {
|
||||
vitreumRender({
|
||||
page: './build/homebrew/bundle.dot',
|
||||
globals:{},
|
||||
prerenderWith : './client/homebrew/homebrew.jsx',
|
||||
initialProps: {
|
||||
url: req.originalUrl,
|
||||
welcomeText : welcomeText,
|
||||
changelog : changelogText
|
||||
},
|
||||
clearRequireCache : !process.env.PRODUCTION,
|
||||
}, function (err, page) {
|
||||
return res.send(page)
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user