use arrays instead of maps

This commit is contained in:
AJ ONeal 2017-03-13 16:39:43 -06:00
parent 0103ae02d4
commit df8cb8d96f
3 changed files with 201 additions and 68 deletions

View File

@ -48,32 +48,51 @@
<div class="input-group" ng-init="siteconf = vm.config.global"> <div class="input-group" ng-init="siteconf = vm.config.global">
<h1>Global Settings:</h1> <h1>Global Settings:</h1>
<br/> <br/>
<div ng-repeat="(pathname, modules) in siteconf.paths">Pathname: <div ng-repeat="path in siteconf.paths">Pathname:
<input class="form-control" ng-model="pathname" /> <input class="form-control" ng-model="path.$id" />
<div ng-repeat="(modulename, module) in modules">Modulename: {{modulename}} <div ng-repeat="module in path.modules">Modulename: {{module.$id}}
<div ng-repeat="target in modules">Target: <div ng-repeat="(key, value) in module">{{key}}:
<input class="form-control" ng-model="target" /> <input class="form-control" ng-model="value" />
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<h1>Per-Domain Settings:</h1> <h1>Per-Domain Settings:</h1>
<div class="input-group" ng-repeat="(sitename, siteconf) in vm.config.sites"> <div class="input-group" ng-repeat="siteconf in vm.config.sites">
<label>Hostname:</label> <input class="form-control" ng-model="sitename" /> <label>Hostname:</label> <input class="form-control" ng-model="sitename" />
<br/> <br/>
<div ng-repeat="(pathname, modules) in siteconf.paths">Pathname: <div ng-repeat="path in siteconf.paths">Pathname:
<input class="form-control" ng-model="pathname" /> <input class="form-control" ng-model="path.$id" />
<div ng-repeat="(modulename, module) in modules">Modulename: {{modulename}} <div ng-repeat="module in path.modules">Modulename: {{module.$id}}
<div ng-repeat="target in modules">Target: <div ng-repeat="(key, value) in module">{{key}}:
<input class="form-control" ng-model="target" /> <input class="form-control" ng-model="value" />
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div class="input-group" ng-init="defaultsconf = vm.config.defaults">
<h1>Fallback Settings:</h1>
<br/>
<div ng-repeat="path in defaultsconf.paths">Pathname:
<input class="form-control" ng-model="path.$id" />
<div ng-repeat="module in path.modules">Modulename: {{module.$id}}
<div ng-repeat="(key, value) in module">{{key}}:
<input class="form-control" ng-model="value" />
</div>
</div>
</div>
</div>
<div ng-if="!vm._showvmconfig">
<button class="btn-link" ng-click="vm._showvmconfig = true">show config as json</button>
</div>
<div ng-if="vm._showvmconfig">
<button class="btn-link" ng-click="vm._showvmconfig = false">hide config</button>
<pre><code ng-bind="vm.config | json">{{vm.config}}</code></pre> <pre><code ng-bind="vm.config | json">{{vm.config}}</code></pre>
</div>
</div>
</div> </div>

View File

