master
Dave Winer 10 years ago
parent b9f84b2b06
commit 16450deb6f

@ -71,6 +71,16 @@ I wanted to make code that could be used for people who are just getting started
There will always be more work to do here. ;-) There will always be more work to do here. ;-)
#### Updates
##### v0.47 1/7/15 by DW
Fixed first time startup problem creating prefs.json and stats.json.
Also, we now make sure the *domains* folder exists at startup.
Fixed a problem in handling requests if you specified a different folder for PagePark to serve from.
#### Questions, comments? #### Questions, comments?
Please post a note on the <a href="https://groups.google.com/forum/#!forum/server-snacks">Server Snacks</a> mail list. Please post a note on the <a href="https://groups.google.com/forum/#!forum/server-snacks">Server Snacks</a> mail list.

@ -0,0 +1,20 @@
{
"name": "PagePark",
"description": "A simple Node.js folder-based HTTP server that serves static and dynamic pages for domains.",
"author": "Dave Winer <dave@smallpicture.com>",
"version": "0.40.0",
"scripts": {
"start": "node pagepark.js"
},
"dependencies" : {
"request": "*",
"url": "*",
"http": "*",
"marked": "*",
"dns": "*"
},
"license": "MIT",
"engines": {
"node": "0.10.*"
}
}

