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.

79 lines
2.5 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. else if(filename.includes("/analytics"))
  49. {
  50. page = "./analytics.js"
  51. }
  52. require(page).main(postData, templateContext).then(function(template)
  53. {
  54. templateContext.adminPage = template;
  55. resolve();
  56. }).catch(function(error)
  57. {
  58. console.log(error);
  59. });
  60. });
  61. }
  62. else
  63. {
  64. require("./login.js").main(request, clientAddress, templateContext)
  65. .then(function()
  66. {
  67. resolve();
  68. }).catch(function(err)
  69. {
  70. console.log(err);
  71. reject(err);
  72. })
  73. }
  74. });
  75. }
  76. };