2015-06-24 21:36:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = function (opts) {
|
|
|
|
var finalhandler = require('finalhandler');
|
|
|
|
var serveStatic = require('serve-static');
|
|
|
|
var serveIndex = require('serve-index');
|
2017-02-01 22:47:08 +00:00
|
|
|
|
|
|
|
var hostsMap = {};
|
|
|
|
var pathsMap = {};
|
2015-06-30 23:11:01 +00:00
|
|
|
var content = opts.content;
|
2017-02-01 22:47:08 +00:00
|
|
|
var server;
|
|
|
|
|
|
|
|
function addServer(hostname) {
|
|
|
|
console.log('add server:', hostname);
|
|
|
|
|
|
|
|
if (hostsMap[hostname]) {
|
|
|
|
return hostsMap[hostname];
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp = { };
|
|
|
|
|
|
|
|
opts.sites.forEach(function (site) {
|
|
|
|
if (hostname !== site.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('add server for reals', tmp);
|
|
|
|
|
|
|
|
site.path = site.path || site.paths[0] || '.';
|
|
|
|
|
|
|
|
if (!pathsMap[site.path]) {
|
|
|
|
pathsMap[site.path] = {
|
|
|
|
serve: serveStatic(site.path)
|
|
|
|
// TODO option for dotfiles
|
|
|
|
, index: serveIndex(site.path)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
hostsMap[hostname] = {
|
|
|
|
serve: pathsMap[site.path].serve
|
|
|
|
, index: pathsMap[site.path].index
|
|
|
|
, app: site.app
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2015-06-24 21:36:17 +00:00
|
|
|
|
2016-10-07 19:12:46 +00:00
|
|
|
function _reloadWrite(data, enc, cb) {
|
|
|
|
/*jshint validthis: true */
|
|
|
|
if (this.headersSent) {
|
|
|
|
this.__write(data, enc, cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!/html/i.test(this.getHeader('Content-Type'))) {
|
|
|
|
this.__write(data, enc, cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.getHeader('Content-Length')) {
|
|
|
|
this.setHeader('Content-Length', this.getHeader('Content-Length') + this.__my_addLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__write(this.__my_livereload);
|
|
|
|
this.__write(data, enc, cb);
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:47:08 +00:00
|
|
|
|
|
|
|
opts.servername = opts.servername || opts.sites[0].name;
|
|
|
|
|
|
|
|
addServer(opts.sites[0].name);
|
|
|
|
|
2015-06-24 21:36:17 +00:00
|
|
|
return function (req, res) {
|
2015-06-30 23:11:01 +00:00
|
|
|
if (content && '/' === req.url) {
|
|
|
|
// res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
res.end(content);
|
|
|
|
return;
|
|
|
|
}
|
2015-06-24 21:36:17 +00:00
|
|
|
var done = finalhandler(req, res);
|
2017-02-01 22:47:08 +00:00
|
|
|
var host = req.headers.host;
|
|
|
|
var hostname = (host||'').split(':')[0] || opts.servername;
|
|
|
|
|
|
|
|
function serveStatic(server) {
|
|
|
|
if (server.expressApp) {
|
|
|
|
server.expressApp(req, res, serveStatic);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.serve(req, res, function (err) {
|
|
|
|
if (err) { return done(err); }
|
|
|
|
server.index(req, res, done);
|
|
|
|
});
|
|
|
|
}
|
2015-12-06 06:43:33 +00:00
|
|
|
|
|
|
|
if (opts.livereload) {
|
2016-10-07 19:12:46 +00:00
|
|
|
res.__my_livereload = '<script src="//'
|
2017-02-01 22:47:08 +00:00
|
|
|
+ (host || opts.servername).split(':')[0]
|
2015-12-06 06:43:33 +00:00
|
|
|
+ ':35729/livereload.js?snipver=1"></script>';
|
2016-10-07 19:12:46 +00:00
|
|
|
res.__my_addLen = res.__my_livereload.length;
|
2015-12-06 06:43:33 +00:00
|
|
|
|
2016-10-07 19:12:46 +00:00
|
|
|
// TODO modify prototype instead of each instance?
|
|
|
|
res.__write = res.write;
|
|
|
|
res.write = _reloadWrite;
|
|
|
|
}
|
2016-08-06 20:15:12 +00:00
|
|
|
|
2017-02-01 22:47:08 +00:00
|
|
|
console.log('hostname:', hostname);
|
|
|
|
|
|
|
|
addServer(hostname);
|
|
|
|
server = hostsMap[hostname] || hostsMap[opts.sites[0].name];
|
|
|
|
serveStatic(server);
|
2016-08-06 20:15:12 +00:00
|
|
|
|
2015-06-24 21:36:17 +00:00
|
|
|
};
|
|
|
|
};
|