seperated routes to cut down on bloat and make a clear structure for additional sections

This commit is contained in:
Ro 2018-11-11 15:22:01 -05:00
parent 99db0dde71
commit e2b3a6e838
3 changed files with 225 additions and 98 deletions

View file

@ -9,99 +9,86 @@ var MemoryStore = require('memorystore')(session)
var flash = require('connect-flash'); var flash = require('connect-flash');
var theme = "default"; var theme = "default";
var app = express(); var app = express();
// view engine setup // view engine setup
app.set('views', path.join(__dirname, '../themes')); app.set('views', path.join(__dirname, '../themes'));
app.set('view engine', 'pug'); app.set('view engine', 'pug');
//app.use(express.static(__dirname+'/content/data'));
app.use(express.static(__dirname + '../content/folio-images')); app.use(express.static(__dirname + '../content/folio-images'));
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev')); app.use(logger('dev'));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ app.use(bodyParser.urlencoded(
{
extended: false extended: false
})); }));
app.use(cookieParser()); app.use(cookieParser());
//app.use(require('node-compass')({mode: 'expanded', css:'styles', sass: 'styles', project: path.join(__dirname, 'src')}));
app.use(express.static(path.join(__dirname, '../content'))); app.use(express.static(path.join(__dirname, '../content')));
app.use(express.static(path.join(__dirname, '../themes'))); app.use(express.static(path.join(__dirname, '../themes')));
app.use(session(
// Why Do we need this key ? {
/** store: new MemoryStore(
app.use(session({ {
secret: '1KqZ18W8KskE1iSw',
saveUninitialized: false,
resave: false,
cookie:{maxAge:608800000}
}));
**/
app.use(session({
store: new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h checkPeriod: 86400000 // prune expired entries every 24h
}), }),
secret: '1KqZ18W8KskE1iSw', secret: '1KqZ18W8KskE1iSw',
saveUninitialized: false, saveUninitialized: false,
resave: false, resave: false,
cookie: { maxAge: 608800000 } cookie:
{
maxAge: 608800000
}
})) }))
app.use(flash()); app.use(flash());
//sections //sections
var front = require('./routes/front/index')(session); var front = require('./routes/front/index')(session);
var back = require('./routes/back/index'); var back = require('./routes/back/index');
var postDashboard = require('./routes/back/dash_posts');
//api //api
var folioLibrary = require('./api/content/folio'); var folioLibrary = require('./api/content/folio');
var projectLibrary = require('./api/content/project'); var projectLibrary = require('./api/content/project');
var bookmarkLibrary = require('./api/content/bookmarks'); var bookmarkLibrary = require('./api/content/bookmarks');
var postLibrary = require('./api/content/posts'); var postLibrary = require('./api/content/posts');
var mailer = require('./api/content/mailer'); var mailer = require('./api/content/mailer');
// API PATHS // API PATHS
app.use('/api/folio', folioLibrary); app.use('/api/folio', folioLibrary);
app.use('/api/projects', projectLibrary); app.use('/api/projects', projectLibrary);
app.use('/api/bookmarks', bookmarkLibrary); app.use('/api/bookmarks', bookmarkLibrary);
app.use('/api/post', postLibrary); app.use('/api/post', postLibrary);
// PAGES // PAGES
app.use('/', front); app.use('/', front);
app.use('/@/dashboard', back); app.use('/@/dashboard', back);
app.use('/@/dashboard/posts', postDashboard);
//app.use('/mailer', mailer); //app.use('/mailer', mailer);
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function(req, res, next) { app.use(function(req, res, next)
{
var err = new Error('Not Found'); var err = new Error('Not Found');
err.status = 404; err.status = 404;
next(err); next(err);
}); });
// error handlers // error handlers
// development error handler // development error handler
// will print stacktrace // will print stacktrace
if (app.get('env') === 'development') { if (app.get('env') === 'development')
app.use(function(err, req, res, next) { {
app.use(function(err, req, res, next)
{
res.status(err.status || 500); res.status(err.status || 500);
res.render(theme+'/error', { res.render(theme + '/error',
{
message: err.message, message: err.message,
error: err error: err
}); });
}); });
} }
// production error handler // production error handler
// no stacktraces leaked to user // no stacktraces leaked to user
app.use(function(err, req, res, next) { app.use(function(err, req, res, next)
{
res.status(err.status || 500); res.status(err.status || 500);
res.render(theme+'/error', { res.render(theme + '/error',
{
message: err.message, message: err.message,
error: {} error:
{}
}); });
}); });
module.exports = app; module.exports = app;

View file

