diff --git a/admin/adminDownloads.js b/admin/adminDownloads.js index 08e45d6..e96583f 100644 --- a/admin/adminDownloads.js +++ b/admin/adminDownloads.js @@ -109,21 +109,36 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - addDownloadPostData(postData), - removeDownloads(postData), displayDownloads(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in add downloads.js"); reject(error); }); }); + }, + + processPostData: function(postData) + { + return new Promise(function(resolve, reject) + { + Promise.all([addDownloadPostData(postData), + removeDownloads(postData)]).then(function() + { + resolve(); + }).catch(function(error) + { + console.log("Error in admin downloads"); + reject(error); + }) + }) } }; \ No newline at end of file diff --git a/admin/adminHome.js b/admin/adminHome.js index c1dbdd0..50f565a 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -29,13 +29,19 @@ const processPostAddCategory = function(postData) if(sql.insert(q) != 0) { console.log("category added"); + resolve(); } else { + reject(); console.log("error adding category"); } } - resolve(""); + else + { + resolve(); + } + }); }; @@ -74,7 +80,6 @@ const processPost = function(postData) urls = urls.split(" ").join("-"); urls =urls.toLowerCase(); - var q = "insert into posts (category_id, picture_url, published, name, url) values "; q += "('" + post.add_post_category + "', '" + post.add_post_picture + @@ -83,7 +88,7 @@ const processPost = function(postData) { var map = require('../utils/generateSiteMap'); map.main(); - resolve(""); + resolve(); }).catch(function(error) { reject(error); @@ -91,17 +96,19 @@ const processPost = function(postData) } else if(post.clear_cache) { - require("../sites/blog.js").clearCache(); + require("../utils/pageBuilder").clearCache(); require("../includes/includes.js").clearCache(); + resolve(); } else if(post.git_pull) { const execSync = require('child_process').execSync; - code = execSync('git pull') + code = execSync('git pull'); + resolve(); } else { - resolve(""); + resolve(); } }); }; @@ -116,23 +123,39 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { - console.log("called"); return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - processPostAddCategory(postData), - appendCategoriesToTemplate(templateContext), - processPost(postData)]) + appendCategoriesToTemplate(templateContext)]) .then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add downloads.js"); + console.log("error in add adminHome.js"); reject(error); }); }); + }, + + processPostData: function(postData) + { + return new Promise(function(resolve, reject) + { + Promise.all([processPostAddCategory(postData), + processPost(postData)]) + .then(function() + { + console.log("all resolved"); + resolve(); + }).catch(function(error) + { + console.log("error in add downloads.js"); + reject(error); + }); + }); } }; \ No newline at end of file diff --git a/admin/analytics.js b/admin/analytics.js index 3877d51..727f3e7 100644 --- a/admin/analytics.js +++ b/admin/analytics.js @@ -50,17 +50,17 @@ module.exports= * Fetches context information for the admin blog page and handles post * data sent regarding editing blog. * - * @param postData posted by user * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), generateData(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { console.log("error in add admin blog.js"); diff --git a/admin/posts.js b/admin/posts.js index 019fb14..6871bef 100644 --- a/admin/posts.js +++ b/admin/posts.js @@ -10,6 +10,9 @@ const sql = require('../utils/sql'); const qs = require('querystring'); +const utils = require('../utils/utils'); + + /** * Detects if the post data came from the edit form in blog table or edit post * in the edit post form. @@ -84,7 +87,24 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + fetchPostsInformation(templateContext)]).then(function(template) + { + templateContext.adminPage = template[0]; + resolve(); + }).catch(function(error) + { + console.log("error in add admin posts.js"); + reject(error); + }); + }); + }, + + processPostData(templateContext, postData) { return new Promise(function(resolve, reject) { @@ -92,10 +112,11 @@ module.exports= processPostData(postData, templateContext), fetchPostsInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add admin blog.js"); + console.log("error in add admin posts.js"); reject(error); }); }); diff --git a/admin/users.js b/admin/users.js index b0d1c4c..72b6139 100644 --- a/admin/users.js +++ b/admin/users.js @@ -154,7 +154,24 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + getUserInformation(templateContext)]).then(function(template) + { + templateContext.adminPage = template[0]; + resolve(); + }).catch(function(error) + { + console.log("error in users.js"); + reject(error); + }); + }); + }, + + processPostData: function(templateContext, postData) { return new Promise(function(resolve, reject) { @@ -164,10 +181,11 @@ module.exports= editUserPost(postData, templateContext), getUserInformation(templateContext)]).then(function(template) { - resolve(template[0]); + templateContext.adminPage = template[0]; + resolve(); }).catch(function(error) { - console.log("error in add downloads.js"); + console.log("error in users.js"); reject(error); }); }); diff --git a/blog/category.js b/blog/category.js index a9eb476..80fe24d 100644 --- a/blog/category.js +++ b/blog/category.js @@ -16,34 +16,26 @@ module.exports= */ main: function(requestURL, request, templateContext) { + console.log(requestURL); return new Promise(function(resolve, reject) { 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("/category" + request.url, page, 5, posts.length, templateContext)]).then(function() { - delete templateContext["posts"]; resolve(); }); - } - - else + }).catch(function() { - //page is not found but, posts list will be empty - // so 404 will display - resolve(); - } + delete templateContext["posts"]; + reject(); + }); + }); } }; \ No newline at end of file diff --git a/blog/contact.js b/blog/contact.js index 4527720..c75703b 100644 --- a/blog/contact.js +++ b/blog/contact.js @@ -215,7 +215,6 @@ module.exports = const html = whiskers.render(content[0], templateContext); result.write(html); result.end(); - }).catch(function(err) { console.log(err); diff --git a/blog/renderBlogPost.js b/blog/renderBlogPost.js index 223c8a7..1798c66 100644 --- a/blog/renderBlogPost.js +++ b/blog/renderBlogPost.js @@ -255,4 +255,4 @@ module.exports= }); }); } - } + }; diff --git a/blog/renderNextBar.js b/blog/renderNextBar.js index 4453ae4..6933f7e 100644 --- a/blog/renderNextBar.js +++ b/blog/renderNextBar.js @@ -40,14 +40,16 @@ module.exports= var nextPage = currentPage + 1; var previousPage = currentPage - 1; + + var strippedURL = baseURL.split("?page=")[0]; if (isValidPage(previousPage, postsPerPage, totalPosts)) { - paginationObject.previous = {url: baseURL + "?page=" + previousPage}; + paginationObject.previous = {url: strippedURL + "?page=" + previousPage}; } if (isValidPage(nextPage, postsPerPage, totalPosts)) { - paginationObject.next = {url: baseURL + "?page=" + nextPage}; + paginationObject.next = {url: strippedURL + "?page=" + nextPage}; } var page = 1; @@ -60,7 +62,7 @@ module.exports= } else { - pages.push({number: page, url: baseURL + "?page=" + page}) + pages.push({number: page, url: strippedURL + "?page=" + page}) } page = page + 1; } diff --git a/includes/downloads.js b/includes/downloads.js index 0387160..7bf65a4 100644 --- a/includes/downloads.js +++ b/includes/downloads.js @@ -26,13 +26,10 @@ module.exports= return new Promise(function(resolve, reject) { const urlSplit = requestURL.split("/"); - console.log(urlSplit); - if(urlSplit.length == 3 || urlSplit.length == 4) + if(urlSplit.length == 2 || urlSplit.length == 4) { - console.log(urlSplit[2]); - sql.getDownload(urlSplit[2]).then(function(result) + sql.getDownload(urlSplit[1]).then(function(result) { - console.log(result); if(result.length == 1) { const file = './blogContent/downloads/' + @@ -41,19 +38,13 @@ module.exports= } else { - utils.print404(res).then(function() - { - resolve(); - }) + utils.print404(res); } }); } else { - utils.print404().then(function(content) - { - resolve(content); - }) + utils.print404(res); } }); } diff --git a/includes/includes.js b/includes/includes.js index 68eb7fc..910b2d2 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -7,9 +7,6 @@ * @author Jeffery Russell */ -//used for file IO -const utils = require('../utils/utils.js'); - const HEADER_KEY = "header"; const FOOTER_KEY = "footer"; @@ -30,6 +27,25 @@ const crypto = require('crypto'); //caching program to make the application run faster const cache = require('memory-cache'); +const fs = require('fs'); + + +const readFile = function(filename) +{ + return new Promise(function(resolve, reject) + { + try + { + resolve(fs.readFileSync(filename)); + } + catch (e) + { + console.log(e); + console.log("Could not find " + filename); + return(""); + } + }) +}; /** * Sends a static file to the client in a way which the web browser @@ -46,7 +62,7 @@ const sendCachedContent = function(path, type, result) if(goods == null) { - utils.include("." + path).then(function(content) + readFile(path).then(function(content) { const eTag = crypto.createHash('md5').update(content).digest('hex'); result.writeHead(200, {'Content-Type': type, 'Cache-Control': @@ -83,10 +99,39 @@ module.exports = */ printHeader: function(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); + return module.exports.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); + }, + + + includeInObject: function(key, context, fileName) + { + return new Promise(function(resolve, reject) + { + readFile(fileName).then(function(result) + { + context[key] = result; + resolve(); + }).catch(function(error) + { + context[key] = "File Not Found"; + reject(error); + console.log(error); + }) + }); }, + /** + * A function similar to the include statement in PHP + * This function writes a file to the output + * + * @param fileName the file to append to the result + */ + include: function(fileName) + { + return readFile(fileName); + }, + /** * Appends the footer to the result object * @@ -94,7 +139,7 @@ module.exports = */ printFooter: function(templateContext) { - return utils.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); + return module.exports.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); }, /** @@ -104,7 +149,7 @@ module.exports = */ printAdminHeader(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); + return module.exports.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); }, @@ -114,7 +159,7 @@ module.exports = * @param path * @return {*} */ - sendCSS: function(result, path, cache) + sendCSS: function(result, path) { sendCachedContent(path, 'text/css', result); }, @@ -145,7 +190,7 @@ module.exports = fetchTemplate: function(templateName) { - return utils.include("templates/" + templateName); + return readFile("templates/" + templateName); }, @@ -156,7 +201,7 @@ module.exports = */ sendHTML: function(result, fileName) { - utils.include("." + fileName).then(function(content) + readFile(fileName).then(function(content) { result.writeHead(200, {'Content-Type': 'text/html'}); result.write(content); diff --git a/includes/staticContentServer.js b/includes/staticContentServer.js index bd2e417..03952ae 100644 --- a/includes/staticContentServer.js +++ b/includes/staticContentServer.js @@ -2,7 +2,6 @@ const includes = require('../includes/includes.js'); - /** * @author Jeffery Russell 10-30-18 * @@ -13,7 +12,9 @@ module.exports= serveStaticContent: function(request, result, filename, baseURL) { - fullPath = baseURL + filename; + var fullPath = baseURL + filename; + if(fullPath.charAt(0) === '/') + fullPath = fullPath.substring(1,); if (filename.includes(".svg") || filename.includes(".svg")) { includes.sendSVG(result, fullPath); diff --git a/package.json b/package.json index 802a1d6..32f89e7 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "promise": "^8.0.1", "remarkable": "^1.7.1", "request": "^2.88.0", + "routes": "^2.1.0", "sanitizer": "^0.1.3", "sendmail": "^1.4.1" }, diff --git a/routes/about.js b/routes/about.js new file mode 100644 index 0000000..ff2818c --- /dev/null +++ b/routes/about.js @@ -0,0 +1,16 @@ +const routes = require('express').Router(); + +const utils = require('../utils/utils.js'); + +routes.get('/', (request, result) => +{ + +}); + + +routes.get('*', (request, result) => +{ + utils.print404(result); +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/analytics.js b/routes/admin/analytics.js new file mode 100644 index 0000000..b2882b7 --- /dev/null +++ b/routes/admin/analytics.js @@ -0,0 +1,10 @@ +const routes = require('express').Router(); + +const builder = require('../../utils/pageBuilder'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/analytics").main) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/downloads.js b/routes/admin/downloads.js new file mode 100644 index 0000000..5f335b2 --- /dev/null +++ b/routes/admin/downloads.js @@ -0,0 +1,18 @@ +const routes = require('express').Router(); + +const builder = require('../../utils/pageBuilder'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/adminDownloads").main) +}); + +routes.post('/', (request, result) => +{ + builder.adminPostRoute(request ,require("../../admin/adminDownloads").processPostData).then(function() + { + result.redirect('/admin/downloads'); + }) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/index.js b/routes/admin/index.js new file mode 100644 index 0000000..04bdc89 --- /dev/null +++ b/routes/admin/index.js @@ -0,0 +1,38 @@ +const routes = require('express').Router(); + +const analytics = require('./analytics'); +routes.use('/analytics', analytics); + +const login = require('./login'); +routes.use('/login', login); + +const posts = require('./posts'); +routes.use('/posts', posts); + +const downloads = require('./downloads'); +routes.use('/downloads', downloads); + +const users = require('./users'); +routes.use('/users', users); + +const builder = require('../../utils/pageBuilder.js'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + +routes.post('/', (request, result) => +{ + builder.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function() + { + result.redirect("/admin"); + }); +}); + +routes.get('*', (request, result) => +{ + builder.print404(result); +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/login.js b/routes/admin/login.js new file mode 100644 index 0000000..65324c2 --- /dev/null +++ b/routes/admin/login.js @@ -0,0 +1,16 @@ +const routes = require('express').Router(); + +const builder = require('../../utils/pageBuilder.js'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + + +routes.post('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/login").main) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/posts.js b/routes/admin/posts.js new file mode 100644 index 0000000..7699d5b --- /dev/null +++ b/routes/admin/posts.js @@ -0,0 +1,15 @@ +const routes = require('express').Router(); + +const builder = require('../../utils/pageBuilder'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/posts").main) +}); + +routes.post('/', (request, result) => +{ + builder.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/admin/users.js b/routes/admin/users.js new file mode 100644 index 0000000..c252497 --- /dev/null +++ b/routes/admin/users.js @@ -0,0 +1,15 @@ +const routes = require('express').Router(); + +const builder = require('../../utils/pageBuilder.js'); + +routes.get('/', (request, result) => +{ + builder.constructAdminPage(request, result, require("../../admin/users").main) +}); + +routes.post('/', (request, result) => +{ + builder.adminPostPageWithOutput(request, result ,require("../../admin/users").processPostData) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/category/index.js b/routes/category/index.js new file mode 100644 index 0000000..7339d18 --- /dev/null +++ b/routes/category/index.js @@ -0,0 +1,10 @@ +const routes = require('express').Router(); + +const pageBuilder = require('../../utils/pageBuilder'); + +routes.get('*', (request, result) => +{ + pageBuilder.buildBlogPage(request, result, require("../../blog/category").main) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/contact.js b/routes/contact.js new file mode 100644 index 0000000..bce9656 --- /dev/null +++ b/routes/contact.js @@ -0,0 +1,15 @@ +const routes = require('express').Router(); + +const utils = require('../utils/utils.js'); + +routes.get('/', (request, result) => +{ + require("../blog/contact.js").main(request, result); +}); + +routes.get('*', (request, result) => +{ + utils.print404(result); +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/downloads/index.js b/routes/downloads/index.js new file mode 100644 index 0000000..36bf405 --- /dev/null +++ b/routes/downloads/index.js @@ -0,0 +1,14 @@ +const routes = require('express').Router(); + +/** used to parse the request URL */ +const url = require('url'); + +routes.get('*', (request, result) => +{ + console.log("ping"); + const filename = url.parse(request.url, true).pathname; + require("../../includes/downloads.js").main(result, filename); +}); + + +module.exports = routes; \ No newline at end of file diff --git a/routes/includes/index.js b/routes/includes/index.js new file mode 100644 index 0000000..8503dc6 --- /dev/null +++ b/routes/includes/index.js @@ -0,0 +1,15 @@ +const routes = require('express').Router(); + +const contentLoader = require('../../includes/staticContentServer.js'); + +routes.get('*', (request, result) => +{ + if(!contentLoader.serveStaticContent(request, result, request.originalUrl, "")) + { + + //missing content + } +}); + + +module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..9256e64 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,49 @@ +const routes = require('express').Router(); + +/** about page */ +const about = require('./about'); +routes.use('/about', about); + +/** admin page and all of its sub pages */ +const admin = require('./admin'); +routes.use('/admin', admin); + +/** Contact page */ +const contact = require('./contact'); +routes.use('/contact', contact); + +/** Downloads path for files */ +const downloads = require('./downloads'); +routes.use('/downloads', downloads); + +/** static content like css, js, and images */ +const includes = require('./includes'); +routes.use('/includes', includes); +routes.use('/blogContent', includes); + + +/** category pages */ +const category = require('./category'); +routes.use('/category', category); + +const pageBuilder = require('../utils/pageBuilder'); + +const project = require('./projects'); +routes.use('/steam', project); + + + +//blog home page +routes.get('/', (request, result) => +{ + pageBuilder.buildBlogPage(request, result, require("../blog/homePage").main) +}); + +//regular blog posts +routes.get('*', (request, result) => +{ + pageBuilder.buildBlogPage(request, result, + require("../blog/posts").main, request.url); +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/projects/index.js b/routes/projects/index.js new file mode 100644 index 0000000..456e378 --- /dev/null +++ b/routes/projects/index.js @@ -0,0 +1,30 @@ +const routes = require('express').Router(); + +const url = require('url'); + +const contentLoader = require('../../includes/staticContentServer.js'); + +routes.get('*', (request, result) => +{ + var filename = url.parse(request.url, true).pathname; + var extra = ''; + if(filename === '/') + { + if(request.originalUrl.charAt(request.originalUrl.length -1) !== '/') + { + result.redirect(request.originalUrl + "/"); + } + else + { + extra = "/index.html"; + } + } + + if(!contentLoader.serveStaticContent(request, result, + request.originalUrl + extra, "blogContent/projects")) + { + + } +}); + +module.exports = routes; \ No newline at end of file diff --git a/server.js b/server.js index 9bcac45..c28ba88 100644 --- a/server.js +++ b/server.js @@ -11,25 +11,15 @@ const config = require('./utils/configLoader').getConfig(); /** Port for the server to run on */ const port = config.PORT; -/** http server */ -const http = require('http'); - -/** used to parse the request URL */ -const url = require('url'); - /** express app */ const express = require("express"); /** express app */ const app = express(); -/** server side logging */ -const sql = require('./utils/sql'); - /** Used for gzip compression */ const compression = require('compression'); - /**Updates the site map whenever the server is started */ const map = require('./utils/generateSiteMap.js'); map.main(); @@ -41,63 +31,13 @@ const session = require('express-session'); /**Initializes sessions for login */ app.use(session({ secret: config.SESSION_SECRET, cookie: { maxAge: 6000000 }})); - -const projects = ["/steam/"]; - -/** - * Parses the request url and calls correct JS files - */ -app.use(function(request, result) -{ - //prevents people from pointing their dns at my IP:port for my site - if(request.headers.host.includes("localhost:" + port) || - request.headers.host.includes("jrtechs.net")) - { - const filename = url.parse(request.url, true).pathname; - - var project = false; - projects.forEach(function(projectName) - { - if(filename.startsWith(projectName)) - { - require("./sites/projects.js").main(request, result, projectName); - project = true; - } - }); - - if(filename.startsWith("/admin")) - { - require("./sites/admin.js").main(request, result, filename); - project = true; - } - - if(!project) - { - require("./sites/blog.js").main(request, result, filename); - } - - try - { - const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; - console.log(getClientAddress); - - sql.logTraffic(getClientAddress, filename); - } - catch (e) - { } - } - else - { - // utils.printWrongHost(result); - result.writeHead(418, {}); - result.end(); - } -}); - +const routes = require('./routes'); +app.use('/', routes); //enables gzip compression for the site app.use(compression()); -http.createServer(app).listen(port); \ No newline at end of file +app.listen(port, () => + console.log(`App listening on port ${port}!`) +); \ No newline at end of file diff --git a/sites/admin.js b/sites/admin.js deleted file mode 100644 index bbfa674..0000000 --- a/sites/admin.js +++ /dev/null @@ -1,55 +0,0 @@ -//sending static content -const includes = require('../includes/includes.js'); - -//used to append static content to result -const contentLoader = require('../includes/staticContentServer.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) - { - if(contentLoader.serveStaticContent(request, result, filename, "")) - { - //do nothing - } - else - { - 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; - }); - } - } - }; \ No newline at end of file diff --git a/sites/blog.js b/sites/blog.js deleted file mode 100644 index 1c1b142..0000000 --- a/sites/blog.js +++ /dev/null @@ -1,122 +0,0 @@ -//used to append static content to result -const includes = require('../includes/includes.js'); - -//used to append static content to result -const contentLoader = require('../includes/staticContentServer.js'); - -const whiskers = require('whiskers'); - -const TEMPLATE_FILE="blog/blogMain.html"; - - -const PAGINATION_TEMPLATE_KEY = "paginationTemplate"; -const PAGINATION_TEMPLATE_FILE = "blog/paginationBar.html"; - - -//caching program to make the application run faster -const cache = require('memory-cache'); - -//file io -const utils = require('../utils/utils.js'); - -/** - * @author Jeffery Russell 11-3-18 - * - * @type {{main: (function(*=, *): Promise)}} - */ -module.exports= - { - /** - * Calls posts and sidebar modules to render blog contents in order - * - * @param requestURL - * @returns {Promise|*} - */ - 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 if (filename.includes("/contact")) - { - require("../blog/contact.js").main(request, result); - } - else - { - 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) - { - 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), - 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 - { - result.write(html); - result.end(); - } - } - }, - - /** - * Clears the memory cache. - */ - clearCache: function() - { - console.log("Blog cache cleared"); - cache.clear(); - } - }; \ No newline at end of file diff --git a/sites/projects.js b/sites/projects.js index 366ba47..976511a 100644 --- a/sites/projects.js +++ b/sites/projects.js @@ -29,7 +29,6 @@ module.exports= filename = baseURL + "index.html"; } - if (!contentLoader.serveStaticContent(request, result, filename, "/blogContent/projects")) { console.log(filename); diff --git a/templates/admin/adminMain.html b/templates/admin/adminMain.html index 94bbd8f..82cf0d6 100644 --- a/templates/admin/adminMain.html +++ b/templates/admin/adminMain.html @@ -26,7 +26,7 @@