Browse Source

Merge pull request #27 from jrtechs/adminRefactor

Admin Page Refactor
pull/29/head
Jeffery Russell 6 years ago
committed by GitHub
parent
commit
286df044ec
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 392 additions and 259 deletions
  1. +1
    -1
      README.md
  2. +0
    -91
      admin.js
  3. +0
    -7
      admin.sh
  4. +104
    -0
      blogContent/posts/programming/sorting-algorithms.md
  5. +0
    -0
      docs/blogSql.svg
  6. +41
    -6
      includes/includes.js
  7. +0
    -64
      includes/projects.js
  8. +50
    -0
      includes/staticContentServer.js
  9. +24
    -90
      server.js
  10. +48
    -0
      sites/admin.js
  11. +83
    -0
      sites/blog.js
  12. +41
    -0
      sites/projects.js

+ 1
- 1
README.md View File

@ -33,7 +33,7 @@ unless otherwise stated.
## MYSQL Schema ## MYSQL Schema
![](blogSql.svg)
![](docs/blogSql.svg)
```mysql ```mysql
create database jrtechs_blog; create database jrtechs_blog;

+ 0
- 91
admin.js View File

@ -1,91 +0,0 @@
/**
* 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;
/**
* 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;
//handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
{
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'});
const file = "./admin/admin.js";
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();
}
});
http.createServer(app).listen(port);

+ 0
- 7
admin.sh View File

@ -1,7 +0,0 @@
#!/bin/bash
#
# Runs the admin nodejs server for jrtechs.net
#
# 8/18/18 Jeffery Russell
nodejs admin.js

+ 104
- 0
blogContent/posts/programming/sorting-algorithms.md View File

@ -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)
```

blogSql.svg → docs/blogSql.svg View File


+ 41
- 6
includes/includes.js View File

@ -23,6 +23,10 @@ const ADMIN_HEADER = "includes/html/adminHeader.html";
const crypto = require('crypto'); const crypto = require('crypto');
//caching program to make the application run faster
const cache = require('memory-cache');
/** /**
* Sends a static file to the client in a way which the web browser * Sends a static file to the client in a way which the web browser
* caches the contents sent. * caches the contents sent.
@ -32,7 +36,7 @@ const crypto = require('crypto');
* @param type -- type of file for the header * @param type -- type of file for the header
* @param result -- sent to client * @param result -- sent to client
*/ */
const sendCachedContent = function(cache, path, type, result)
const sendCachedContent = function(path, type, result)
{ {
const goods = cache.get(path); const goods = cache.get(path);
@ -108,7 +112,18 @@ module.exports =
*/ */
sendCSS: function(result, path, cache) sendCSS: function(result, path, cache)
{ {
sendCachedContent(cache, "/" + path, 'text/css', result);
sendCachedContent(path, 'text/css', result);
},
/**Sends the user an image from the specified fileName.
*
* @param result
* @param fileName
*/
sendImage: function(result, fileName)
{
sendCachedContent(fileName, 'image/png', result);
}, },
@ -117,9 +132,9 @@ module.exports =
* @param result * @param result
* @param fileName * @param fileName
*/ */
sendImage: function(result, fileName, cache)
sendJS: function(result, fileName)
{ {
sendCachedContent(cache, fileName, 'image/png', result);
sendCachedContent(fileName, 'application/javascript', result);
}, },
@ -128,8 +143,28 @@ module.exports =
* @param result * @param result
* @param fileName * @param fileName
*/ */
sendJS: function(result, fileName, cache)
sendHTML: function(result, fileName)
{
utils.include("." + fileName).then(function(content)
{
result.writeHead(200, {'Content-Type': 'text/html'});
result.write(content);
result.end();
}).catch(function(error)
{
console.log(error);
});
},
/**
* Sends a svg file to the client.
*
* @param result
* @param fileName
*/
sendSVG: function(result, fileName)
{ {
sendCachedContent(cache, fileName, 'application/javascript', result);
sendCachedContent(fileName, 'image/svg+xml', result);
} }
}; };

