implemented static file serving HTTP module

This commit is contained in:
tigerbot 2017-05-09 16:50:07 -06:00
parent bcba0abddc
commit 56113cb100
1 changed files with 37 additions and 1 deletions

View File

@ -1,7 +1,8 @@
'use strict'; 'use strict';
module.exports.create = function (deps, conf) { module.exports.create = function (deps, conf) {
var app = require('express')(); var express = require('express');
var app = express();
var adminApp = require('./admin').create(deps, conf); var adminApp = require('./admin').create(deps, conf);
var domainMatches = require('../match-domain').match; var domainMatches = require('../match-domain').match;
@ -90,6 +91,38 @@ module.exports.create = function (deps, conf) {
}; };
} }
function createStaticRoute(mod) {
var getStaticApp, staticApp;
if (/:hostname/.test(mod.root)) {
staticApp = {};
getStaticApp = function (hostname) {
if (!staticApp[hostname]) {
staticApp[hostname] = express.static(mod.root.replace(':hostname', hostname));
}
return staticApp[hostname];
};
}
else {
staticApp = express.static(mod.root);
getStaticApp = function () {
return staticApp;
};
}
return function (req, res, next) {
var hostname = req.headers.host.split(':')[0];
var relevant = mod.domains.some(function (pattern) {
return domainMatches(pattern, hostname);
});
if (relevant) {
getStaticApp(hostname)(req, res, next);
} else {
next();
}
};
}
app.use(redirectHttps); app.use(redirectHttps);
app.use(handleAdmin); app.use(handleAdmin);
@ -97,6 +130,9 @@ module.exports.create = function (deps, conf) {
if (mod.name === 'proxy') { if (mod.name === 'proxy') {
app.use(createProxyRoute(mod)); app.use(createProxyRoute(mod));
} }
else if (mod.name === 'static') {
app.use(createStaticRoute(mod));
}
else { else {
console.warn('unknown HTTP module', mod); console.warn('unknown HTTP module', mod);
} }