From 4b470ffe51f10bda7c927babef3ec671363801b7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 27 Apr 2017 16:50:03 -0600 Subject: [PATCH] admin page now loads properly --- bin/goldilocks.js | 30 +++++++++++++++++++++++++ lib/app.js | 10 +++++++-- lib/modules/admin.js | 8 ++++--- packages/apis/com.daplie.caddy/index.js | 2 +- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/bin/goldilocks.js b/bin/goldilocks.js index ed618ce..6d7a9ed 100755 --- a/bin/goldilocks.js +++ b/bin/goldilocks.js @@ -86,6 +86,36 @@ function readConfigAndRun(args) { config.cwd = process.cwd(); } + var ipaddr = require('ipaddr.js'); + var addresses = []; + var ifaces = require('../lib/local-ip.js').find(); + + Object.keys(ifaces).forEach(function (ifacename) { + var iface = ifaces[ifacename]; + iface.ipv4.forEach(function (ip) { + addresses.push(ip); + }); + iface.ipv6.forEach(function (ip) { + addresses.push(ip); + }); + }); + + addresses.sort(function (a, b) { + if (a.family !== b.family) { + return 'IPv4' === a.family ? 1 : -1; + } + + return a.address > b.address ? 1 : -1; + }); + + addresses.forEach(function (addr) { + addr.range = ipaddr.parse(addr.address).range(); + }); + + // TODO maybe move to config.state.addresses (?) + config.addresses = addresses; + config.device = { hostname: 'TODO: fetch hostname from device and from ip and try to make a match' }; + if (config.tcp.ports) { run(config); return; diff --git a/lib/app.js b/lib/app.js index 57dbaf1..7f89cf8 100644 --- a/lib/app.js +++ b/lib/app.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = function (deps, conf) { +module.exports = function (deps, conf, overrideHttp) { var express = require('express'); //var finalhandler = require('finalhandler'); var serveStatic = require('serve-static'); @@ -244,7 +244,13 @@ module.exports = function (deps, conf) { } }; - var opts = conf.http; + var opts = overrideHttp || conf.http; + if (!opts.defaults) { + opts.defaults = {}; + } + if (!opts.global) { + opts.global = {}; + } if (!opts.sites) { opts.sites = []; } diff --git a/lib/modules/admin.js b/lib/modules/admin.js index a174fac..8aeb0eb 100644 --- a/lib/modules/admin.js +++ b/lib/modules/admin.js @@ -5,9 +5,10 @@ module.exports.create = function (deps, conf) { //var defaultServername = 'localhost.daplie.me'; //var defaultWebRoot = '.'; var assetsPath = path.join(__dirname, '..', '..', 'packages', 'assets'); - var opts = /*conf.http ||*/ {}; + var opts = {}; - opts.sites = []; + opts.global = opts.global || {}; + opts.sites = opts.sites || []; opts.sites._map = {}; // argv.sites @@ -58,7 +59,8 @@ module.exports.create = function (deps, conf) { ] }); - var app = require('../app.js')(deps, { cwd: conf.cwd, http: opts }); + /* device, addresses, cwd, http */ + var app = require('../app.js')(deps, conf, opts); var http = require('http'); return http.createServer(app); }; diff --git a/packages/apis/com.daplie.caddy/index.js b/packages/apis/com.daplie.caddy/index.js index 02c7914..7134e05 100644 --- a/packages/apis/com.daplie.caddy/index.js +++ b/packages/apis/com.daplie.caddy/index.js @@ -139,7 +139,7 @@ module.exports.create = function (deps, conf) { isAuthorized(req, res, function () { if ('POST' !== req.method) { res.setHeader('Content-Type', 'application/json;'); - res.end(JSON.stringify(deps.recase.snakeCopy(conf.snake_copy))); + res.end(JSON.stringify(deps.recase.snakeCopy(conf))); return; }