breaking routing out from app.js into subfiles.

This commit is contained in:
Andrew Davidson 2016-01-23 16:19:42 -05:00
parent e635ec680d
commit 38184e451f
4 changed files with 311 additions and 304 deletions

307
app.js
View file

@ -22,8 +22,9 @@ Category = require('./category.js');
Static = require('./static.js');
var database = require('./db.js');
var genPhotos = require('./genPhotos.js');
require('./post-routes.js')(app);
require('./photo-routes.js')(app);
require('./routes-post.js')(app);
require('./routes-photo.js')(app);
require('./routes-preview.js')(app);
// Get config variables
var config = require('./config.js').config;
@ -93,168 +94,6 @@ app.get('/logout', function(req, res) {
}
);
// Photo management Routing
app.get('/admin/photo/list/:start?',
function(req, res, next) {
var count = 25;
if (req.params.start) {
var start = req.params.start;
} else {
var start = 0;
}
database.listPhotos(count, start, function(photos){
for (photo in photos) {
var date = new Date(photos[photo].photoDate);
photos[photo].dateString = date.getFullYear() + '-' +
("0" + (date.getMonth()+1)).slice(-2) + '-' +
("0" + date.getDate()).slice(-2);
}
res.render('admin-photo-list', {photos, user: req.user});
});
}
);
app.get('/admin/photo/view/:id?',
function (req, res, next) {
if (req.params.id) {
database.getPhotoById(req.params.id, function(row) {
res.render('admin-photo-view', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
photo: row,
srcPath: '/'+row.path,
user: req.user
})
})
}
else {
res.redirect('/admin/photo/list');
}
}
);
app.get('/admin/photo/new',
function(req, res, next) {
res.render('admin-photo-new', { user: req.user });
}
);
app.post('/admin/photo/new',
upload.single('photo'),
function(req, res, next) {
console.log(req.file);
if ( req.body.title != "") {
var title = req.body.title;
}
else {
var title = req.file.originalname.split('.').slice(0,-1).join(' ');
}
var extension = req.file.originalname.split('.').slice(-1);
if (req.body.photoDate != "") {
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
}
else {
var photoDate = helper.dateToEpoch(new Date());
}
if (req.body.slug != "") {
var slug = req.body.slug;
}
else if ( req.body.title != "" ){
var slug = helper.makeSlug(title+'-'+photoDate);
}
else {
var cleanFileName = req.file.originalname.split('.').slice(0,-1).join('-');
var slug = helper.makeSlug(cleanFileName+'-'+photoDate);
}
var mimetype = req.file.mimetype;
var tags = helper.parseTags(req.body.tags);
var markdown = req.body.markdown;
var updatedDate = photoDate;
var createDate = helper.dateToEpoch(new Date());
var categoryName = req.body.category;
var path = req.file.path;
database.createPhoto(title, slug, markdown, extension, mimetype, photoDate, updatedDate, createDate, path, function(err, row) {
if (err) console.log(err);
console.log(row);
database.tagPhoto(row.id, tags);
req.flash('successNotice', 'Photo created.');
res.redirect('/admin/photo/view/'+row.id);
});
}
);
app.get('/admin/photo/edit/:id',
function(req, res, next) {
var id = req.params.id;
async.parallel({
photoTags: function(callback) {
database.getPhotoTagsAsString(id, function(tagString) {
callback(null, tagString);
})
},
photo: function(callback) {
database.getPhotoById(id, function(photo) {
callback(null, photo);
})
}
},
function(err, results) {
res.render('admin-photo-edit', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
photo: results.photo,
srcPath: '/'+results.photo.path,
photoTags: results.photoTags,
formattedDate: helper.epochToDateString(results.photo.photoDate),
user: req.user
});
});
}
);
app.post('/admin/photo/edit/:id',
function(req, res, next) {
var id = req.params.id;
var title = req.body.title;
var slug = req.body.slug;
var markdown = req.body.markdown;
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
var tags = helper.parseTags(req.body.tags);
console.log('Post '+id+' update request received');
database.tagPhoto(id, tags);
database.updatePhoto(id, title, slug, markdown, photoDate, function(err, row) {
req.flash('successNotice', 'Photo updated.');
res.redirect('/admin/photo/view/'+id);
});
}
);
app.get('/admin/photo/regenerate/:id',
function(req, res, next) {
console.log('Generating resized images for: '+req.params.id);
genStatic.generateStatic(function(err){ if (err) console.log(err) });
genPhotos.generatePhotoSizesById(req.params.id, function(err) {
if (!err) {
genPhotos.generatePhotoPage(req.params.id, function(err) {
if (!err) {
req.flash("successNotice", "Photo regenerated");
res.redirect('/admin/photo/view/'+req.params.id);
}
else {
req.flash('failureNotice', 'Photo was unable to be resized.');
res.redirect('/admin/photo/view/'+req.params.id);
}
})
}
else {
req.flash('failureNotice', 'Photo was unable to be resized.');
res.redirect('/admin/photo/view/'+req.params.id);
}
});
}
);
// Admin dashboard page.
app.get('/admin',
function(req, res, next) {
@ -280,146 +119,6 @@ app.get('/admin',
}
);
// Routes for previewing sent versions of the built items.
app.get('/blog/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/blog/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/photos/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/photos/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/galleries/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/galleries/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/static/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/static/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/'+config.uploadDir+'/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/'+ config.uploadDir + '/'+ req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
// Have to have some sort of home page.
app.get('/', function(req, res, next) {

165
routes-photo.js Normal file
View file

@ -0,0 +1,165 @@
// Routes for photo administration.
module.exports = function(app) {
// Photo management Routing
app.get('/admin/photo/list/:start?',
function(req, res, next) {
var count = 25;
if (req.params.start) {
var start = req.params.start;
} else {
var start = 0;
}
database.listPhotos(count, start, function(photos){
for (photo in photos) {
var date = new Date(photos[photo].photoDate);
photos[photo].dateString = date.getFullYear() + '-' +
("0" + (date.getMonth()+1)).slice(-2) + '-' +
("0" + date.getDate()).slice(-2);
}
res.render('admin-photo-list', {photos, user: req.user});
});
}
);
app.get('/admin/photo/view/:id?',
function (req, res, next) {
if (req.params.id) {
database.getPhotoById(req.params.id, function(row) {
res.render('admin-photo-view', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
photo: row,
srcPath: '/'+row.path,
user: req.user
})
})
}
else {
res.redirect('/admin/photo/list');
}
}
);
app.get('/admin/photo/new',
function(req, res, next) {
res.render('admin-photo-new', { user: req.user });
}
);
app.post('/admin/photo/new',
upload.single('photo'),
function(req, res, next) {
console.log(req.file);
if ( req.body.title != "") {
var title = req.body.title;
}
else {
var title = req.file.originalname.split('.').slice(0,-1).join(' ');
}
var extension = req.file.originalname.split('.').slice(-1);
if (req.body.photoDate != "") {
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
}
else {
var photoDate = helper.dateToEpoch(new Date());
}
if (req.body.slug != "") {
var slug = req.body.slug;
}
else if ( req.body.title != "" ){
var slug = helper.makeSlug(title+'-'+photoDate);
}
else {
var cleanFileName = req.file.originalname.split('.').slice(0,-1).join('-');
var slug = helper.makeSlug(cleanFileName+'-'+photoDate);
}
var mimetype = req.file.mimetype;
var tags = helper.parseTags(req.body.tags);
var markdown = req.body.markdown;
var updatedDate = photoDate;
var createDate = helper.dateToEpoch(new Date());
var categoryName = req.body.category;
var path = req.file.path;
database.createPhoto(title, slug, markdown, extension, mimetype, photoDate, updatedDate, createDate, path, function(err, row) {
if (err) console.log(err);
console.log(row);
database.tagPhoto(row.id, tags);
req.flash('successNotice', 'Photo created.');
res.redirect('/admin/photo/view/'+row.id);
});
}
);
app.get('/admin/photo/edit/:id',
function(req, res, next) {
var id = req.params.id;
async.parallel({
photoTags: function(callback) {
database.getPhotoTagsAsString(id, function(tagString) {
callback(null, tagString);
})
},
photo: function(callback) {
database.getPhotoById(id, function(photo) {
callback(null, photo);
})
}
},
function(err, results) {
res.render('admin-photo-edit', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
photo: results.photo,
srcPath: '/'+results.photo.path,
photoTags: results.photoTags,
formattedDate: helper.epochToDateString(results.photo.photoDate),
user: req.user
});
});
}
);
app.post('/admin/photo/edit/:id',
function(req, res, next) {
var id = req.params.id;
var title = req.body.title;
var slug = req.body.slug;
var markdown = req.body.markdown;
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
var tags = helper.parseTags(req.body.tags);
console.log('Post '+id+' update request received');
database.tagPhoto(id, tags);
database.updatePhoto(id, title, slug, markdown, photoDate, function(err, row) {
req.flash('successNotice', 'Photo updated.');
res.redirect('/admin/photo/view/'+id);
});
}
);
app.get('/admin/photo/regenerate/:id',
function(req, res, next) {
console.log('Generating resized images for: '+req.params.id);
genStatic.generateStatic(function(err){ if (err) console.log(err) });
genPhotos.generatePhotoSizesById(req.params.id, function(err) {
if (!err) {
genPhotos.generatePhotoPage(req.params.id, function(err) {
if (!err) {
req.flash("successNotice", "Photo regenerated");
res.redirect('/admin/photo/view/'+req.params.id);
}
else {
req.flash('failureNotice', 'Photo was unable to be resized.');
res.redirect('/admin/photo/view/'+req.params.id);
}
})
}
else {
req.flash('failureNotice', 'Photo was unable to be resized.');
res.redirect('/admin/photo/view/'+req.params.id);
}
});
}
);
}

143
routes-preview.js Normal file
View file

@ -0,0 +1,143 @@
module.exports = function (app) {
// Routes for previewing sent versions of the built items.
app.get('/blog/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/blog/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/photos/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/photos/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/galleries/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/galleries/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/static/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/' + config.genDir + '/static/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
app.get('/'+config.uploadDir+'/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/'+ config.uploadDir + '/'+ req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
else {
console.log(path + ' does not exist...');
if (path.slice(-1) != '/') path += '/';
path += 'index.html'
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
console.log(path + ' exists serving...');
res.sendFile(path);
}
});
}
});
}
}
);
}