From 90f66c89dd4763e42af0e4b6efaf09ea5b8f7a2f Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Fri, 8 Apr 2011 18:02:16 -0600
Subject: [PATCH] (authority) implement have_email wsapi and dispatch

---
 authority/server/db.js    |  6 ++++++
 authority/server/run.js   | 31 +++++++++++++++++++++++++++----
 authority/server/wsapi.js | 16 ++++++++++++++++
 3 files changed, 49 insertions(+), 4 deletions(-)
 create mode 100644 authority/server/db.js
 create mode 100644 authority/server/wsapi.js

diff --git a/authority/server/db.js b/authority/server/db.js
new file mode 100644
index 000000000..d8e7d964c
--- /dev/null
+++ b/authority/server/db.js
@@ -0,0 +1,6 @@
+var g_emails = {
+};
+
+exports.haveEmail = function(email) {
+  return g_emails.hasOwnProperty(email);
+};
diff --git a/authority/server/run.js b/authority/server/run.js
index 3586ef877..15420661a 100644
--- a/authority/server/run.js
+++ b/authority/server/run.js
@@ -1,17 +1,40 @@
 console.log("authority handler starting up!");
 
-const path = require('path'); 
+const path = require('path'),
+       url = require('url'),
+     wsapi = require('./wsapi.js');
 
 const STATIC_DIR = path.join(path.dirname(__dirname), "static");
 
+function fourOhFour(resp, reason) {
+  resp.writeHead(404, {"Content-Type": "text/plain"});
+  resp.write("404 Not Found");
+  if (reason) {
+    resp.write(": " + reason);
+  }
+  resp.end();
+  return undefined;
+}
+
 exports.handler = function(request, response, serveFile) {
   // dispatch!
-  console.log(request.url);
+  var urlpath = url.parse(request.url).pathname;
 
-  if (request.url === '/sign_in') {
+  if (urlpath === '/sign_in') {
     serveFile(path.join(STATIC_DIR, "dialog", "index.html"));
+  } else if (/^\/wsapi\/\w+$/.test(urlpath)) {
+    try {
+      var method = path.basename(urlpath);
+      wsapi[method](request, response);
+    } catch(e) {
+      var errMsg = "oops, no such wsapi method: " + method + " (" + e.toString() +")";
+      fourOhFour(response, errMsg);
+      console.log(errMsg);
+    }
   } else {
     // node.js takes care of sanitizing the request path
-    serveFile(path.join(STATIC_DIR, request.url));
+    serveFile(path.join(STATIC_DIR, urlpath));
   }
 };
+
+console.log("authority handler started!");
diff --git a/authority/server/wsapi.js b/authority/server/wsapi.js
new file mode 100644
index 000000000..e85187e29
--- /dev/null
+++ b/authority/server/wsapi.js
@@ -0,0 +1,16 @@
+const db = require('./db.js'),
+     url = require('url');
+
+exports.have_email = function(req, resp) {
+  // get inputs from get data!
+  var email = url.parse(req.url, true).query['email'];
+
+  var haveit = db.haveEmail(email);
+
+  console.log("have_email for " + email + ": " + haveit);
+
+  // 200 means we have the email, 404 means no
+  resp.writeHead(haveit ? 200 : 404, {"Content-Type": "application/json"});
+  resp.write(JSON.stringify(haveit));
+  resp.end();
+};
\ No newline at end of file
-- 
GitLab