Browse Source

Got the category pages to work under the routes system.

pull/61/head
jrtechs 5 years ago
parent
commit
4ed7ad10da
14 changed files with 293 additions and 258 deletions
  1. +1
    -1
      admin/adminHome.js
  2. +11
    -19
      blog/category.js
  3. +2
    -2
      routes/admin/analytics.js
  4. +3
    -3
      routes/admin/downloads.js
  5. +4
    -4
      routes/admin/index.js
  6. +3
    -4
      routes/admin/login.js
  7. +3
    -3
      routes/admin/posts.js
  8. +3
    -3
      routes/admin/users.js
  9. +4
    -2
      routes/category/index.js
  10. +7
    -0
      routes/index.js
  11. +0
    -44
      sites/admin.js
  12. +49
    -59
      sites/blog.js
  13. +203
    -0
      utils/pageBuilder.js
  14. +0
    -114
      utils/utils.js

+ 1
- 1
admin/adminHome.js View File

@ -96,7 +96,7 @@ const processPost = function(postData)
} }
else if(post.clear_cache) else if(post.clear_cache)
{ {
require("../sites/blog.js").clearCache();
require("../utils/pageBuilder").clearCache();
require("../includes/includes.js").clearCache(); require("../includes/includes.js").clearCache();
resolve(); resolve();
} }

+ 11
- 19
blog/category.js View File

@ -16,34 +16,26 @@ module.exports=
*/ */
main: function(requestURL, request, templateContext) main: function(requestURL, request, templateContext)
{ {
console.log(requestURL);
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
var page = request.query.page; var page = request.query.page;
const splitURL = requestURL.split("/");
if(splitURL.length >= 3)
{
const category = requestURL.split("/").join("");
sql.getPostsFromCategory(splitURL[2]).then(function(posts)
{
Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext),
require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function()
{
resolve();
});
}).catch(function()
sql.getPostsFromCategory(category).then(function(posts)
{
Promise.all([blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext),
require('./renderNextBar').main(requestURL, page, 5, posts.length, templateContext)]).then(function()
{ {
delete templateContext["posts"];
resolve(); resolve();
}); });
}
else
}).catch(function()
{ {
//page is not found but, posts list will be empty
// so 404 will display
resolve();
}
delete templateContext["posts"];
reject();
});
}); });
} }
}; };

+ 2
- 2
routes/admin/analytics.js View File

@ -1,10 +1,10 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/analytics").main)
builder.constructAdminPage(request, result, require("../../admin/analytics").main)
}); });
module.exports = routes; module.exports = routes;

+ 3
- 3
routes/admin/downloads.js View File

@ -1,15 +1,15 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/adminDownloads").main)
builder.constructAdminPage(request, result, require("../../admin/adminDownloads").main)
}); });
routes.post('/', (request, result) => routes.post('/', (request, result) =>
{ {
utils.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function()
builder.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function()
{ {
result.redirect('/admin/downloads'); result.redirect('/admin/downloads');
}) })

+ 4
- 4
routes/admin/index.js View File

@ -15,16 +15,16 @@ routes.use('/downloads', downloads);
const users = require('./users'); const users = require('./users');
routes.use('/users', users); routes.use('/users', users);
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/adminHome").main)
builder.constructAdminPage(request, result, require("../../admin/adminHome").main)
}); });
routes.post('/', (request, result) => routes.post('/', (request, result) =>
{ {
utils.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function()
builder.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function()
{ {
result.redirect("/admin"); result.redirect("/admin");
}); });
@ -32,7 +32,7 @@ routes.post('/', (request, result) =>
routes.get('*', (request, result) => routes.get('*', (request, result) =>
{ {
utils.print404(result);
builder.print404(result);
}); });
module.exports = routes; module.exports = routes;

+ 3
- 4
routes/admin/login.js View File

@ -1,17 +1,16 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/adminHome").main)
builder.constructAdminPage(request, result, require("../../admin/adminHome").main)
}); });
routes.post('/', (request, result) => routes.post('/', (request, result) =>
{ {
console.log("login request");
utils.constructAdminPage(request, result, require("../../admin/login").main)
builder.constructAdminPage(request, result, require("../../admin/login").main)
}); });
module.exports = routes; module.exports = routes;

+ 3
- 3
routes/admin/posts.js View File

@ -1,15 +1,15 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/posts").main)
builder.constructAdminPage(request, result, require("../../admin/posts").main)
}); });
routes.post('/', (request, result) => routes.post('/', (request, result) =>
{ {
utils.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData)
builder.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData)
}); });
module.exports = routes; module.exports = routes;