+ 0
- 64
includes/projects.js View File

@ -1,64 +0,0 @@
//file io
const utils = require('../utils/utils.js');
//used to parse the request URL
const url = require('url');
/**
* @author Jeffery Russell 10-30-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, baseURL)
{
//const filename = url.parse(request.url, true).pathname
var filename = url.parse(request.url, true).pathname;
if(filename.includes(".svg") || filename.includes(".svg"))
{
result.writeHead(200, {'Content-Type': 'image/svg+xml'});
}
else if(filename.includes("/img/") || filename.includes(".jpg") ||
filename.includes(".png") || filename.includes(".ico"))
{
result.writeHead(200, {'Content-Type': 'image/png'});
}
else if(filename.includes("/css/") || filename.includes(".woff2") ||
filename.includes(".txt"))
{
result.writeHead(200, {'Content-Type': 'text/css'});
}
else if(filename.includes("/js/") || filename.includes(".js"))
{
result.writeHead(200, {'Content-Type': 'application/javascript'});
}
else
{
result.writeHead(200, {'Content-Type': 'text/html'});
}
if(filename == baseURL || filename == baseURL.substring(0, baseURL.length - 1))
{
filename = baseURL + "index.html";
}
utils.include("./blogContent/projects" + filename).then(function(content)
{
result.write(content);
result.end();
}).catch(function(error)
{
console.log(error);
});
}
};

+ 50
- 0
includes/staticContentServer.js View File

@ -0,0 +1,50 @@
//used to append static content to result
const includes = require('../includes/includes.js');
/**
* @author Jeffery Russell 10-30-18
*
* @type {{main: (function(*=, *): Promise)}}
*/
module.exports=
{
serveStaticContent: function(request, result, filename, baseURL)
{
console.log(filename);
if (filename.includes(".svg") || filename.includes(".svg"))
{
includes.sendSVG(result, baseURL + filename);
return true;
}
//handles image requests
else if (filename.includes(".jpg") ||
filename.includes(".png") || filename.includes(".ico"))
{
includes.sendImage(result, baseURL + filename);
return true;
}
//css and font files
else if (filename.includes(".woff2") || filename.includes(".css") ||
filename.includes(".txt"))
{
includes.sendCSS(result, baseURL + filename);
return true;
}
//scripts
else if (filename.includes(".js"))
{
includes.sendJS(result, baseURL + filename);
return true;
}
//html
else if (filename.includes(".html"))
{
includes.sendHTML(result, baseURL + filename);
return true;
}
return false;
}
};

+ 24
- 90
server.js View File

