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.

199 lines
6.5 KiB

  1. const utils = require('./utils');
  2. const whiskers = require('whiskers');
  3. const includes = require("../includes/includes");
  4. const cache = require('memory-cache');
  5. /** used to parse the request URL */
  6. const url = require('url');
  7. const TEMPLATE_FILE="blog/blogMain.html";
  8. const PAGINATION_TEMPLATE_KEY = "paginationTemplate";
  9. const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html";
  10. module.exports =
  11. {
  12. /**
  13. * Displays 404 error to user
  14. *
  15. * @param result
  16. * @returns {*}
  17. */
  18. print404: function(result)
  19. {
  20. includes.include("includes/html/404.html").then(function(html)
  21. {
  22. result.write(html);
  23. result.end();
  24. })
  25. },
  26. constructAdminPage: function(request, result, templateFiller)
  27. {
  28. var templateContext = Object();
  29. var promises = [];
  30. promises.push(includes.fetchTemplate("admin/adminMain.html"));
  31. promises.push(includes.printAdminHeader(templateContext));
  32. promises.push(includes.printFooter(templateContext));
  33. if(module.exports.loggedIn(request))
  34. {
  35. templateContext.loggedIn = true;
  36. promises.push(templateFiller(templateContext));
  37. }
  38. else
  39. {
  40. //login
  41. const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  42. || request.connection.remoteAddress;
  43. promises.push(require("../admin/login").main(request, clientAddress,templateContext));
  44. }
  45. Promise.all(promises).then(function(content)
  46. {
  47. result.write(whiskers.render(content[0], templateContext));
  48. result.end();
  49. }).catch(function(err)
  50. {
  51. console.log(err);
  52. throw err;
  53. });
  54. },
  55. adminPostPageWithOutput: function(request, result, templateFiller)
  56. {
  57. utils.getPostData(request).then(function(postData)
  58. {
  59. var templateContext = Object();
  60. var promises = [];
  61. promises.push(includes.fetchTemplate("admin/adminMain.html"));
  62. promises.push(includes.printAdminHeader(templateContext));
  63. promises.push(includes.printFooter(templateContext));
  64. if (module.exports.loggedIn(request))
  65. {
  66. templateContext.loggedIn = true;
  67. promises.push(templateFiller(templateContext, postData));
  68. }
  69. else
  70. {
  71. //login
  72. const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  73. || request.connection.remoteAddress;
  74. promises.push(require("../admin/login").main(request, clientAddress, templateContext));
  75. }
  76. Promise.all(promises).then(function (content)
  77. {
  78. result.write(whiskers.render(content[0], templateContext));
  79. result.end();
  80. }).catch(function (err) {
  81. console.log(err);
  82. throw err;
  83. });
  84. });
  85. },
  86. adminPostRoute: function(request, templateFiller)
  87. {
  88. return new Promise(function(resolve, reject)
  89. {
  90. utils.getPostData(request).then(function(postData)
  91. {
  92. templateFiller(postData).then(function()
  93. {
  94. resolve();
  95. })
  96. }).catch(function(e)
  97. {
  98. reject(e);
  99. })
  100. })
  101. },
  102. loggedIn(request)
  103. {
  104. return(request.session && request.session.user);
  105. },
  106. buildBlogPageWithURL: function(request, result, templateFiller, filename)
  107. {
  108. var page = request.query.page;
  109. if(typeof page == "undefined")
  110. page = 1;
  111. page = Number(page);
  112. const html = cache.get(filename + "?page=" + page);
  113. result.writeHead(200, {'Content-Type': 'text/html'});
  114. if (html == null)
  115. {
  116. var templateContext = Object();
  117. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  118. includes.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
  119. includes.printHeader(templateContext),
  120. includes.printFooter(templateContext),
  121. templateFiller(filename, request, templateContext),
  122. require("../blog/sidebar.js").main(templateContext)])
  123. .then(function (content)
  124. {
  125. const html = whiskers.render(content[0], templateContext);
  126. result.write(html);
  127. result.end();
  128. cache.put(filename + "?page=" + page, html);
  129. }).catch(function (err)
  130. {
  131. cache.del(filename + "?page=" + page);
  132. module.exports.print404(result)
  133. });
  134. }
  135. else
  136. {
  137. result.write(html);
  138. result.end();
  139. }
  140. },
  141. buildBlogPage: function(request, result, templateFiller)
  142. {
  143. var filename = url.parse(request.url, true).pathname;
  144. module.exports.buildBlogPageWithURL(request, result,templateFiller, filename);
  145. },
  146. /**
  147. * Clears the memory cache.
  148. */
  149. clearCache: function()
  150. {
  151. console.log("Blog cache cleared");
  152. cache.clear();
  153. },
  154. buildPageWithTemplate: function(request, result, templateFiller, templateFile)
  155. {
  156. var templateContext = Object();
  157. Promise.all([includes.include("templates/" + templateFile),
  158. includes.printHeader(templateContext),
  159. includes.printFooter(templateContext),
  160. require("../blog/sidebar.js").main(templateContext)])
  161. .then(function (content)
  162. {
  163. const html = whiskers.render(content[0], templateContext);
  164. result.write(html);
  165. result.end();
  166. })
  167. }
  168. };