From b2be7a828f5b3c6250d809cf016e70776b425031 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 17 Feb 2015 12:46:35 +0100 Subject: [PATCH 1/3] Default handler for errors in foxx routes now behaves differently in production and development mode --- js/server/modules/org/arangodb/foxx/routing.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/server/modules/org/arangodb/foxx/routing.js b/js/server/modules/org/arangodb/foxx/routing.js index 38642c4691..b081aa0b30 100644 --- a/js/server/modules/org/arangodb/foxx/routing.js +++ b/js/server/modules/org/arangodb/foxx/routing.js @@ -379,12 +379,12 @@ /// @brief transform the internal route objects into proper routing callbacks //////////////////////////////////////////////////////////////////////////////// - var transformRoutes = function (rt, routes, controller, prefix) { + var transformRoutes = function (rt, routes, controller, prefix, isDevel) { var j, route; for (j = 0; j < rt.length; ++j) { route = rt[j]; route.action = { - callback: transformControllerToRoute(route.action, route.url || "No Route") + callback: transformControllerToRoute(route.action, route.url || "No Route", isDevel) }; if (route.hasOwnProperty("url")) { route.url.match = arangodb.normalizeURL(prefix + "/" + route.url.match); From d9bc1be027ed0180cb25a5266d556a2f94f06c99 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 17 Feb 2015 16:45:19 +0100 Subject: [PATCH 2/3] Fixed upgrade script. Did print some lines in an unreadable format --- js/server/upgrade-database.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/server/upgrade-database.js b/js/server/upgrade-database.js index 9212122eff..bf2b2f8f22 100644 --- a/js/server/upgrade-database.js +++ b/js/server/upgrade-database.js @@ -1454,7 +1454,7 @@ function updateGlobals() { try { mapAppZip[tmp.app] = fmUtils.zipDirectory(path); } catch (e) { - logger.errorLines("Tried to move app " + tmp.app + " but it was not found at app-path" + path + + logger.errorLines("Tried to move app " + tmp.app + " but it was not found at app-path " + path + " : " +(e.stack || String(e))); } } @@ -1484,7 +1484,7 @@ function updateGlobals() { try { fs.removeDirectoryRecursive(module.oldAppPath(), true); } catch(e) { - logger.log("Unable to remove old app path %s", module.oldAppPath()); + logger.warn("Unable to remove old app path " + module.oldAppPath()); } // 4. For each mounted app, reinstall appId from zipFile to mount From 3c96aa8c6ae1d2ccc20a99cc45b3028c40fe904b Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 17 Feb 2015 17:25:04 +0100 Subject: [PATCH 3/3] Applied default configuration to foxx apps. Fixed RegEx for allowed routes. Started to fix exports. --- js/server/modules/org/arangodb/foxx.js | 2 +- .../modules/org/arangodb/foxx/arangoApp.js | 24 ++++++++++++++++++ .../modules/org/arangodb/foxx/manager.js | 16 ++++++++++-- .../modules/org/arangodb/foxx/routing.js | 25 +++++++++++++++++-- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/js/server/modules/org/arangodb/foxx.js b/js/server/modules/org/arangodb/foxx.js index 4545f3bc93..1f4870d60b 100644 --- a/js/server/modules/org/arangodb/foxx.js +++ b/js/server/modules/org/arangodb/foxx.js @@ -44,7 +44,7 @@ exports.createQuery = createQuery; exports.toJSONSchema = toJSONSchema; exports.requireApp = function (path) { 'use strict'; - return manager.mountedApp(arangodb.normalizeURL('/' + path)); + return manager.requireApp(arangodb.normalizeURL('/' + path)); }; // ----------------------------------------------------------------------------- diff --git a/js/server/modules/org/arangodb/foxx/arangoApp.js b/js/server/modules/org/arangodb/foxx/arangoApp.js index 242b15d8eb..4508a64cfd 100644 --- a/js/server/modules/org/arangodb/foxx/arangoApp.js +++ b/js/server/modules/org/arangodb/foxx/arangoApp.js @@ -47,6 +47,25 @@ var errors = arangodb.errors; var throwFileNotFound = arangodb.throwFileNotFound; + +// ----------------------------------------------------------------------------- +// --SECTION-- private functions +// ----------------------------------------------------------------------------- + + var applyDefaultConfig = function(config) { + if (config === undefined) { + return {}; + } + var res = {}; + var attr; + for (attr in config) { + if (config.hasOwnProperty(attr) && config[attr].hasOwnProperty("default")) { + res[attr] = config[attr].default; + } + } + return res; + }; + // ----------------------------------------------------------------------------- // --SECTION-- constructors and destructors // ----------------------------------------------------------------------------- @@ -137,6 +156,11 @@ this._isDevelopment = config.isDevelopment || false; this._exports = {}; this._collectionPrefix = this._mount.substr(1).replace(/-/g, "_").replace(/\//g, "_") + "_"; + // Apply the default configuration and ignore all missing options + + var cfg = config.options.configuration; + this._options.configuration = applyDefaultConfig(this._manifest.configuration); + this.configure(cfg); this._context = new AppContext(this); if (! this._manifest.hasOwnProperty("defaultDocument")) { diff --git a/js/server/modules/org/arangodb/foxx/manager.js b/js/server/modules/org/arangodb/foxx/manager.js index 4940c50bb6..85a92e9191 100644 --- a/js/server/modules/org/arangodb/foxx/manager.js +++ b/js/server/modules/org/arangodb/foxx/manager.js @@ -960,7 +960,7 @@ "configure()", [ [ "Mount path", "string" ] ], [ mount ] ); - utils.validateMount(mount); + utils.validateMount(mount, true); var app = lookupApp(mount); var invalid = app.configure(options); if (invalid.length > 0) { @@ -981,11 +981,22 @@ "configuration()", [ [ "Mount path", "string" ] ], [ mount ] ); - utils.validateMount(mount); + utils.validateMount(mount, true); var app = lookupApp(mount); return app.getConfiguration(); }; + var requireApp = function(mount) { + checkParameter( + "requireApp()", + [ [ "Mount path", "string" ] ], + [ mount ] ); + utils.validateMount(mount, true); + var app = lookupApp(mount); + require("console").log("Exports:", app._exports); + return app._exports; + }; + // ----------------------------------------------------------------------------- // --SECTION-- exports @@ -1005,6 +1016,7 @@ exports.production = setProduction; exports.configure = configure; exports.configuration = configuration; + exports.requireApp = requireApp; //////////////////////////////////////////////////////////////////////////////// /// @brief Serverside only API diff --git a/js/server/modules/org/arangodb/foxx/routing.js b/js/server/modules/org/arangodb/foxx/routing.js index b081aa0b30..2f3960a6ed 100644 --- a/js/server/modules/org/arangodb/foxx/routing.js +++ b/js/server/modules/org/arangodb/foxx/routing.js @@ -36,6 +36,8 @@ // ----------------------------------------------------------------------------- var arangodb = require("org/arangodb"); + var ArangoError = arangodb.ArangoError; + var errors = arangodb.errors; var preprocess = require("org/arangodb/foxx/preprocessor").preprocess; var _ = require("underscore"); var fs = require("fs"); @@ -43,7 +45,6 @@ var frontendDevelopmentMode = require("internal").frontendDevelopmentMode; var console = require("console"); var actions = require("org/arangodb/actions"); - var utils = require("org/arangodb/foxx/manager-utils"); // ----------------------------------------------------------------------------- // --SECTION-- private functions @@ -394,6 +395,26 @@ } }; + var routeRegEx = /^(\/:?[a-zA-Z0-9_\-%]+)+\/?$/; + + var validateRoute = function(route) { + if (route[0] !== "/") { + throw new ArangoError({ + errorNum: errors.ERROR_INVALID_MOUNTPOINT.code, + errorMessage: "Route has to start with /." + }); + } + if (!routeRegEx.test(route)) { + // Controller routes may be /. Foxxes are not allowed to + if (route.length !== 1) { + throw new ArangoError({ + errorNum: errors.ERROR_INVALID_MOUNTPOINT.code, + errorMessage: "Route parts '" + route + "' may only contain a-z, A-Z, 0-9 or _. But may start with a :" + }); + } + } + }; + // ----------------------------------------------------------------------------- // --SECTION-- public functions // ----------------------------------------------------------------------------- @@ -472,7 +493,7 @@ try { for (i in controllers) { if (controllers.hasOwnProperty(i)) { - utils.validateMount(i, true); + validateRoute(i); file = controllers[i]; // set up a context for the application start function