You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pagePark/pagepark.js

423 lines
12 KiB
JavaScript

9 years ago
var myVersion = "0.53", myProductName = "PagePark";
10 years ago
//The MIT License (MIT)
//Copyright (c) 2014 Dave Winer
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
var fs = require ("fs");
var request = require ("request");
var urlpack = require ("url");
var http = require ("http");
var marked = require ("marked");
var dns = require ("dns");
9 years ago
var mime = require ("mime"); //1/8/15 by DW
9 years ago
var utils = require ("./lib/utils.js"); //1/18/15 by DW
10 years ago
10 years ago
var folderPathFromEnv = process.env.pageparkFolderPath; //1/3/15 by DW
9 years ago
var pageparkPrefs = {
9 years ago
myPort: 1339, //1/8/15 by DW -- was 80, see note in readme.md
10 years ago
indexFilename: "index"
};
var fnamePrefs = "prefs/prefs.json";
9 years ago
var pageparkStats = {
10 years ago
ctStarts: 0,
whenLastStart: new Date (0),
ctHits: 0, ctHitsToday: 0,
whenLastHit: new Date (0),
hitsByDomain: {}
};
var fnameStats = "prefs/stats.json", flStatsDirty = false;
var domainsPath = "domains/";
9 years ago
var configFname = "/config.json";
10 years ago
var mdTemplatePath = "prefs/mdTemplate.txt";
var urlDefaultTemplate = "http://fargo.io/code/pagepark/defaultmarkdowntemplate.txt";
//routines from utils.js, fs.js
function fsSureFilePath (path, callback) {
9 years ago
var splits = path.split ("/");
path = ""; //1/8/15 by DW
10 years ago
if (splits.length > 0) {
function doLevel (levelnum) {
if (levelnum < (splits.length - 1)) {
path += splits [levelnum] + "/";
fs.exists (path, function (flExists) {
if (flExists) {
doLevel (levelnum + 1);
}
else {
fs.mkdir (path, undefined, function () {
doLevel (levelnum + 1);
});
}
});
}
else {
if (callback != undefined) {
callback ();
}
}
}
doLevel (0);
}
else {
if (callback != undefined) {
callback ();
}
}
}
9 years ago
function httpExt2MIME (ext) { //12/24/14 by DW
mime.default_type = "text/plain";
return (mime.lookup (ext));
}
function httpReadUrl (url, callback) {
request (url, function (error, response, body) {
if (!error && (response.statusCode == 200)) {
callback (body)
}
});
}
10 years ago
function getFullFilePath (relpath) { //1/3/15 by DW
var folderpath = folderPathFromEnv;
if (folderpath == undefined) { //the environment variable wasn't specified
return (relpath);
}
9 years ago
if (!utils.endsWith (folderpath, "/")) {
10 years ago
folderpath += "/";
}
9 years ago
if (utils.beginsWith (relpath, "/")) {
relpath = utils.stringDelete (relpath, 1, 1);
10 years ago
}
return (folderpath + relpath);
}
10 years ago
function getMarkdownTemplate (callback) {
10 years ago
var f = getFullFilePath (mdTemplatePath);
fs.readFile (f, function (err, data) {
10 years ago
if (err) {
httpReadUrl (urlDefaultTemplate, function (s) {
fs.writeFile (mdTemplatePath, s, function (err) {
if (callback != undefined) {
callback (s);
}
});
});
}
else {
if (callback != undefined) {
callback (data.toString ());
}
}
});
}
function checkPathForIllegalChars (path) {
function isIllegal (ch) {
9 years ago
if (utils.isAlpha (ch) || utils.isNumeric (ch)) {
10 years ago
return (false);
}
switch (ch) {
case "/": case "_": case "-": case ".": case " ":
return (false);
}
return (true);
}
for (var i = 0; i < path.length; i++) {
if (isIllegal (path [i])) {
return (false);
}
}
9 years ago
if (utils.stringContains (path, "./")) {
10 years ago
return (false);
}
return (true);
}
function everySecond () {
if (flStatsDirty) {
9 years ago
writeStats (fnameStats, pageparkStats);
10 years ago
flStatsDirty = false;
}
}
9 years ago
10 years ago
function handleHttpRequest (httpRequest, httpResponse) {
9 years ago
function getConfigFile (host, callback) {
var f = getFullFilePath (domainsPath) + host + configFname;
fs.readFile (f, function (err, data) {
if (err) {
callback (undefined);
}
else {
try {
var config = JSON.parse (data.toString ());
callback (config);
}
catch (err) {
console.log ("getConfigFile: error reading " + configFname + " file for host " + host + ". " + err.message);
callback (undefined);
}
}
});
}
10 years ago
function return404 () {
httpResponse.writeHead (404, {"Content-Type": "text/plain"});
httpResponse.end ("The file was not found.");
}
function findIndexFile (folder, callback) {
fs.readdir (folder, function (err, list) {
for (var i = 0; i < list.length; i++) {
var fname = list [i];
9 years ago
if (utils.stringCountFields (fname, ".") == 2) { //something like xxx.yyy
if (utils.stringNthField (fname, ".", 1).toLowerCase () == pageparkPrefs.indexFilename) { //something like index.wtf
10 years ago
callback (folder + fname);
return;
}
}
}
return404 ();
});
}
function serveFile (f) {
fs.readFile (f, function (err, data) {
if (err) {
return404 ();
}
else {
9 years ago
var ext = utils.stringLastField (f, ".").toLowerCase (), type = httpExt2MIME (ext);
10 years ago
switch (ext) {
case "js":
try {
var val = eval (data.toString ());
httpResponse.writeHead (200, {"Content-Type": "text/html"});
httpResponse.end (val.toString ());
}
catch (err) {
httpResponse.writeHead (500, {"Content-Type": "text/plain"});
httpResponse.end ("Error running " + parsedUrl.pathname + ": \"" + err.message + "\"");
}
break;
case "md":
getMarkdownTemplate (function (theTemplate) {
var mdtext = data.toString (), pagetable = new Object ();
pagetable.bodytext = marked (mdtext);
9 years ago
pagetable.title = utils.stringLastField (f, "/");
var s = utils.multipleReplaceAll (theTemplate, pagetable, false, "[%", "%]");
10 years ago
httpResponse.writeHead (200, {"Content-Type": "text/html"});
httpResponse.end (s);
});
break;
default:
httpResponse.writeHead (200, {"Content-Type": type});
httpResponse.end (data);
break;
}
}
});
}
try {
var parsedUrl = urlpack.parse (httpRequest.url, true), host, lowerhost, port, referrer;
var lowerpath = parsedUrl.pathname.toLowerCase (), now = new Date ();
//set host, port
host = httpRequest.headers.host;
9 years ago
if (utils.stringContains (host, ":")) {
port = utils.stringNthField (host, ":", 2);
host = utils.stringNthField (host, ":", 1);
10 years ago
}
else {
port = 80;
}
lowerhost = host.toLowerCase ();
//set referrer
referrer = httpRequest.headers.referer;
if (referrer == undefined) {
referrer = "";
}
//stats
//hits by domain
9 years ago
if (pageparkStats.hitsByDomain [lowerhost] == undefined) {
pageparkStats.hitsByDomain [lowerhost] = 1;
10 years ago
}
else {
9 years ago
pageparkStats.hitsByDomain [lowerhost]++;
10 years ago
}
//hits today
9 years ago
if (!utils.sameDay (now, pageparkStats.whenLastHit)) { //day rollover
9 years ago
pageparkStats.ctHitsToday = 0;
10 years ago
}
9 years ago
pageparkStats.ctHits++;
pageparkStats.ctHitsToday++;
pageparkStats.whenLastHit = now;
10 years ago
flStatsDirty = true;
//log the request
dns.reverse (httpRequest.connection.remoteAddress, function (err, domains) {
var client = httpRequest.connection.remoteAddress;
if (!err) {
if (domains.length > 0) {
client = domains [0];
}
}
9 years ago
if (client == undefined) { //1/25/15 by DW
client = "";
}
10 years ago
console.log (now.toLocaleTimeString () + " " + httpRequest.method + " " + host + ":" + port + " " + lowerpath + " " + referrer + " " + client);
});
switch (lowerpath) {
case "/version":
httpResponse.writeHead (200, {"Content-Type": "text/plain"});
httpResponse.end (myVersion);
break;
case "/now":
httpResponse.writeHead (200, {"Content-Type": "text/plain"});
httpResponse.end (now.toString ());
break;
case "/status":
var status = {
9 years ago
prefs: pageparkPrefs,
status: pageparkStats
10 years ago
}
httpResponse.writeHead (200, {"Content-Type": "text/plain"});
9 years ago
httpResponse.end (utils.jsonStringify (status));
10 years ago
break;
default: //see if it's a path in the domains folder, if not 404
9 years ago
var domainfolder = getFullFilePath (domainsPath) + host;
var f = domainfolder + parsedUrl.pathname;
10 years ago
if (checkPathForIllegalChars (f)) {
fsSureFilePath (domainsPath, function () { //make sure domains folder exists
9 years ago
getConfigFile (host, function (config) { //get config.json, if it exists -- 1/18/15 by DW
if (config != undefined) {
console.log ("handleHttpRequest: config == " + utils.jsonStringify (config));
if (config.urlSiteRedirect != undefined) {
var urlRedirect = config.urlSiteRedirect + parsedUrl.pathname;
httpResponse.writeHead (302, {"Location": urlRedirect, "Content-Type": "text/plain"});
httpResponse.end ("Temporary redirect to " + urlRedirect + ".");
return;
}
10 years ago
}
9 years ago
fs.stat (f, function (err, stats) {
if (err) {
return404 ();
10 years ago
}
else {
9 years ago
if (stats.isDirectory ()) {
if (!utils.endsWith (f, "/")) {
f += "/";
}
findIndexFile (f, function (findex) {
serveFile (findex);
});
}
else {
serveFile (f);
}
10 years ago
}
9 years ago
});
10 years ago
});
});
}
else {
httpResponse.writeHead (500, {"Content-Type": "text/plain"});
httpResponse.end ("The file name contains illegal characters.");
}
break;
}
}
catch (tryError) {
httpResponse.writeHead (500, {"Content-Type": "text/plain"});
httpResponse.end (tryError.message);
}
}
9 years ago
function writeStats (fname, stats, callback) {
var f = getFullFilePath (fname);
fsSureFilePath (f, function () {
9 years ago
fs.writeFile (f, utils.jsonStringify (stats), function (err) {
9 years ago
if (err) {
console.log ("writeStats: error == " + err.message);
}
if (callback != undefined) {
callback ();
}
});
});
}
function readStats (fname, stats, callback) {
var f = getFullFilePath (fname);
fsSureFilePath (f, function () {
fs.exists (f, function (flExists) {
if (flExists) {
fs.readFile (f, function (err, data) {
if (err) {
console.log ("readStats: error reading file " + f + " == " + err.message)
if (callback != undefined) {
callback ();
}
}
else {
var storedStats = JSON.parse (data.toString ());
for (var x in storedStats) {
stats [x] = storedStats [x];
}
writeStats (fname, stats, function () {
if (callback != undefined) {
callback ();
}
});
}
});
}
else {
writeStats (fname, stats, function () {
if (callback != undefined) {
callback ();
}
});
}
});
});
}
10 years ago
function startup () {
9 years ago
readStats (fnamePrefs, pageparkPrefs, function () {
readStats (fnameStats, pageparkStats, function () {
fsSureFilePath (getFullFilePath (domainsPath) + "x", function () { //make sure domains folder exists
var now = new Date ();
pageparkStats.ctStarts++;
pageparkStats.whenLastStart = now;
flStatsDirty = true;
http.createServer (handleHttpRequest).listen (pageparkPrefs.myPort);
9 years ago
console.log (""); console.log (myProductName + " v" + myVersion + " running on port " + pageparkPrefs.myPort + "."); console.log ("");
9 years ago
setInterval (everySecond, 1000);
});
10 years ago
});
});
}
startup ();