issue #24098: all browser access to all extension client directories
authorSteve Hackbarth <stephenhackbarth@gmail.com>
Mon, 7 Jul 2014 18:06:36 +0000 (14:06 -0400)
committerSteve Hackbarth <stephenhackbarth@gmail.com>
Mon, 7 Jul 2014 18:06:36 +0000 (14:06 -0400)
node-datasource/main.js

index 5ca1713..24568c9 100755 (executable)
@@ -10,6 +10,7 @@ _ = require("underscore");
 jsonpatch = require("json-patch");
 SYS = {};
 XT = { };
+var express = require('express');
 var app;
 
 (function () {
@@ -94,53 +95,31 @@ var app;
   XT.session = Object.create(XT.Session);
   XT.session.schemas.SYS = false;
 
-
-//
-// Load all extension-defined routes. By convention the paths,
-// filenames, and functions to be used
-// for the routes should be described in a file called routes.js
-// in the routes directory.
-//
-/*
-if (X.options.extensionRoutes && X.options.extensionRoutes.length > 0) {
-  _.each(X.options.extensionRoutes, function (route) {
-    "use strict";
-    var routes = require(__dirname + "/" + route + "/routes");
-
-    _.each(routes, function (routeDetails) {
-      var verb = (routeDetails.verb || "all").toLowerCase();
-      if (_.contains(["all", "get", "post", "patch", "delete"], verb)) {
-        app[verb]('/:org/' + routeDetails.path, routeDetails.function);
-      } else {
-        console.log("Invalid verb for extension-defined route " + routeDetails.path);
-      }
+  var getExtensionDir = function (extension) {
+    return _.contains(["/private-extensions", "/xtuple-extensions"], extension.location) ?
+      X.path.join(__dirname, "../..", extension.location, "source", extension.name) :
+      extension.location === "/core-extensions" ?
+      X.path.join(__dirname, "../enyo-client/extensions/source", extension.name) :
+      extension.location === "npm" ?
+      X.path.join(__dirname, "../node_modules", extension.name) : "error";
+  };
+  var useClientDir = function (path, dir) {
+    path = path.indexOf("npm") === 0 ? "/" + path : path;
+    _.each(X.options.datasource.databases, function (orgValue, orgKey, orgList) {
+      app.use("/" + orgValue + path, express.static(dir, { maxAge: 86400000 }));
     });
-  });
-}
-*/
-
+  };
   var loadExtensionClientside = function (extension) {
-
+    var extensionLocation = extension.location === "npm" ? extension.location : extension.location + "/source";
+    useClientDir(extensionLocation + "/" + extension.name + "/client", X.path.join(getExtensionDir(extension), "client"));
   };
   var loadExtensionRoutes = function (extension) {
-    var extensionLocation = _.contains(["/private-extensions", "/xtuple-extensions"], extension.location) ?
-      X.path.join(__dirname, "../..", extension.location, "source") :
-      extension.location === "/core-extensions" ?
-      X.path.join(__dirname, "../enyo-client/extensions/source") :
-      extension.location === "npm" ?
-      X.path.join(__dirname, "../node_modules") : "error";
-
-    if (!app) {
-      // XXX time bomb: assuming app has been initialized, below, by now
-      XT.log("Could not load extension routes");
-      return;
-    }
-    var manifest = JSON.parse(X.fs.readFileSync(X.path.join(extensionLocation,
-        extension.name, "database/source/manifest.js")));
+    var manifest = JSON.parse(X.fs.readFileSync(X.path.join(getExtensionDir(extension),
+        "database/source/manifest.js")));
     _.each(manifest.routes || [], function (routeDetails) {
       var verb = (routeDetails.verb || "all").toLowerCase(),
-        func = require(X.path.join(__dirname, "../..", extension.location,
-          extension.name, "node-datasource", routeDetails.filename))[routeDetails.functionName];
+        func = require(X.path.join(getExtensionDir(extension),
+          "node-datasource", routeDetails.filename))[routeDetails.functionName];
 
       if (_.contains(["all", "get", "post", "patch", "delete"], verb)) {
         app[verb]('/:org/' + routeDetails.path, func);
@@ -162,6 +141,12 @@ if (X.options.extensionRoutes && X.options.extensionRoutes.length > 0) {
     extensions.fetch({
       database: X.options.datasource.databases[0],
       success: function (coll, results, options) {
+        if (!app) {
+          // XXX time bomb: assuming app has been initialized, below, by now
+          XT.log("Could not load extension routes");
+          return;
+        }
+        useClientDir("/client", "../enyo-client/application");
         _.each(results, loadExtensionRoutes);
         _.each(results, loadExtensionClientside);
       }
@@ -190,8 +175,7 @@ try {
 /**
  * Module dependencies.
  */
-var express = require('express'),
-    passport = require('passport'),
+  var passport = require('passport'),
     oauth2 = require('./oauth2/oauth2'),
     routes = require('./routes/routes'),
     socketio = require('socket.io'),
@@ -406,22 +390,6 @@ require('./oauth2/passport');
 var that = this;
 
 app.use(express.favicon(__dirname + '/views/login/assets/favicon.ico'));
-var useClientDir = function (path, dir) {
-  "use strict";
-  _.each(X.options.datasource.databases, function (orgValue, orgKey, orgList) {
-    app.use("/" + orgValue + '/' + path, express.static(dir, { maxAge: 86400000 }));
-  });
-};
-if (X.options.datasource.debugging) {
-  _.each(X.options.datasource.databases, function (orgValue, orgKey, orgList) {
-    "use strict";
-    app.use("/" + orgValue + '/client', express.static('../enyo-client/application', { maxAge: 86400000 }));
-    app.use("/" + orgValue + '/core-extensions', express.static('../enyo-client/extensions', { maxAge: 86400000 }));
-    app.use("/" + orgValue + '/private-extensions', express.static('../../private-extensions', { maxAge: 86400000 }));
-    app.use("/" + orgValue + '/xtuple-extensions', express.static('../../xtuple-extensions', { maxAge: 86400000 }));
-    app.use("/" + orgValue + '/npm', express.static('../node_modules', { maxAge: 86400000 }));
-  });
-}
 app.use('/assets', express.static('views/login/assets', { maxAge: 86400000 }));
 
 app.get('/:org/dialog/authorize', oauth2.authorization);