@ -1,4 +1,4 @@
var myVersion = "0.46", myProductName = "Page Park"; var myVersion = "0.47", myProductName = "PagePark";
//The MIT License (MIT) //The MIT License (MIT)
@ -31,13 +31,13 @@ var dns = require ("dns");
var folderPathFromEnv = process.env.pageparkFolderPath; //1/3/15 by DW var folderPathFromEnv = process.env.pageparkFolderPath; //1/3/15 by DW
var pageParkPrefs = { var pageparkPrefs = {
myPort: 80, myPort: 80,
indexFilename: "index" indexFilename: "index"
}; };
var fnamePrefs = "prefs/prefs.json"; var fnamePrefs = "prefs/prefs.json";
var pageParkStats = { var pageparkStats = {
ctStarts: 0, ctStarts: 0,
whenLastStart: new Date (0), whenLastStart: new Date (0),
ctHits: 0, ctHitsToday: 0, ctHits: 0, ctHitsToday: 0,
@ -275,44 +275,6 @@ function getFullFilePath (relpath) { //1/3/15 by DW
} }
return (folderpath + relpath); return (folderpath + relpath);
} }
function writeStats (fname, stats) {
var f = getFullFilePath (fname);
fsSureFilePath (f, function () {
fs.writeFile (f, jsonStringify (stats), function (err) {
if (err) {
console.log ("writeStats: error == " + err.message);
}
});
});
}
function readStats (fname, stats, callback) {
var f = getFullFilePath (fname);
fs.exists (f, function (flExists) {
if (flExists) {
fs.readFile (f, function (err, data) {
if (err) {
console.log ("readStats: error reading file " + f + " == " + err.message)
}
else {
var storedStats = JSON.parse (data.toString ());
for (var x in storedStats) {
stats [x] = storedStats [x];
}
writeStats (fname, stats);
}
if (callback != undefined) {
callback ();
}
});
}
else {
writeStats (fname, stats);
if (callback != undefined) {
callback ();
}
}
});
}
function getMarkdownTemplate (callback) { function getMarkdownTemplate (callback) {
var f = getFullFilePath (mdTemplatePath); var f = getFullFilePath (mdTemplatePath);
fs.readFile (f, function (err, data) { fs.readFile (f, function (err, data) {
@ -356,7 +318,7 @@ function checkPathForIllegalChars (path) {
function everySecond () { function everySecond () {
if (flStatsDirty) { if (flStatsDirty) {
writeStats (fnameStats, pageParkStats); writeStats (fnameStats, pageparkStats);
flStatsDirty = false; flStatsDirty = false;
} }
} }
@ -372,7 +334,7 @@ function handleHttpRequest (httpRequest, httpResponse) {
for (var i = 0; i < list.length; i++) { for (var i = 0; i < list.length; i++) {
var fname = list [i]; var fname = list [i];
if (stringCountFields (fname, ".") == 2) { //something like xxx.yyy if (stringCountFields (fname, ".") == 2) { //something like xxx.yyy
if (stringNthField (fname, ".", 1).toLowerCase () == pageParkPrefs.indexFilename) { //something like index.wtf if (stringNthField (fname, ".", 1).toLowerCase () == pageparkPrefs.indexFilename) { //something like index.wtf
callback (folder + fname); callback (folder + fname);
return; return;
} }
@ -440,19 +402,19 @@ function handleHttpRequest (httpRequest, httpResponse) {
//stats //stats
//hits by domain //hits by domain
if (pageParkStats.hitsByDomain [lowerhost] == undefined) { if (pageparkStats.hitsByDomain [lowerhost] == undefined) {
pageParkStats.hitsByDomain [lowerhost] = 1; pageparkStats.hitsByDomain [lowerhost] = 1;
} }
else { else {
pageParkStats.hitsByDomain [lowerhost]++; pageparkStats.hitsByDomain [lowerhost]++;
} }
//hits today //hits today
if (!sameDay (now, pageParkStats.whenLastHit)) { //day rollover if (!sameDay (now, pageparkStats.whenLastHit)) { //day rollover
pageParkStats.ctHitsToday = 0; pageparkStats.ctHitsToday = 0;
} }
pageParkStats.ctHits++; pageparkStats.ctHits++;
pageParkStats.ctHitsToday++; pageparkStats.ctHitsToday++;
pageParkStats.whenLastHit = now; pageparkStats.whenLastHit = now;
flStatsDirty = true; flStatsDirty = true;
//log the request //log the request
@ -477,14 +439,14 @@ function handleHttpRequest (httpRequest, httpResponse) {
break; break;
case "/status": case "/status":
var status = { var status = {
prefs: pageParkPrefs, prefs: pageparkPrefs,
status: pageParkStats status: pageparkStats
} }
httpResponse.writeHead (200, {"Content-Type": "text/plain"}); httpResponse.writeHead (200, {"Content-Type": "text/plain"});
httpResponse.end (jsonStringify (status)); httpResponse.end (jsonStringify (status));
break; break;
default: //see if it's a path in the domains folder, if not 404 default: //see if it's a path in the domains folder, if not 404
var f = domainsPath + host + parsedUrl.pathname; var f = getFullFilePath (domainsPath) + host + parsedUrl.pathname;
if (checkPathForIllegalChars (f)) { if (checkPathForIllegalChars (f)) {
fsSureFilePath (domainsPath, function () { //make sure domains folder exists fsSureFilePath (domainsPath, function () { //make sure domains folder exists
fs.stat (f, function (err, stats) { fs.stat (f, function (err, stats) {
@ -520,16 +482,69 @@ function handleHttpRequest (httpRequest, httpResponse) {
} }
} }
function writeStats (fname, stats, callback) {
var f = getFullFilePath (fname);
fsSureFilePath (f, function () {
fs.writeFile (f, jsonStringify (stats), function (err) {
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 ();
}
});
}
});
});
}
function startup () { function startup () {
readStats (fnamePrefs, pageParkPrefs, function () { readStats (fnamePrefs, pageparkPrefs, function () {
readStats (fnameStats, pageParkStats, function () { readStats (fnameStats, pageparkStats, function () {
var now = new Date (); fsSureFilePath (getFullFilePath (domainsPath) + "x", function () { //make sure domains folder exists
console.log (myProductName + " v" + myVersion + "."); var now = new Date ();
pageParkStats.ctStarts++; console.log (myProductName + " v" + myVersion + ".");
pageParkStats.whenLastStart = now; pageparkStats.ctStarts++;
flStatsDirty = true; pageparkStats.whenLastStart = now;
http.createServer (handleHttpRequest).listen (pageParkPrefs.myPort); flStatsDirty = true;
setInterval (everySecond, 1000); http.createServer (handleHttpRequest).listen (pageparkPrefs.myPort);
setInterval (everySecond, 1000);
});
}); });
}); });
} }

Loading…
Cancel
Save