Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. /**
  2. * Determines what template and controls that will be
  3. * displayed based on the url such as
  4. * /
  5. * /blog
  6. * /downloads
  7. *
  8. * For each controls it calls that "pages" associated javascript file
  9. * which fetches the template, deals with post data and gathers context
  10. * for the template engine.
  11. */
  12. //file IO
  13. const utils = require('../utils/utils.js');
  14. module.exports=
  15. {
  16. /**
  17. *
  18. * @param request -- used to get post data
  19. * @param clientAddress -- used to see if user is banned for login
  20. * @param templateContext -- used by whiskers for information to plug
  21. * in the template
  22. * @param filename -- specific admin page requested
  23. * @returns {Promise} resolves once everything has been added to the template context
  24. */
  25. main: function(request, clientAddress, templateContext, filename)
  26. {
  27. return new Promise(function(resolve, reject)
  28. {
  29. //if logged in
  30. if(request.session && request.session.user)
  31. {
  32. templateContext.loggedIn = true;
  33. utils.getPostData(request).then(function (postData)
  34. {
  35. var page = "./adminHome.js";
  36. if(filename.includes('/downloads'))
  37. {
  38. page = "./adminDownloads.js";
  39. }
  40. else if(filename.includes("/posts"))
  41. {
  42. page = "./posts.js";
  43. }
  44. else if(filename.includes("/users"))
  45. {
  46. page = "./users.js";
  47. }
  48. require(page).main(postData, templateContext).then(function(template)
  49. {
  50. templateContext.adminPage = template;
  51. resolve();
  52. }).catch(function(error)
  53. {
  54. console.log(error);
  55. });
  56. });
  57. }
  58. else
  59. {
  60. require("./login/login.js").main(request, clientAddress, templateContext)
  61. .then(function()
  62. {
  63. resolve();
  64. }).catch(function(err)
  65. {
  66. console.log(err);
  67. reject(err);
  68. })
  69. }
  70. });
  71. }
  72. };