@ -94,20 +94,10 @@ function createServer(port, _delete_me_, content, opts) {
var app = require('../lib/app.js'); var app = require('../lib/app.js');
var directive = { var directive = {
content: content global: opts.global
, livereload: opts.livereload
, global: {
greenlock: { email: opts.email, tos: opts.tos }
, rvpn: { email: opts.email, tos: opts.tos }
, paths: {
'/assets/': { serve: [ opts.assetsPath ] }
// TODO figure this b out
, '/.well-known/': { serve: [ path.resolve(opts.assetsPath, 'well-known') ] }
, '/': { serve: [ opts.webRoot ], indexes: [ opts.webRoot ] }
}
}
, sites: opts.sites , sites: opts.sites
, expressApp: opts.expressApp , defaults: opts.defaults
, cwd: process.cwd()
}; };
var server; var server;
var insecureServer; var insecureServer;
@ -260,13 +250,15 @@ function run() {
var argv = minimist(process.argv.slice(2)); var argv = minimist(process.argv.slice(2));
var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort; var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort;
var livereload = argv.livereload; var livereload = argv.livereload;
var defaultWebRoot = path.resolve(argv['default-web-root'] || argv.d || argv._[1] || process.cwd()); var defaultWebRoot = path.normalize(argv['default-web-root'] || argv.d || argv._[1] || '.');
var assetsPath = path.join(__dirname, '..', 'packages', 'assets');
var content = argv.c; var content = argv.c;
var letsencryptHost = argv['letsencrypt-certs']; var letsencryptHost = argv['letsencrypt-certs'];
var yaml = require('js-yaml'); var yaml = require('js-yaml');
var fs = PromiseA.promisifyAll(require('fs')); var fs = PromiseA.promisifyAll(require('fs'));
var configFile = argv.c || argv.conf || argv.config; var configFile = argv.c || argv.conf || argv.config;
var config; var config;
console.log('defaultWebRoot', defaultWebRoot);
try { try {
config = fs.readFileSync(configFile || 'Goldilocks.yml'); config = fs.readFileSync(configFile || 'Goldilocks.yml');
@ -383,7 +375,8 @@ function run() {
opts.cwd = process.cwd(); opts.cwd = process.cwd();
opts.sites = {}; opts.sites = [];
opts.sites._map = {};
if (argv.sites) { if (argv.sites) {
opts._externalHost = false; opts._externalHost = false;
@ -394,48 +387,86 @@ function run() {
opts._externalHost = opts._externalHost || !/(^|\.)localhost\./.test(servername); opts._externalHost = opts._externalHost || !/(^|\.)localhost\./.test(servername);
// TODO allow reverse proxy // TODO allow reverse proxy
if (!opts.sites[servername]) { if (!opts.sites._map[servername]) {
opts.sites[servername] = { paths: {} }; opts.sites._map[servername] = { $id: servername, paths: [] };
opts.sites._map[servername].paths._map = {};
opts.sites.push(opts.sites._map[servername]);
} }
if (!nameparts.length) { if (!nameparts.length) {
return; return;
} }
if (!opts.sites[servername].paths['/']) { if (!opts.sites._map[servername].paths._map['/']) {
opts.sites[servername].paths['/'] = {}; opts.sites._map[servername].paths._map['/'] = { $id: '/', modules: [] };
opts.sites._map[servername].paths.push(opts.sites._map[servername].paths._map['/']);
} }
modules = opts.sites[servername].paths['/']; modules = opts.sites._map[servername].paths._map['/'].modules;
modules.serve = nameparts; modules.push({
modules.indexes = nameparts; $id: 'serve'
, paths: nameparts
});
modules.push({
$id: 'indexes'
, paths: nameparts
});
}); });
} }
opts.groups = {}; opts.groups = [];
// 'packages', 'assets', 'com.daplie.caddy' // 'packages', 'assets', 'com.daplie.caddy'
opts.sites['localhost.alpha.daplie.me'] = { opts.global = {
modules: [ // TODO uh-oh we've got a mixed bag of modules (various types), a true map
{ $id: 'greenlock', email: opts.email, tos: opts.tos }
, { $id: 'rvpn', email: opts.email, tos: opts.tos }
, { $id: 'content', content: content }
, { $id: 'livereload', on: opts.livereload }
, { $id: 'app', path: opts.expressApp }
]
, paths: [
{ $id: '/assets/', modules: [ { $id: 'serve', paths: [ assetsPath ] } ] }
// TODO figure this b out
, { $id: '/.well-known/', modules: [
{ $id: 'serve', paths: [ path.join(assetsPath, 'well-known') ] }
] }
]
};
opts.defaults = {
modules: []
, paths: [
{ $id: '/', modules: [
{ $id: 'serve', paths: [ defaultWebRoot ] }
, { $id: 'indexes', paths: [ defaultWebRoot ] }
] }
]
};
opts.sites.push({
// greenlock: {} // greenlock: {}
paths: { $id: 'localhost.alpha.daplie.me'
'/': { serve: [ path.resolve(__dirname, '..', 'admin', 'public') ] } , paths: [
, '/api/': { app: path.join(__dirname, 'admin') } { $id: '/', modules: [
} { $id: 'serve', paths: [ path.resolve(__dirname, '..', 'admin', 'public') ] }
}; ] }
opts.sites['localhost.daplie.invalid'] = { , { $id: '/api/', modules: [
paths: { { $id: 'app', path: path.join(__dirname, 'admin') }
'/': { serve: [ path.resolve(__dirname, '..', 'admin', 'public') ] } ] }
, '/api/': { app: path.join(__dirname, 'admin') } ]
} });
}; opts.sites.push({
opts.assetsPath = path.join(__dirname, '..', 'packages', 'assets'); $id: 'localhost.daplie.invalid'
opts.webRoot = defaultWebRoot; , paths: [
{ $id: '/', modules: [ { $id: 'serve', paths: [ path.resolve(__dirname, '..', 'admin', 'public') ] } ] }
, { $id: '/api/', modules: [ { $id: 'app', path: path.join(__dirname, 'admin') } ] }
]
});
// ifaces // ifaces
opts.ifaces = require('../lib/local-ip.js').find(); opts.ifaces = require('../lib/local-ip.js').find();
// TODO use arrays in all things // TODO use arrays in all things
opts._old_server_name = Object.keys(opts.sites)[0]; opts._old_server_name = opts.sites[0].$id;
opts.pubdir = defaultWebRoot.replace(/(:hostname|:servername).*/, ''); opts.pubdir = defaultWebRoot.replace(/(:hostname|:servername).*/, '');
if (argv.p || argv.port || argv._[0]) { if (argv.p || argv.port || argv._[0]) {
@ -454,7 +485,7 @@ function run() {
opts.livereload = livereload; opts.livereload = livereload;
if (argv['express-app']) { if (argv['express-app']) {
opts.expressApp = require(path.resolve(process.cwd(), argv['express-app'])); opts.expressApp = require(argv['express-app']);
} }
if (opts.email || opts._externalHost) { if (opts.email || opts._externalHost) {

View File

@ -81,6 +81,69 @@ module.exports = function (opts) {
app = express(); app = express();
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);
});
return app.use('/', function (req, res, next) { return app.use('/', function (req, res, next) {
if (!req.headers.host) { if (!req.headers.host) {
next(new Error('missing HTTP Host header')); next(new Error('missing HTTP Host header'));
@ -112,21 +175,22 @@ module.exports = function (opts) {
var hostname = (host||'').split(':')[0].toLowerCase(); var hostname = (host||'').split(':')[0].toLowerCase();
console.log('opts.global', opts.global); console.log('opts.global', opts.global);
var sites = [ opts.global || {}, opts.sites[hostname] || {}, opts.defer || {} ]; var sites = [ opts.global || null, opts.sites._map[hostname] || null, opts.defaults || null ];
var loadables = { var loadables = {
serve: function (config, hostname, pathname, req, res, next) { serve: function (config, hostname, pathname, req, res, next) {
config = config.slice(0);
var originalUrl = req.url; var originalUrl = req.url;
var dirpaths = config.paths.slice(0);
function nextServe() { function nextServe() {
var dirname = config.pop(); var dirname = dirpaths.pop();
console.log('[serve]', req.url, hostname, pathname, dirname);
if (!dirname) { if (!dirname) {
req.url = originalUrl; req.url = originalUrl;
next(); next();
return; return;
} }
console.log('[serve]', req.url, hostname, pathname, dirname);
dirname = path.resolve(opts.cwd, dirname.replace(/:hostname/, hostname));
if (!serveStaticMap[dirname]) { if (!serveStaticMap[dirname]) {
serveStaticMap[dirname] = serveStatic(dirname); serveStaticMap[dirname] = serveStatic(dirname);
} }
@ -138,18 +202,19 @@ module.exports = function (opts) {
nextServe(); nextServe();
} }
, indexes: function (config, hostname, pathname, req, res, next) { , indexes: function (config, hostname, pathname, req, res, next) {
config = config.slice(0);
var originalUrl = req.url; var originalUrl = req.url;
var dirpaths = config.paths.slice(0);
function nextIndex() { function nextIndex() {
var dirname = config.pop(); var dirname = dirpaths.pop();
console.log('[indexes]', req.url, hostname, pathname, dirname);
if (!dirname) { if (!dirname) {
req.url = originalUrl; req.url = originalUrl;
next(); next();
return; return;
} }
console.log('[indexes]', req.url, hostname, pathname, dirname);
dirname = path.resolve(opts.cwd, dirname.replace(/:hostname/, hostname));
if (!serveStaticMap[dirname]) { if (!serveStaticMap[dirname]) {
serveIndexMap[dirname] = serveIndex(dirname); serveIndexMap[dirname] = serveIndex(dirname);
} }
@ -159,18 +224,26 @@ module.exports = function (opts) {
req.url = req.url.substr(pathname.length - 1); req.url = req.url.substr(pathname.length - 1);
nextIndex(); nextIndex();
} }
, 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);
}
}; };
function runModule(config, hostname, pathname, modulename, req, res, next) { function runModule(module, hostname, pathname, modulename, req, res, next) {
if (!loadables[modulename]) { if (!loadables[modulename]) {
next(new Error("no module '" + modulename + "' found")); next(new Error("no module '" + modulename + "' found"));
return; return;
} }
loadables[modulename](config, hostname, pathname, req, res, next); loadables[modulename](module, hostname, pathname, req, res, next);
} }
function iterModules(modules, hostname, pathname, req, res, next) { function iterModules(modules, hostname, pathname, req, res, next) {
var modulenames = Object.keys(modules); console.log('modules');
console.log(modules);
var modulenames = Object.keys(modules._map);
function nextModule() { function nextModule() {
var modulename = modulenames.pop(); var modulename = modulenames.pop();
@ -180,14 +253,17 @@ module.exports = function (opts) {
} }
console.log('modules', modules); console.log('modules', modules);
runModule(modules[modulename], hostname, pathname, modulename, req, res, nextModule); runModule(modules._map[modulename], hostname, pathname, modulename, req, res, nextModule);
} }
nextModule(); nextModule();
} }
function iterPaths(site, hostname, req, res, next) { function iterPaths(site, hostname, req, res, next) {
var pathnames = Object.keys(site.paths || {}); console.log('site', hostname);
console.log(site);
var pathnames = Object.keys(site.paths._map);
console.log('pathnames', pathnames);
pathnames = pathnames.filter(function (pathname) { pathnames = pathnames.filter(function (pathname) {
// TODO ensure that pathname has trailing / // TODO ensure that pathname has trailing /
return (0 === req.url.indexOf(pathname)); return (0 === req.url.indexOf(pathname));
@ -196,27 +272,34 @@ module.exports = function (opts) {
pathnames.sort(function (a, b) { pathnames.sort(function (a, b) {
return b.length - a.length; return b.length - a.length;
}); });
console.log('pathnames', pathnames);
function nextPath() { function nextPath() {
var pathname = pathnames.pop(); var pathname = pathnames.shift();
if (!pathname) { if (!pathname) {
next(); next();
return; return;
} }
console.log('iterPaths', hostname, pathname, req.url); console.log('iterPaths', hostname, pathname, req.url);
iterModules(site.paths[pathname], hostname, pathname, req, res, nextPath); iterModules(site.paths._map[pathname].modules, hostname, pathname, req, res, nextPath);
} }
nextPath(); nextPath();
} }
function nextSite() { function nextSite() {
var site = sites.pop(); console.log('hostname', hostname, sites);
if (!site) { var site;
if (!sites.length) {
next(); // 404 next(); // 404
return; return;
} }
site = sites.shift();
if (!site) {
nextSite();
return;
}
iterPaths(site, hostname, req, res, nextSite); iterPaths(site, hostname, req, res, nextSite);
} }