@ -17,17 +17,14 @@ const express = require("express");
//express app //express app
const app = express(); const app = express();
//used to append static content to result
const includes = require('./includes/includes.js');
//server side logging //server side logging
const sql = require('./utils/sql'); const sql = require('./utils/sql');
//Used for gzip compression //Used for gzip compression
const compression = require('compression')
const compression = require('compression');
//caching program to make the application run faster
const cache = require('memory-cache');
//used for file io
const utils = require('./utils/utils.js');
//Updates the site map whenever the server is started //Updates the site map whenever the server is started
const map = require('./utils/generateSiteMap.js'); const map = require('./utils/generateSiteMap.js');
@ -37,6 +34,13 @@ map.main();
//port for the server to run on //port for the server to run on
const port = 8000; 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/"]; const projects = ["/steam/"];
/** /**
@ -55,102 +59,32 @@ app.use(function(request, result)
{ {
if(filename.startsWith(projectName)) if(filename.startsWith(projectName))
{ {
require("./includes/projects.js").main(request, result, projectName);
require("./sites/projects.js").main(request, result, projectName);
project = true; project = true;
} }
}); });
if(project)
if(filename.startsWith("/admin"))
{ {
//don't do blog stuff
require("./sites/admin.js").main(request, result, filename);
project = true;
} }
else if(filename.includes(".svg") || filename.includes(".svg"))
{
result.writeHead(200, {'Content-Type': 'image/svg+xml'});
}
//handles image requests
else if(filename.includes("/img/") || filename.includes(".jpg") ||
filename.includes(".png") || filename.includes(".ico"))
{
includes.sendImage(result, filename, cache);
}
//css and font files
else if(filename.includes("/css/") || filename.includes(".woff2") ||
filename.includes(".txt"))
{
includes.sendCSS(result, filename, cache);
}
//scripts
else if(filename.includes("/js/") || filename.includes(".js"))
{
includes.sendJS(result, filename, cache);
}
//downloads
else if(filename.includes("/downloads/"))
{
require("./includes/downloads.js").main(result, filename);
}
else if(filename.includes("/contact"))
if(!project)
{ {
require("./includes/contact.js").main(request, result);
require("./sites/blog.js").main(request, result, filename);
} }
else
{
const html = cache.get(filename);
result.writeHead(200, {'Content-Type': 'text/html'});
if(html == null)
{
var file = "";
if(filename === '' || filename === '/')
{
file="./posts/homePage.js";
}
else
{
var urlSplit = filename.split("/");
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
else
file = "./posts/posts.js";
}
Promise.all([includes.printHeader(),
require(file).main(filename, request),
includes.printFooter()]).then(function(content)
{
result.write(content.join(''));
result.end();
cache.put(filename, content.join(''));
}).catch(function(err)
{
console.log(err);
throw err;
});
}
else
{
result.write(html);
result.end();
}
try
{
const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
console.log(getClientAddress);
try
{
const getClientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
console.log(getClientAddress);
sql.logTraffic(getClientAddress, filename);
}
catch (e)
{ }
sql.logTraffic(getClientAddress, filename);
} }
catch (e)
{ }
} }
else else
{ {

+ 48
- 0
sites/admin.js View File

@ -0,0 +1,48 @@
//sending static content
const includes = require('../includes/includes.js');
//used to append static content to result
const contentLoader = require('../includes/staticContentServer.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
}
else
{
result.writeHead(200, {'Content-Type': 'text/html'});
const file = "../admin/admin.js";
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;
});
}
}
};

+ 83
- 0
sites/blog.js View File

@ -0,0 +1,83 @@
//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');
//caching program to make the application run faster
const cache = require('memory-cache');
/**
* @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("../includes/contact.js").main(request, result);
}
else
{
const html = cache.get(filename);
result.writeHead(200, {'Content-Type': 'text/html'});
if (html == null) {
var file = "";
if (filename === '' || filename === '/')
{
file = "../posts/homePage.js";
}
else
{
var urlSplit = filename.split("/");
if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "../posts/category.js";
else
file = "../posts/posts.js";
}
Promise.all([includes.printHeader(),
require(file).main(filename, request),
includes.printFooter()]).then(function (content)
{
result.write(content.join(''));
result.end();
cache.put(filename, content.join(''));
}).catch(function (err)
{
console.log(err);
throw err;
});
}
else
{
result.write(html);
result.end();
}
}
}
};

+ 41
- 0
sites/projects.js View File

@ -0,0 +1,41 @@
//file io
const utils = require('../utils/utils.js');
//used to parse the request URL
const url = require('url');
//used to append static content to result
const contentLoader = require('../includes/staticContentServer.js');
/**
* @author Jeffery Russell 10-30-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, baseURL)
{
var filename = url.parse(request.url, true).pathname;
if(filename == baseURL || filename == baseURL.substring(0, baseURL.length - 1))
{
filename = baseURL + "index.html";
}
if (!contentLoader.serveStaticContent(request, result, filename, "/blogContent/projects"))
{
//do something?
}
}
};

Loading…
Cancel
Save