+ 3
- 3
routes/admin/users.js View File

@ -1,15 +1,15 @@
const routes = require('express').Router(); const routes = require('express').Router();
const utils = require('../../utils/utils.js');
const builder = require('../../utils/pageBuilder.js');
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
utils.constructAdminPage(request, result, require("../../admin/users").main)
builder.constructAdminPage(request, result, require("../../admin/users").main)
}); });
routes.post('/', (request, result) => routes.post('/', (request, result) =>
{ {
utils.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData)
builder.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData)
}); });
module.exports = routes; module.exports = routes;

+ 4
- 2
routes/category/index.js View File

@ -1,8 +1,10 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.get('/', (request, result) =>
{
const pageBuilder = require('../../utils/pageBuilder');
routes.get('*', (request, result) =>
{
pageBuilder.buildBlogPage(request, result, require("../../blog/category").main)
}); });
module.exports = routes; module.exports = routes;

+ 7
- 0
routes/index.js View File

@ -20,6 +20,13 @@ routes.use('/downloads', downloads);
const includes = require('./includes'); const includes = require('./includes');
routes.use('/includes', includes); routes.use('/includes', includes);
/** category pages */
const category = require('./category');
routes.use('/category', category);
const projects = ["/steam/"]; const projects = ["/steam/"];

+ 0
- 44
sites/admin.js View File

@ -1,44 +0,0 @@
//sending static content
const includes = require('../includes/includes.js');
const whiskers = require('whiskers');
/**
* @author Jeffery Russell 11-3-18
*
* @type {{main: (function(*=, *): Promise)}}
*/
module.exports=
{
/**
* Calls blog and sidebar modules to render blog contents in order
*
* @param requestURL
* @returns {Promise|*}
*/
main: function(request, result, filename)
{
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
result.writeHead(200, {'Content-Type': 'text/html'});
const file = "../admin/admin.js";
var templateContext = Object();
Promise.all([includes.fetchTemplate("admin/adminMain.html"),
includes.printAdminHeader(templateContext),
require(file).main(request, clientAddress, templateContext, filename),
includes.printFooter(templateContext),
]).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
}
};

+ 49
- 59
sites/blog.js View File

