Skip to content

Commit

Permalink
feat(serveStatic): allow mapping single or multiple routes to a direc…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
shakyShane committed Sep 29, 2016
1 parent cc2f4d9 commit 97dd907
Showing 1 changed file with 96 additions and 23 deletions.
119 changes: 96 additions & 23 deletions lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var Map = require("immutable").Map;
var snippet = require("./../snippet").utils;
var _ = require("./../../lodash.custom");
var serveStatic = require("serve-static");
var logger = require("../logger");

var utils = {
/**
Expand Down Expand Up @@ -82,30 +83,23 @@ var utils = {
})
}

if (options.get('serveStatic')) {
options
.get("serveStatic")
.forEach(function (dir, i) {
if (Immutable.Map.isMap(dir)) {
var ssOptions = (function () {
if (dir.get('options')) {
return dir.get('options').toJS();
}
return {}
})();
defaultMiddlewares.push({
id: "Serve static " + i,
route: dir.get("route"),
handle: serveStatic(dir.get("dir"), ssOptions)
});
} else if (_.isString(dir)) {
defaultMiddlewares.push({
id: "Serve static " + i,
route: "",
handle: serveStatic(dir, options.get('serveStaticOptions').toJS())
});
}
if (options.get("serveStatic")) {

var ssMiddlewares = utils.getServeStaticMiddlewares(options.get("serveStatic"), options.get("serveStaticOptions", Immutable.Map({})).toJS());
var withErrors = ssMiddlewares.filter(function(x) { return x.errors.length > 0 });
var withoutErrors = ssMiddlewares.filter(function(x) { return x.errors.length === 0 });

if (withErrors.size) {
withErrors.forEach(function (item) {
logger.logger.error("{red:Warning!} %s", item.errors[0].data.message);
});
}

if (withoutErrors.size) {
withoutErrors.forEach(function (item) {
defaultMiddlewares.push.apply(defaultMiddlewares, item.items);
});
}
}

/**
Expand Down Expand Up @@ -175,6 +169,85 @@ var utils = {
res.setHeader("Access-Control-Allow-Credentials", true);
next();
}
},
getServeStaticMiddlewares: function (ssOption, serveStaticOptions) {

return ssOption.map(function (dir, i) {

if (Immutable.Map.isMap(dir)) {

var ssOptions = (function () {
if (dir.get("options")) {
return dir.get("options").toJS();
}
return {}
})();

var route = dir.get("route");
var _dir = dir.get("dir");

if (!isValidOption(route) || !isValidOption(_dir)) {
return {
items: [],
errors: [{
type: "Invalid Object",
data: {
message: "Serve Static requires both 'route' and 'dir' options when using an Object"
}
}]
}
}
var ssItems = (function () {
if (_.isString(route)) {
return [{
id: "Serve static " + i,
route: getRoute(route),
handle: serveStatic(_dir, ssOptions)
}]
}
return route.map(function (item, j) {
return {
id: "Serve static " + i + "." + j,
route: getRoute(item),
handle: serveStatic(_dir, ssOptions)
}
}).toJS()
})();
return {
items: ssItems,
errors: []
};
}

if (_.isString(dir)) {
return {
items: [
{
id: "Serve static " + i,
route: "",
handle: serveStatic(dir, serveStaticOptions)
}
],
errors: []
}
}

return {
items: [],
errors: [{
type: "Invalid Type",
data: {
message: "Only strings and Objects (with route+dir) are supported for the ServeStatic option"
}
}]
}
});
function isValidOption (x) {
return _.isString(x) || (_.isArray(x) && x.length > 0) || (Immutable.List.isList(x) && x.size > 0);
}
function getRoute (x) {
return x[0] === "/" ? x : "/" + x;
}
}
};

Expand Down

0 comments on commit 97dd907

Please sign in to comment.