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.

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