@ -34,77 +34,67 @@ module.exports=
*/ */
main: function(request, result, filename) main: function(request, result, filename)
{ {
if(contentLoader.serveStaticContent(request, result, filename, ""))
{
//do nothing
}
//downloads
else if (filename.includes("/downloads/"))
{
require("../includes/downloads.js").main(result, filename);
}
else
{
var page = request.query.page;
if(typeof page == "undefined")
page = 1;
page = Number(page);
const html = cache.get(filename + "?page=" + page);
var page = request.query.page;
if(typeof page == "undefined")
page = 1;
page = Number(page);
const html = cache.get(filename + "?page=" + page);
result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null)
result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null)
{
var file = "";
if (filename === '' || filename === '/')
{ {
var file = "";
file = "../blog/homePage.js";
}
else
{
var urlSplit = filename.split("/");
if (filename === '' || filename === '/')
{
file = "../blog/homePage.js";
}
if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "../blog/category.js";
else else
{ {
var urlSplit = filename.split("/");
if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "../blog/category.js";
else
{
file = "../blog/posts.js";
page = 1; // all blog are single page, everyone must be one to ensure
// cache is not tricked into storing same blog post a ton of times
}
file = "../blog/posts.js";
page = 1; // all blog are single page, everyone must be one to ensure
// cache is not tricked into storing same blog post a ton of times
} }
var templateContext = Object();
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
includes.printHeader(templateContext),
includes.printFooter(templateContext),
require(file).main(filename, request, templateContext),
require("../blog/sidebar.js").main(templateContext)])
.then(function (content)
{
const html = whiskers.render(content[0], templateContext);
result.write(html);
result.end();
cache.put(filename + "?page=" + page, html);
}).catch(function (err)
{
console.log(err);
cache.del(filename + "?page=" + page);
utils.print404().then(function(content)
{
result.write(content);
result.end();
})
});
} }
else
var templateContext = Object();
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
includes.printHeader(templateContext),
includes.printFooter(templateContext),
require(file).main(filename, request, templateContext),
require("../blog/sidebar.js").main(templateContext)])
.then(function (content)
{ {
const html = whiskers.render(content[0], templateContext);
result.write(html); result.write(html);
result.end(); result.end();
}
cache.put(filename + "?page=" + page, html);
}).catch(function (err)
{
console.log(err);
cache.del(filename + "?page=" + page);
utils.print404().then(function(content)
{
result.write(content);
result.end();
})
});
} }
else
{
result.write(html);
result.end();
}
}, },
/** /**

+ 203
- 0
utils/pageBuilder.js View File

@ -0,0 +1,203 @@
const utils = require('./utils');
const whiskers = require('whiskers');
const includes = require("../includes/includes");
const cache = require('memory-cache');
/** used to parse the request URL */
const url = require('url');
const TEMPLATE_FILE="blog/blogMain.html";
const PAGINATION_TEMPLATE_KEY = "paginationTemplate";
const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html";
module.exports =
{
/**
* Displays 404 error to user
*
* @param result
* @returns {*}
*/
print404: function(result)
{
includes.include("includes/html/404.html").then(function(html)
{
result.write(html);
result.end();
})
},
constructAdminPage: function(request, result, templateFiller)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if(module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress,templateContext));
}
Promise.all(promises).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
},
adminPostPageWithOutput: function(request, result, templateFiller)
{
utils.getPostData(request).then(function(postData)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if (module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext, postData));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress, templateContext));
}
Promise.all(promises).then(function (content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function (err) {
console.log(err);
throw err;
});
});
},
adminPostRoute: function(request, templateFiller)
{
return new Promise(function(resolve, reject)
{
utils.getPostData(request).then(function(postData)
{
templateFiller(postData).then(function()
{
resolve();
})
}).catch(function(e)
{
reject(e);
})
})
},
loggedIn(request)
{
return(request.session && request.session.user);
},
buildBlogPage: function(request, result, templateFiller)
{
var page = request.query.page;
if(typeof page == "undefined")
page = 1;
page = Number(page);
var filename = url.parse(request.url, true).pathname;
const html = cache.get(filename + "?page=" + page);
result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null)
{
// var file = "";
//
// if (filename === '' || filename === '/')
// {
// file = "../blog/homePage.js";
// }
// else
// {
// var urlSplit = filename.split("/");
//
// if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
// file = "../blog/category.js";
// else
// {
// file = "../blog/posts.js";
// page = 1; // all blog are single page, everyone must be one to ensure
// // cache is not tricked into storing same blog post a ton of times
// }
// }
var templateContext = Object();
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
includes.printHeader(templateContext),
includes.printFooter(templateContext),
templateFiller(filename, request, templateContext),
require("../blog/sidebar.js").main(templateContext)])
.then(function (content)
{
console.log(JSON.stringify(content));
const html = whiskers.render(content[0], templateContext);
result.write(html);
result.end();
cache.put(filename + "?page=" + page, html);
}).catch(function (err)
{
console.log("dewie");
console.log(err);
cache.del(filename + "?page=" + page);
module.exports.print404(result)
});
}
else
{
result.write(html);
result.end();
}
},
/**
* Clears the memory cache.
*/
clearCache: function()
{
console.log("Blog cache cleared");
cache.clear();
}
};

+ 0
- 114
utils/utils.js View File

@ -6,11 +6,6 @@
//used for file io //used for file io
const fs = require('fs'); const fs = require('fs');
const whiskers = require('whiskers');
const includes = require("../includes/includes");
module.exports= module.exports=
{ {
@ -106,113 +101,4 @@ module.exports=
} }
}); });
}, },
/**
* Displays 404 error to user
*
* @param result
* @returns {*}
*/
print404: function(result)
{
includes.include("includes/html/404.html").then(function(html)
{
result.write(html);
result.end();
})
},
constructAdminPage: function(request, result, templateFiller)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if(module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress,templateContext));
}
Promise.all(promises).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
},
adminPostPageWithOutput: function(request, result, templateFiller)
{
module.exports.getPostData(request).then(function(postData)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if (module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext, postData));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress, templateContext));
}
Promise.all(promises).then(function (content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function (err) {
console.log(err);
throw err;
});
});
},
adminPostRoute: function(request, templateFiller)
{
return new Promise(function(resolve, reject)
{
module.exports.getPostData(request).then(function(postData)
{
templateFiller(postData).then(function()
{
resolve();
})
}).catch(function(e)
{
reject(e);
})
})
},
loggedIn(request)
{
return(request.session && request.session.user);
}
}; };

Loading…
Cancel
Save