From dc75b0997aa5e62bebeea6c63d4a3910f7d63f98 Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Tue, 21 Jun 2011 11:23:05 -0600 Subject: [PATCH] port primary and testing harness to new file organization --- .gitignore | 3 -- primary/.gitignore | 1 + primary/{server => }/app.js | 28 +++++++++++-------- primary/{server => lib}/db.js | 2 +- primary/{server => lib}/httputils.js | 0 primary/{server => lib}/secrets.js | 0 primary/{server => lib}/webfinger.js | 0 .../{server => lib}/webfinger_template.xml | 0 primary/{server => lib}/wsapi.js | 0 primary/{server/standalone.js => run.js} | 8 +++--- run.js | 2 +- verifier/.gitignore | 1 + 12 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 primary/.gitignore rename primary/{server => }/app.js (70%) rename primary/{server => lib}/db.js (97%) rename primary/{server => lib}/httputils.js (100%) rename primary/{server => lib}/secrets.js (100%) rename primary/{server => lib}/webfinger.js (100%) rename primary/{server => lib}/webfinger_template.xml (100%) rename primary/{server => lib}/wsapi.js (100%) rename primary/{server/standalone.js => run.js} (73%) mode change 100644 => 100755 create mode 100644 verifier/.gitignore diff --git a/.gitignore b/.gitignore index 73041fa17..d92c19c88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -*.sekret -*.sqlite -*.log *~ \#*\# .\#* diff --git a/primary/.gitignore b/primary/.gitignore new file mode 100644 index 000000000..c4c53c804 --- /dev/null +++ b/primary/.gitignore @@ -0,0 +1 @@ +/var diff --git a/primary/server/app.js b/primary/app.js similarity index 70% rename from primary/server/app.js rename to primary/app.js index f44582a04..9aee24b82 100644 --- a/primary/server/app.js +++ b/primary/app.js @@ -1,21 +1,27 @@ const path = require('path'), +fs = require('fs'), url = require('url'), -wsapi = require('./wsapi.js'), -httputils = require('./httputils.js'), -webfinger = require('./webfinger.js'), +wsapi = require('./lib/wsapi.js'), +httputils = require('./lib/httputils.js'), +webfinger = require('./lib/webfinger.js'), sessions = require('cookie-sessions'), -secrets = require('./secrets.js'); +secrets = require('./lib/secrets.js'); -const STATIC_DIR = path.join(path.dirname(__dirname), "static"); +// create the var directory if it doesn't exist +var VAR_DIR = path.join(__dirname, "var"); +try { fs.mkdirSync(VAR_DIR, 0755); } catch(e) { } -const COOKIE_SECRET = secrets.hydrateSecret('cookie_secret', __dirname); +const STATIC_DIR = path.join(__dirname, "static"); -function handler(request, response, serveFile) { +const COOKIE_SECRET = secrets.hydrateSecret('cookie_secret', VAR_DIR); + +function handler(request, response, next) { // dispatch! var urlpath = url.parse(request.url).pathname; if (urlpath === '/sign_in') { - serveFile(path.join(STATIC_DIR, "dialog", "index.html"), response); + request.url = "/dialog/index.html"; + next(); } else if (/^\/wsapi\/\w+$/.test(urlpath)) { try { var method = path.basename(urlpath); @@ -38,13 +44,11 @@ function handler(request, response, serveFile) { } else if (urlpath === "/code_update") { console.log("code updated. shutting down."); process.exit(); - } else { - // node.js takes care of sanitizing the request path - if (urlpath == "/") urlpath = "/index.html" - serveFile(path.join(STATIC_DIR, urlpath), response); } }; +exports.varDir = VAR_DIR; + exports.setup = function(app) { var week = (7 * 24 * 60 * 60 * 1000); app.use(sessions({ diff --git a/primary/server/db.js b/primary/lib/db.js similarity index 97% rename from primary/server/db.js rename to primary/lib/db.js index ff4473046..e30356da5 100644 --- a/primary/server/db.js +++ b/primary/lib/db.js @@ -3,7 +3,7 @@ const sqlite = require('sqlite'), var db = new sqlite.Database(); -db.open(path.join(path.dirname(__dirname), "userdb.sqlite"), function (error) { +db.open(path.join(path.dirname(__dirname), "var", "userdb.sqlite"), function (error) { if (error) { console.log("Couldn't open database: " + error); throw error; diff --git a/primary/server/httputils.js b/primary/lib/httputils.js similarity index 100% rename from primary/server/httputils.js rename to primary/lib/httputils.js diff --git a/primary/server/secrets.js b/primary/lib/secrets.js similarity index 100% rename from primary/server/secrets.js rename to primary/lib/secrets.js diff --git a/primary/server/webfinger.js b/primary/lib/webfinger.js similarity index 100% rename from primary/server/webfinger.js rename to primary/lib/webfinger.js diff --git a/primary/server/webfinger_template.xml b/primary/lib/webfinger_template.xml similarity index 100% rename from primary/server/webfinger_template.xml rename to primary/lib/webfinger_template.xml diff --git a/primary/server/wsapi.js b/primary/lib/wsapi.js similarity index 100% rename from primary/server/wsapi.js rename to primary/lib/wsapi.js diff --git a/primary/server/standalone.js b/primary/run.js old mode 100644 new mode 100755 similarity index 73% rename from primary/server/standalone.js rename to primary/run.js index 7197e7c69..228d29092 --- a/primary/server/standalone.js +++ b/primary/run.js @@ -1,6 +1,6 @@ +#!/usr/bin/env node + var sys = require("sys"), - http = require("http"), - url = require("url"), path = require("path"), fs = require("fs"), express = require("express"); @@ -13,13 +13,13 @@ var handler = require("./app.js"); var app = express.createServer(); app.use(express.logger({ - stream: fs.createWriteStream(path.join(__dirname, "server.log")) + stream: fs.createWriteStream(path.join(handler.varDir, "server.log")) })); // let the specific server interact directly with the connect server to register their middleware if (handler.setup) handler.setup(app); // use the connect 'static' middleware for serving of static files (cache headers, HTTP range, etc) -app.use(express.static(path.join(path.dirname(__dirname), "static"))); +app.use(express.static(path.join(__dirname, "static"))); app.listen(PRIMARY_PORT, PRIMARY_HOST); diff --git a/run.js b/run.js index fdb42c908..915da6e66 100755 --- a/run.js +++ b/run.js @@ -157,7 +157,7 @@ console.log("Running test servers:"); dirs.forEach(function(dirObj) { if (!fs.statSync(dirObj.path).isDirectory()) return; // does this server have a js handler for custom request handling? - var handlerPath = path.join(dirObj.path, "server", "app.js"); + var handlerPath = path.join(dirObj.path, "app.js"); var runJS = {}; try { var runJSExists = false; diff --git a/verifier/.gitignore b/verifier/.gitignore new file mode 100644 index 000000000..c4c53c804 --- /dev/null +++ b/verifier/.gitignore @@ -0,0 +1 @@ +/var -- GitLab