From 2ef43a68723b0316ed12346478ec530e9f80fb14 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 3 Nov 2018 13:00:56 -0400 Subject: [PATCH] Refactored the admin page to be hosted under /admin again. --- admin.sh | 7 -- .../posts/programming/sorting-algorithms.md | 104 ++++++++++++++++ includes/includes.js | 4 - includes/staticContentServer.js | 3 +- server.js | 18 ++- sites/admin.js | 113 ++++++------------ sites/projects.js | 1 - 7 files changed, 156 insertions(+), 94 deletions(-) delete mode 100644 admin.sh diff --git a/admin.sh b/admin.sh deleted file mode 100644 index ca5a09c..0000000 --- a/admin.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -# -# Runs the admin nodejs server for jrtechs.net -# -# 8/18/18 Jeffery Russell - -nodejs admin.js \ No newline at end of file diff --git a/blogContent/posts/programming/sorting-algorithms.md b/blogContent/posts/programming/sorting-algorithms.md index e69de29..2cdb91c 100644 --- a/blogContent/posts/programming/sorting-algorithms.md +++ b/blogContent/posts/programming/sorting-algorithms.md @@ -0,0 +1,104 @@ + +# Insertion Sort + + +# Heap Sort + + +# Merge Sort + + +#Quick Sort + + +## Memory Greedy Solution + +``` +def quickSortNormal(data): + """ + This is the traditional implementation of quick sort + where there are two recursive calls. + """ + if len(data) == 0: + return [] + else: + less, equal, greater = partition(data) + return quickSortNormal(less) + equal + quickSortNormal(greater) +``` + + +## Accumulation Solution + +``` +def quick_sort_accumulation(data, a): + """ + Implementation of quickSort which forces tail recursion + by wrapping the second recursive in the tail positioned + recursive call and added an accumulation variable. + """ + if len(data) == 0: + return a + less, equal, greater = partition(data) + return quick_sort_accumulation(less, + equal + quick_sort_accumulation(greater, a)) + + +def quicksort(data): + """ + Wrapper function for quick sort accumulation. + """ + return quick_sort_accumulation(data, []) +``` + + +## In-Place Sorting Implementation + +``` +def iterative_partition(data, left, right): + """ + Function which partitions the data into two segments, + the left which is less than the pivot and the right + which is greater than the pivot. The pivot for this + algo is the right most index. This function returns + the ending index of the pivot. + + :param data: array to be sorted + :param left: left most portion of array to look at + :param right: right most portion of the array to look at + """ + x = data[right] + i = left - 1 + j = left + while j < right: + if data[j] <= x: + i = i + 1 + data[i], data[j] = data[j], data[i] + j = j+1 + data[i + 1], data[right] = data[right], data[i + 1] + return i + 1 + + +def iterative_quick_sort(data): + """ + In place implementation of quick sort + + Wrapper function for iterative_quick_sort_helper which + initializes, left, right to be the extrema of the array. + """ + iterative_quick_sort_helper(data, 0, len(data) -1) + return data + + +def iterative_quick_sort_helper(data, left, right): + """ + Uses the divide and conquer algo to sort an array + + :param data: array of data + :param left: left index bound for sorting + :param right: right bound for sorting + """ + if left < right: + pivot = iterative_partition(data, left, right) + iterative_quick_sort_helper(data, left, pivot -1) + iterative_quick_sort_helper(data, pivot+1, right) +``` \ No newline at end of file diff --git a/includes/includes.js b/includes/includes.js index c660bd3..cb816d7 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -49,7 +49,6 @@ const sendCachedContent = function(path, type, result) 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'}); result.write(content); - console.log(content); result.end(); cache.put(path, content); }).catch(function(error) @@ -124,7 +123,6 @@ module.exports = */ sendImage: function(result, fileName) { - console.log(fileName); sendCachedContent(fileName, 'image/png', result); }, @@ -149,8 +147,6 @@ module.exports = { utils.include("." + fileName).then(function(content) { - console.log(fileName); - console.log(content); result.writeHead(200, {'Content-Type': 'text/html'}); result.write(content); result.end(); diff --git a/includes/staticContentServer.js b/includes/staticContentServer.js index 407fe7c..5d65e01 100644 --- a/includes/staticContentServer.js +++ b/includes/staticContentServer.js @@ -23,7 +23,6 @@ module.exports= else if (filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico")) { - console.log("Da fuck"); includes.sendImage(result, baseURL + filename); return true; } @@ -35,7 +34,7 @@ module.exports= return true; } //scripts - else if (filename.includes("/js/") || filename.includes(".js")) + else if (filename.includes(".js")) { includes.sendJS(result, baseURL + filename); return true; diff --git a/server.js b/server.js index 7d0ceb5..3c154b4 100644 --- a/server.js +++ b/server.js @@ -23,7 +23,8 @@ const sql = require('./utils/sql'); //Used for gzip compression const compression = require('compression'); - +//used for file io +const utils = require('./utils/utils.js'); //Updates the site map whenever the server is started const map = require('./utils/generateSiteMap.js'); @@ -33,6 +34,13 @@ map.main(); //port for the server to run on const port = 8000; +//session data for login +const session = require('express-session'); + +//Initializes sessions for login +app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); + + const projects = ["/steam/"]; /** @@ -46,7 +54,6 @@ app.use(function(request, result) { const filename = url.parse(request.url, true).pathname; - console.log("main main" + filename) var project = false; projects.forEach(function(projectName) { @@ -56,6 +63,13 @@ app.use(function(request, result) 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); diff --git a/sites/admin.js b/sites/admin.js index a20c9d5..50ce73b 100644 --- a/sites/admin.js +++ b/sites/admin.js @@ -1,91 +1,48 @@ -/** - * Main server file for the blog. This file is responsible for - * creating the server and listening for clients. The main run - * function parses the url and calls a sub module to make the - * appropriate pages. - * - * @author Jeffery Russell 7-21-18 - */ - -//http server -const http = require('http'); - -//parsing request url -const url = require('url'); - -//express app -const express = require("express"); -const app = express(); - -//session data for login -const session = require('express-session'); - //sending static content const includes = require('../includes/includes.js'); -//used for file io -const utils = require('../utils/utils.js'); - -//cache -- only used for static contents -const cache = require('memory-cache'); - -//Initializes sessions for login -app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); - -//port to listen for the admin server on -const port = 8001; +//used to append static content to result +const contentLoader = require('../includes/staticContentServer.js'); /** - * Parses the request url and calls correct JS files + * @author Jeffery Russell 11-3-18 + * + * @type {{main: (function(*=, *): Promise)}} */ -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")) +module.exports= { - const filename = url.parse(request.url, true).pathname; - - //handles image requests - if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) + /** + * Calls posts and sidebar modules to render blog contents in order + * + * @param requestURL + * @returns {Promise|*} + */ + main: function(request, result, filename) { - includes.sendJS(result, filename, cache); - } - else if(filename.includes("/css/") || filename.includes(".woff2")) - { - includes.sendCSS(result, filename, cache) - } - else if(filename.includes("/js/") || filename.includes(".js")) - { - includes.sendJS(result, filename, cache); - } - else - { - result.writeHead(200, {'Content-Type': 'text/html'}); + if(contentLoader.serveStaticContent(request, result, filename, "")) + { + //do nothing + } + else + { + result.writeHead(200, {'Content-Type': 'text/html'}); - const file = "./admin/admin.js"; + const file = "../admin/admin.js"; - Promise.all([includes.printAdminHeader(), - require(file).main(request), - includes.printFooter()]).then(function(content) - { - result.write(content.join('')); - result.end(); + Promise.all([includes.printAdminHeader(), + require(file).main(request), + includes.printFooter()]).then(function(content) + { + result.write(content.join('')); + result.end(); - }).catch(function(err) - { - console.log(err); - throw err; - }); - } - } - else - { - // utils.printWrongHost(result); - result.writeHead(418, {}); - result.end(); - } -}); + }).catch(function(err) + { + console.log(err); + throw err; + }); + } -http.createServer(app).listen(port); \ No newline at end of file + } + }; \ No newline at end of file diff --git a/sites/projects.js b/sites/projects.js index 0a10dcb..ad8ea55 100644 --- a/sites/projects.js +++ b/sites/projects.js @@ -31,7 +31,6 @@ module.exports= { filename = baseURL + "index.html"; } - console.log("main " + filename) if (!contentLoader.serveStaticContent(request, result, filename, "/blogContent/projects"))