2015-06-24 21:36:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = function (opts) {
|
2017-03-02 07:58:45 +00:00
|
|
|
var express = require('express');
|
|
|
|
//var finalhandler = require('finalhandler');
|
2015-06-24 21:36:17 +00:00
|
|
|
var serveStatic = require('serve-static');
|
|
|
|
var serveIndex = require('serve-index');
|
2017-03-02 07:58:45 +00:00
|
|
|
//var assetServer = serveStatic(opts.assetsPath);
|
2017-02-23 01:48:34 +00:00
|
|
|
var path = require('path');
|
2017-03-02 07:58:45 +00:00
|
|
|
//var wellKnownServer = serveStatic(path.join(opts.assetsPath, 'well-known'));
|
2017-02-01 22:47:08 +00:00
|
|
|
|
2017-03-02 07:58:45 +00:00
|
|
|
var serveStaticMap = {};
|
|
|
|
var serveIndexMap = {};
|
2015-06-30 23:11:01 +00:00
|
|
|
var content = opts.content;
|
2017-03-02 07:58:45 +00:00
|
|
|
//var server;
|
2017-02-28 21:55:48 +00:00
|
|
|
var serveInit;
|
2017-03-02 07:58:45 +00:00
|
|
|
var 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
|
|
|
|
2017-03-02 07:58:45 +00:00
|
|
|
function createServeInit() {
|
|
|
|
var PromiseA = require('bluebird');
|
|
|
|
var fs = PromiseA.promisifyAll(require('fs'));
|
|
|
|
var ownersPath = path.join(__dirname, '..', 'var', 'owners.json');
|
2017-02-01 22:47:08 +00:00
|
|
|
|
2017-03-02 07:58:45 +00:00
|
|
|
return require('../packages/apis/com.daplie.caddy').create({
|
|
|
|
PromiseA: PromiseA
|
|
|
|
, storage: {
|
|
|
|
owners: {
|
|
|
|
all: function () {
|
|
|
|
var owners;
|
|
|
|
try {
|
|
|
|
owners = require(ownersPath);
|
|
|
|
} catch(e) {
|
|
|
|
owners = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseA.resolve(Object.keys(owners).map(function (key) {
|
|
|
|
var owner = owners[key];
|
|
|
|
owner.id = key;
|
|
|
|
return owner;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
, set: function (id, obj) {
|
|
|
|
var owners;
|
|
|
|
try {
|
|
|
|
owners = require(ownersPath);
|
|
|
|
} catch(e) {
|
|
|
|
owners = {};
|
|
|
|
}
|
|
|
|
obj.id = id;
|
|
|
|
owners[id] = obj;
|
|
|
|
|
|
|
|
return fs.writeFileAsync(ownersPath, JSON.stringify(owners), 'utf8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, recase: require('recase').create({})
|
|
|
|
, options: opts
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
app = express();
|
|
|
|
|
2017-03-13 22:39:43 +00:00
|
|
|
if (!opts.sites) {
|
|
|
|
opts.sites = [];
|
|
|
|
}
|
|
|
|
opts.sites._map = {};
|
|
|
|
opts.sites.forEach(function (site) {
|
|
|
|
|
|
|
|
if (!opts.sites._map[site.$id]) {
|
|
|
|
opts.sites._map[site.$id] = site;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!site.paths) {
|
|
|
|
site.paths = [];
|
|
|
|
}
|
|
|
|
if (!site.paths._map) {
|
|
|
|
site.paths._map = {};
|
|
|
|
}
|
|
|
|
site.paths.forEach(function (path) {
|
|
|
|
|
|
|
|
site.paths._map[path.$id] = path;
|
|
|
|
|
|
|
|
if (!path.modules) {
|
|
|
|
path.modules = [];
|
|
|
|
}
|
|
|
|
if (!path.modules._map) {
|
|
|
|
path.modules._map = {};
|
|
|
|
}
|
|
|
|
path.modules.forEach(function (module) {
|
|
|
|
|
|
|
|
path.modules._map[module.$id] = module;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function mapMap(el, i, arr) {
|
|
|
|
arr._map[el.$id] = el;
|
|
|
|
}
|
|
|
|
opts.global.modules._map = {};
|
|
|
|
opts.global.modules.forEach(mapMap);
|
|
|
|
opts.global.paths._map = {};
|
|
|
|
opts.global.paths.forEach(function (path, i, arr) {
|
|
|
|
mapMap(path, i, arr);
|
|
|
|
//opts.global.paths._map[path.$id] = path;
|
|
|
|
path.modules._map = {};
|
|
|
|
path.modules.forEach(mapMap);
|
|
|
|
});
|
|
|
|
opts.sites.forEach(function (site) {
|
|
|
|
site.paths._map = {};
|
|
|
|
site.paths.forEach(function (path, i, arr) {
|
|
|
|
mapMap(path, i, arr);
|
|
|
|
//site.paths._map[path.$id] = path;
|
|
|
|
path.modules._map = {};
|
|
|
|
path.modules.forEach(mapMap);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
opts.defaults.modules._map = {};
|
|
|
|
opts.defaults.modules.forEach(mapMap);
|
|
|
|
opts.defaults.paths._map = {};
|
|
|
|
opts.defaults.paths.forEach(function (path, i, arr) {
|
|
|
|
mapMap(path, i, arr);
|
|
|
|
//opts.global.paths._map[path.$id] = path;
|
|
|
|
path.modules._map = {};
|
|
|
|
path.modules.forEach(mapMap);
|
|
|
|
});
|
2017-03-02 07:58:45 +00:00
|
|
|
return app.use('/', function (req, res, next) {
|
|
|
|
if (!req.headers.host) {
|
|
|
|
next(new Error('missing HTTP Host header'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 === req.url.indexOf('/api/com.daplie.caddy/')) {
|
2017-02-28 21:55:48 +00:00
|
|
|
if (!serveInit) {
|
2017-03-02 07:58:45 +00:00
|
|
|
serveInit = createServeInit();
|
2017-02-28 21:55:48 +00:00
|
|
|
}
|
2017-03-02 07:58:45 +00:00
|
|
|
}
|
|
|
|
if ('/api/com.daplie.caddy/init' === req.url) {
|
|
|
|
serveInit.init(req, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ('/api/com.daplie.caddy/config' === req.url) {
|
|
|
|
serveInit.config(req, res);
|
2017-02-28 21:55:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-30 23:11:01 +00:00
|
|
|
if (content && '/' === req.url) {
|
|
|
|
// res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
res.end(content);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 07:58:45 +00:00
|
|
|
|
|
|
|
//var done = finalhandler(req, res);
|
2017-02-01 22:47:08 +00:00
|
|
|
var host = req.headers.host;
|
2017-03-02 07:58:45 +00:00
|
|
|
var hostname = (host||'').split(':')[0].toLowerCase();
|
|
|
|
|
|
|
|
console.log('opts.global', opts.global);
|
2017-03-13 22:39:43 +00:00
|
|
|
var sites = [ opts.global || null, opts.sites._map[hostname] || null, opts.defaults || null ];
|
2017-03-02 07:58:45 +00:00
|
|
|
var loadables = {
|
|
|
|
serve: function (config, hostname, pathname, req, res, next) {
|
|
|
|
var originalUrl = req.url;
|
2017-03-13 22:39:43 +00:00
|
|
|
var dirpaths = config.paths.slice(0);
|
2017-02-01 22:47:08 +00:00
|
|
|
|
2017-03-02 07:58:45 +00:00
|
|
|
function nextServe() {
|
2017-03-13 22:39:43 +00:00
|
|
|
var dirname = dirpaths.pop();
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!dirname) {
|
|
|
|
req.url = originalUrl;
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('[serve]', req.url, hostname, pathname, dirname);
|
|
|
|
dirname = path.resolve(opts.cwd, dirname.replace(/:hostname/, hostname));
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!serveStaticMap[dirname]) {
|
|
|
|
serveStaticMap[dirname] = serveStatic(dirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
serveStaticMap[dirname](req, res, nextServe);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.url = req.url.substr(pathname.length - 1);
|
|
|
|
nextServe();
|
|
|
|
}
|
|
|
|
, indexes: function (config, hostname, pathname, req, res, next) {
|
|
|
|
var originalUrl = req.url;
|
2017-03-13 22:39:43 +00:00
|
|
|
var dirpaths = config.paths.slice(0);
|
2017-03-02 07:58:45 +00:00
|
|
|
|
|
|
|
function nextIndex() {
|
2017-03-13 22:39:43 +00:00
|
|
|
var dirname = dirpaths.pop();
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!dirname) {
|
|
|
|
req.url = originalUrl;
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('[indexes]', req.url, hostname, pathname, dirname);
|
|
|
|
dirname = path.resolve(opts.cwd, dirname.replace(/:hostname/, hostname));
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!serveStaticMap[dirname]) {
|
|
|
|
serveIndexMap[dirname] = serveIndex(dirname);
|
|
|
|
}
|
|
|
|
serveIndexMap[dirname](req, res, nextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.url = req.url.substr(pathname.length - 1);
|
|
|
|
nextIndex();
|
|
|
|
}
|
2017-03-13 22:39:43 +00:00
|
|
|
, app: function (config, hostname, pathname, req, res, next) {
|
|
|
|
//var appfile = path.resolve(/*process.cwd(), */config.path.replace(/:hostname/, hostname));
|
|
|
|
var appfile = config.path.replace(/:hostname/, hostname);
|
|
|
|
var app = require(appfile);
|
|
|
|
app(req, res, next);
|
|
|
|
}
|
2017-03-02 07:58:45 +00:00
|
|
|
};
|
|
|
|
|
2017-03-13 22:39:43 +00:00
|
|
|
function runModule(module, hostname, pathname, modulename, req, res, next) {
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!loadables[modulename]) {
|
|
|
|
next(new Error("no module '" + modulename + "' found"));
|
|
|
|
return;
|
|
|
|
}
|
2017-03-13 22:39:43 +00:00
|
|
|
loadables[modulename](module, hostname, pathname, req, res, next);
|
2017-03-02 07:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function iterModules(modules, hostname, pathname, req, res, next) {
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('modules');
|
|
|
|
console.log(modules);
|
|
|
|
var modulenames = Object.keys(modules._map);
|
2017-03-02 07:58:45 +00:00
|
|
|
|
|
|
|
function nextModule() {
|
|
|
|
var modulename = modulenames.pop();
|
|
|
|
if (!modulename) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('modules', modules);
|
2017-03-13 22:39:43 +00:00
|
|
|
runModule(modules._map[modulename], hostname, pathname, modulename, req, res, nextModule);
|
2017-03-02 07:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextModule();
|
|
|
|
}
|
|
|
|
|
|
|
|
function iterPaths(site, hostname, req, res, next) {
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('site', hostname);
|
|
|
|
console.log(site);
|
|
|
|
var pathnames = Object.keys(site.paths._map);
|
|
|
|
console.log('pathnames', pathnames);
|
2017-03-02 07:58:45 +00:00
|
|
|
pathnames = pathnames.filter(function (pathname) {
|
|
|
|
// TODO ensure that pathname has trailing /
|
|
|
|
return (0 === req.url.indexOf(pathname));
|
|
|
|
//return req.url.match(pathname);
|
|
|
|
});
|
|
|
|
pathnames.sort(function (a, b) {
|
|
|
|
return b.length - a.length;
|
|
|
|
});
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('pathnames', pathnames);
|
2017-03-02 07:58:45 +00:00
|
|
|
|
|
|
|
function nextPath() {
|
2017-03-13 22:39:43 +00:00
|
|
|
var pathname = pathnames.shift();
|
2017-03-02 07:58:45 +00:00
|
|
|
if (!pathname) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('iterPaths', hostname, pathname, req.url);
|
2017-03-13 22:39:43 +00:00
|
|
|
iterModules(site.paths._map[pathname].modules, hostname, pathname, req, res, nextPath);
|
2017-03-02 07:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextSite() {
|
2017-03-13 22:39:43 +00:00
|
|
|
console.log('hostname', hostname, sites);
|
|
|
|
var site;
|
|
|
|
if (!sites.length) {
|
2017-03-02 07:58:45 +00:00
|
|
|
next(); // 404
|
|
|
|
return;
|
|
|
|
}
|
2017-03-13 22:39:43 +00:00
|
|
|
site = sites.shift();
|
|
|
|
if (!site) {
|
|
|
|
nextSite();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 07:58:45 +00:00
|
|
|
iterPaths(site, hostname, req, res, nextSite);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextSite();
|
|
|
|
|
|
|
|
/*
|
2017-02-23 01:48:34 +00:00
|
|
|
function serveStaticly(server) {
|
|
|
|
function serveTheStatic() {
|
|
|
|
server.serve(req, res, function (err) {
|
|
|
|
if (err) { return done(err); }
|
|
|
|
server.index(req, res, function (err) {
|
|
|
|
if (err) { return done(err); }
|
|
|
|
req.url = req.url.replace(/\/assets/, '');
|
|
|
|
assetServer(req, res, function () {
|
|
|
|
if (err) { return done(err); }
|
|
|
|
req.url = req.url.replace(/\/\.well-known/, '');
|
|
|
|
wellKnownServer(req, res, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:47:08 +00:00
|
|
|
if (server.expressApp) {
|
2017-02-23 01:48:34 +00:00
|
|
|
server.expressApp(req, res, serveTheStatic);
|
2017-02-01 22:47:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-23 01:48:34 +00:00
|
|
|
serveTheStatic();
|
2017-02-01 22:47:08 +00:00
|
|
|
}
|
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-02 03:00:00 +00:00
|
|
|
+ (host || opts.sites[0].name).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-23 01:48:34 +00:00
|
|
|
console.log('hostname:', hostname, opts.sites[0].paths);
|
2017-02-01 22:47:08 +00:00
|
|
|
|
|
|
|
addServer(hostname);
|
|
|
|
server = hostsMap[hostname] || hostsMap[opts.sites[0].name];
|
2017-02-23 01:48:34 +00:00
|
|
|
serveStaticly(server);
|
2017-03-02 07:58:45 +00:00
|
|
|
*/
|
|
|
|
});
|
2015-06-24 21:36:17 +00:00
|
|
|
};
|