Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from AirVantage/feature/Support-debug-mode
Browse files Browse the repository at this point in the history
Support 'debug=true' mode
  • Loading branch information
phtrivier committed Apr 7, 2015
2 parents 9174a81 + e683e9d commit ddecbd0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
71 changes: 43 additions & 28 deletions techStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@ var assert = require("assert");
*
* @param opts.optimize
* {Boolean} if true, all resources are served from a single 'dist/public' folder
* @param opts.dirs
* {Array} of paths to dirs that contain a 'public' folder to serve. (eg ['/path/to/av-server'])
* @param opts.resourcesUrlBase
* {String} eg ("/resources/15.0.1")
* @param opts.version
* {String} the version of the web application released. It is used to compute the URL of all static resources (eg "15.01")
*
* @param opts.cacheOptions
* @param [opts.cacheOptions.ms]
* {Integer} number of ms during which resources should be cached
* {Integer} number of ms during which resources should be cached
* @param [opts.cacheOptions.seconds]
* {Integer} number of seconds during which resources should be cached
* {Integer} number of seconds during which resources should be cached
*
* @param [opts.staticMw]
* {Function} mw function to server static. Defaults to express.static.
* This argument should only be used for testing.
* {Function} mw function to server static. Defaults to express.static.
* This argument should only be used for testing.
*/
var serveStaticAssets = function(app, opts) {

assert.ok(opts.dirs);
assert.ok(opts.resourcesUrlBase);
assert.ok(opts.version);

var resourcesUrlBase = "/resources/" + opts.version;
// In case the 'debug=true' mode is activated, the URL contains the '/resources-debug/' path segment
// and resources should be served from the 'public' folder with no caching (maxAge: 0)
var resourcesDebugUrlBase = "/resources-debug/" + opts.version;

var dirs = opts.dirs;
var resourcesUrlBase = opts.resourcesUrlBase;
var optimize = opts.optimize;
var cacheOptions = staticCacheOptions(opts.cache);
var staticMw = opts.staticMw || express.static;
Expand All @@ -44,6 +48,15 @@ var serveStaticAssets = function(app, opts) {
var distDir = path.join(dirs[0], "dist", "public");
logger.debug("Serving optimized resources from folder:", distDir);
app.use(resourcesUrlBase, staticMw(distDir, cacheOptions));

_.each(dirs, function(dir) {
var publicDir = path.join(dir, "public");
logger.debug("Serving non optimized resources from folder:", publicDir);
app.use(resourcesDebugUrlBase, staticMw(publicDir, {
maxAge: 0
}));
});

} else {
_.each(dirs, function(dir) {
var publicDir = path.join(dir, "public");
Expand All @@ -55,17 +68,17 @@ var serveStaticAssets = function(app, opts) {

function staticCacheOptions(cacheConfiguration) {
var res = {
ms : 0
ms: 0
};
if (cacheConfiguration) {
if (cacheConfiguration.seconds) {
res = {
maxAge : cacheConfiguration.seconds * 1000
maxAge: cacheConfiguration.seconds * 1000
};
}
if (cacheConfiguration.ms) {
res = {
maxAge : cacheConfiguration.ms
maxAge: cacheConfiguration.ms
};
}
}
Expand All @@ -83,22 +96,24 @@ function staticCacheOptions(cacheConfiguration) {
* @param opts.bundles
* {Array} bundles to be served (eg ["PortalMessages"])
*
* @param opts.resourcesUrlBase
* {String} base of all urls that serves resources (eg /resources/15.01)
* @param opts.version
* {String} the version of the web application released. It is used to compute the URL of all static resources (eg "15.01")
*
* @param opts.contextUrl
* {String} base of all *other* urls (eg "/" or "")
*/
var serveI18nBundles = function(app, opts) {

assert.ok(opts.bundles);
assert.ok(opts.resourcesUrlBase);
assert.ok(opts.version);

var resourcesUrlBase = "/resources/" + opts.version;

var bundles = opts.bundles;

_.each(bundles, function(bundle) {
var en_path = [opts.resourcesUrlBase, "/i18n/", bundle, "_en.properties"].join("");
var no_locale_path = [opts.resourcesUrlBase, "/i18n/", bundle, ".properties"].join("");
var en_path = [resourcesUrlBase, "/i18n/", bundle, "_en.properties"].join("");
var no_locale_path = [resourcesUrlBase, "/i18n/", bundle, ".properties"].join("");
app.use(en_path, function(req, res) {
res.redirect(opts.contextUrl + no_locale_path);
});
Expand All @@ -123,17 +138,17 @@ function configure(app, configuration, dirs, bundles) {
checkOptimizedDir(dirs[0], configuration);

serveStaticAssets(app, {
resourcesUrlBase : "/resources/" + configuration.AV_VERSION,
optimize : configuration.resources.optimize,
cacheOptions : configuration.resources.cache,
dirs : dirs
version: configuration.AV_VERSION,
optimize: configuration.resources.optimize,
cacheOptions: configuration.resources.cache,
dirs: dirs
});

if (bundles) {
serveI18nBundles(app, {
resourcesUrlBase : "/resources/" + configuration.AV_VERSION,
contextUrl : configuration.contextUrl,
bundles : bundles
version: configuration.AV_VERSION,
contextUrl: configuration.contextUrl,
bundles: bundles
});
}

Expand All @@ -143,7 +158,7 @@ module.exports = {

serveStaticAssets: serveStaticAssets,
serveI18nBundles: serveI18nBundles,
checkOptimizedDir : checkOptimizedDir,
configure : configure
checkOptimizedDir: checkOptimizedDir,
configure: configure

};
28 changes: 23 additions & 5 deletions test/techStaticSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var sinon = require("sinon");
var logger = require("node-tech-logger");
var techStatic = require("../techStatic");
var path = require("path");

logger.setup({});

Expand All @@ -22,11 +23,11 @@ describe("node-tech-static", function() {
cache: {
ms: 123456
},
resourcesUrlBase: "/resources/1.0",
version: "1.0",
optimize: false,
dirs: [".", "../../node-ui-commons"],
staticMw: function(folder, options) {
return [folder, options];
return [folder.split(path.sep).join("/"), options];
}
};
});
Expand All @@ -51,13 +52,30 @@ describe("node-tech-static", function() {

techStatic.serveStaticAssets(app, opts);

sinon.assert.calledOnce(app.use);
sinon.assert.calledThrice(app.use);

sinon.assert.calledWith(app.use, "/resources/1.0", ["dist/public", {
maxAge: 123456
}]);

});

it("Serves non optimized resources from folders suffixed with '-debug' when using 'debug=true'", function() {

opts.optimize = true;

techStatic.serveStaticAssets(app, opts);

sinon.assert.calledThrice(app.use);

sinon.assert.calledWith(app.use, "/resources-debug/1.0", ["public", {
maxAge: 0
}]);
sinon.assert.calledWith(app.use, "/resources-debug/1.0", ["../../node-ui-commons/public", {
maxAge: 0
}]);
});

it("converts seconds to ms", function() {
opts.cache.seconds = 1;
delete opts.cache.ms;
Expand All @@ -78,7 +96,7 @@ describe("node-tech-static", function() {

techStatic.serveI18nBundles(app, {
bundles: ["Portal", "Error"],
resourcesUrlBase: "/resources/1.0",
version: "1.0",
contextUrl: "/portal"
});

Expand All @@ -91,7 +109,7 @@ describe("node-tech-static", function() {
it("Redirects en to default property files", function() {
techStatic.serveI18nBundles(app, {
bundles: ["Portal"],
resourcesUrlBase: "/resources/1.0",
version: "1.0",
contextUrl: "/portal"
});

Expand Down

0 comments on commit ddecbd0

Please sign in to comment.