@ -0,0 +1,114 @@
import DateUtils from '../../tools/utilities/DateUtils';
var express = require('express');
var router = express.Router();
var Models = require('../../models');
var hljs = require('highlight.js/lib/highlight');
var hljs_md = require('highlight.js/lib/languages/markdown');
hljs.registerLanguage('markdown', hljs_md);
const dateUtils = new DateUtils();
//--------------------------
// POSTS
//--------------------------
router.get('/:page?', function (req, res) {
var pageNum = req.params.page;
if (pageNum == "" || pageNum == null) pageNum = 1;
var offset = ((pageNum * 5) - 5);
if (req.session.user) {
Models.Post.findAll({
order: [
['id', 'DESC']
]
}).then(function (posts) {
var count = Math.round(posts.length / 6);
//console.log("COUNT: "+count);
var pageItems = [];
var itemLimit = 6;
var rangeStart = (pageNum * itemLimit) - itemLimit;
for (var i = 0; i < itemLimit; i++) {
try {
if (posts[i + rangeStart].id != null) {
pageItems.push(posts[i + rangeStart]);
}
} catch (e) {
//console.log(e)
}
}
res.render('dash/posts-index', {
title: 'Dashbord | Posts',
mode: 'admin',
items: pageItems,
page_index: pageNum,
page_count: count
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
})
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST ADD DISPLAY
//--------------------------
router.get('/add/new', function (req, res) {
if (req.session.user) {
res.render('dash/post-edit', {
title: 'Make New Post',
mode: 'admin',
date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()),
status:['false', 'false', 'false'],
edit: false
});
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST EDIT DISPLAY
//--------------------------
router.get('/edit/:id', function (req, res) {
if (req.session.user) {
Models.Post.findOne({
where: {
slug: req.params.id
}
}).then(post => {
let featured_img = JSON.parse(post.feature_image);
let featured = 'null';
if (featured_img.length != 0)
featured = featured_img[0].substr(7, featured_img[0].length);
let pretty = hljs.highlight('markdown', post.plaintext).value;
let sexydate
if (post.origin_date == "" || post.origin_date == null)
sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at)
else
sexydate = post.origin_date
res.render('dash/post-edit', {
title: 'Edit Post',
mode: 'admin',
post: post,
date: sexydate,
colored: pretty,
html: post.plaintext,
feature: featured,
status:[String(post.page), String(post.featured), String(post.published)],
edit: true
});
}).then(function (value) {
console.log("VALUE: " + value);
}).catch(function (err) {
console.log(err);
})
} else {
res.redirect('/@/dashboard');
}
});
module.exports = router;

View file

@ -1,4 +1,3 @@
import DateUtils from '../../tools/utilities/DateUtils'; import DateUtils from '../../tools/utilities/DateUtils';
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
@ -6,139 +5,162 @@ var Models = require('../../models');
var hljs = require('highlight.js/lib/highlight'); var hljs = require('highlight.js/lib/highlight');
var hljs_md = require('highlight.js/lib/languages/markdown'); var hljs_md = require('highlight.js/lib/languages/markdown');
hljs.registerLanguage('markdown', hljs_md); hljs.registerLanguage('markdown', hljs_md);
const dateUtils = new DateUtils(); const dateUtils = new DateUtils();
//-------------------------- //--------------------------
// Index // Index
//-------------------------- //--------------------------
router.get('/', function (req, res) { router.get('/', function(req, res)
{
var loggedIn = false var loggedIn = false
if (req.session.user) if (req.session.user) loggedIn = true;
loggedIn = true; Models.Post.sync().then(f =>
{
Models.Post.sync().then(f => { Models.Post.findAll(
Models.Post.findAll({ {
order: [ order: [
['id', 'DESC'] ['id', 'DESC']
], ],
limit: 10 limit: 10
}).then(function (posts) { }).then(function(posts)
res.render('dash/index', { {
res.render('dash/index',
{
title: 'Dashboard', title: 'Dashboard',
user_status: loggedIn, user_status: loggedIn,
items: posts items: posts
}); });
}).then(function (value) { }).then(function(value)
{
//console.log(value); //console.log(value);
}).catch(function (err) { }).catch(function(err)
{
//next(err); //next(err);
}) })
}).catch(err => { }).catch(err =>
{
console.log("ERR: " + err); console.log("ERR: " + err);
}) })
}); });
//-------------------------- //--------------------------
// SETTINGS // SETTINGS
//-------------------------- //--------------------------
router.get('/settings/', function (req, res) { router.get('/settings/', function(req, res)
if (req.session.user) { {
res.render('dash/settings', { if (req.session.user)
{
res.render('dash/settings',
{
title: 'Dashboard | Settings', title: 'Dashboard | Settings',
mode: 'admin' mode: 'admin'
}); });
} else { }
else
{
res.redirect('/@/dashboard'); res.redirect('/@/dashboard');
} }
}); });
//-------------------------- //--------------------------
// POSTS // POSTS
//-------------------------- //--------------------------
router.get('/posts/:page?', function (req, res) { router.get('/posts/:page?', function(req, res)
{
var pageNum = req.params.page; var pageNum = req.params.page;
if (pageNum == "" || pageNum == null) pageNum = 1; if (pageNum == "" || pageNum == null) pageNum = 1;
var offset = ((pageNum * 5) - 5); var offset = ((pageNum * 5) - 5);
if (req.session.user) { if (req.session.user)
Models.Post.findAll({ {
Models.Post.findAll(
{
order: [ order: [
['id', 'DESC'] ['id', 'DESC']
] ]
}).then(function (posts) { }).then(function(posts)
{
var count = Math.round(posts.length / 6); var count = Math.round(posts.length / 6);
//console.log("COUNT: "+count); //console.log("COUNT: "+count);
var pageItems = []; var pageItems = [];
var itemLimit = 6; var itemLimit = 6;
var rangeStart = (pageNum * itemLimit) - itemLimit; var rangeStart = (pageNum * itemLimit) - itemLimit;
for (var i = 0; i < itemLimit; i++) { for (var i = 0; i < itemLimit; i++)
try { {
if (posts[i + rangeStart].id != null) { try
{
if (posts[i + rangeStart].id != null)
{
pageItems.push(posts[i + rangeStart]); pageItems.push(posts[i + rangeStart]);
} }
} catch (e) { }
catch (e)
{
//console.log(e) //console.log(e)
} }
} }
res.render('dash/posts-index', { res.render('dash/posts-index',
{
title: 'Dashbord | Posts', title: 'Dashbord | Posts',
mode: 'admin', mode: 'admin',
items: pageItems, items: pageItems,
page_index: pageNum, page_index: pageNum,
page_count: count page_count: count
}); });
}).then(function (value) { }).then(function(value)
{
//console.log(value); //console.log(value);
}).catch(function (err) { }).catch(function(err)
{
//next(err); //next(err);
}) })
} else { }
else
{
res.redirect('/@/dashboard'); res.redirect('/@/dashboard');
} }
}); });
//-------------------------- //--------------------------
// BLOG POST ADD DISPLAY // BLOG POST ADD DISPLAY
//-------------------------- //--------------------------
router.get('/posts/add/new', function (req, res) { router.get('/posts/add/new', function(req, res)
if (req.session.user) { {
res.render('dash/post-edit', { if (req.session.user)
{
res.render('dash/post-edit',
{
title: 'Make New Post', title: 'Make New Post',
mode: 'admin', mode: 'admin',
date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()), date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()),
status:['false', 'false', 'false'], status: ['false', 'false', 'false'],
edit: false edit: false
}); });
} else { }
else
{
res.redirect('/@/dashboard'); res.redirect('/@/dashboard');
} }
}); });
//-------------------------- //--------------------------
// BLOG POST EDIT DISPLAY // BLOG POST EDIT DISPLAY
//-------------------------- //--------------------------
router.get('/posts/edit/:id', function (req, res) { router.get('/posts/edit/:id', function(req, res)
if (req.session.user) { {
Models.Post.findOne({ if (req.session.user)
where: { {
Models.Post.findOne(
{
where:
{
slug: req.params.id slug: req.params.id
} }
}).then(post => { }).then(post =>
{
let featured_img = JSON.parse(post.feature_image); let featured_img = JSON.parse(post.feature_image);
let featured = 'null'; let featured = 'null';
if (featured_img.length != 0) if (featured_img.length != 0) featured = featured_img[0].substr(7, featured_img[0].length);
featured = featured_img[0].substr(7, featured_img[0].length);
let pretty = hljs.highlight('markdown', post.plaintext).value; let pretty = hljs.highlight('markdown', post.plaintext).value;
let sexydate let sexydate
if (post.origin_date == "" || post.origin_date == null) if (post.origin_date == "" || post.origin_date == null) sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at)
sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at) else sexydate = post.origin_date
else res.render('dash/post-edit',
sexydate = post.origin_date {
res.render('dash/post-edit', {
title: 'Edit Post', title: 'Edit Post',
mode: 'admin', mode: 'admin',
post: post, post: post,
@ -146,15 +168,19 @@ router.get('/posts/edit/:id', function (req, res) {
colored: pretty, colored: pretty,
html: post.plaintext, html: post.plaintext,
feature: featured, feature: featured,
status:[String(post.page), String(post.featured), String(post.published)], status: [String(post.page), String(post.featured), String(post.published)],
edit: true edit: true
}); });
}).then(function (value) { }).then(function(value)
{
console.log("VALUE: " + value); console.log("VALUE: " + value);
}).catch(function (err) { }).catch(function(err)
{
console.log(err); console.log(err);
}) })
} else { }
else
{
res.redirect('/@/dashboard'); res.redirect('/@/dashboard');
} }
}); });