more generalizing of configs

This commit is contained in:
AJ ONeal 2015-11-28 07:30:34 +00:00
parent 134d294cf5
commit 384f465ff1
2 changed files with 17 additions and 12 deletions

View File

@ -25,7 +25,8 @@ var conf = {
, locked: false // TODO XXX , locked: false // TODO XXX
, ipcKey: null , ipcKey: null
, caddyfilepath: config.caddy.conf , caddyfilepath: config.caddy.conf
, sitespath: path.join(__dirname, '..', '..', 'sites-enabled') // TODO needs mappings from db
, caddy: config.caddy
}; };
var state = {}; var state = {};
var caddy; var caddy;
@ -43,7 +44,7 @@ function fork() {
cluster.on('online', function (worker) { cluster.on('online', function (worker) {
var path = require('path'); var path = require('path');
// TODO XXX Should these be configurable? If so, where? // TODO XXX Should these be configurable? If so, where?
var certPaths = [path.join(__dirname, '..', '..', 'certs', 'live')]; var certPaths = config.certPaths;
var info; var info;
conf.ddns = config.ddns; conf.ddns = config.ddns;
conf.redirects = config.redirects; conf.redirects = config.redirects;

View File

@ -3,8 +3,9 @@
function tplCaddyfile(conf) { function tplCaddyfile(conf) {
var contents = []; var contents = [];
conf.domains.forEach(function (hostname) { conf.caddy.domains.forEach(function (hostname) {
var content = ""; var content = "";
var pagesname = hostname;
// TODO prefix // TODO prefix
content+= "https://" + hostname + " {\n" content+= "https://" + hostname + " {\n"
@ -17,10 +18,10 @@ function tplCaddyfile(conf) {
if (conf.locked) { if (conf.locked) {
content += " root /srv/walnut/init.public/\n"; content += " root /srv/walnut/init.public/\n";
} else { } else {
content += " root /srv/walnut/sites-enabled/" + hostname + "/\n"; content += " root " + conf.caddy.sitespath + "/" + pagesname + "/\n";
} }
content += content +=
" proxy /api http://localhost:" + conf.localPort.toString() + " {\n" " proxy /api http://localhost:" + conf.localPort.toString() + " {\n"
+ " proxy_header Host {host}\n" + " proxy_header Host {host}\n"
+ " proxy_header X-Forwarded-Host {host}\n" + " proxy_header X-Forwarded-Host {host}\n"
@ -38,15 +39,18 @@ function tplCaddyfile(conf) {
module.exports.tplCaddyfile = tplCaddyfile; module.exports.tplCaddyfile = tplCaddyfile;
module.exports.create = function (config) { module.exports.create = function (config) {
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var caddypath = config.caddypath; var caddyBin = config.caddy.bin;
var caddyfilepath = config.caddyfilepath; var caddyConf = config.caddy.conf;
var sitespath = config.sitespath; // TODO put up a booting / lock screen on boot
// and wait for all to be grabbed from db
// NOTE caddy cannot yet support multiple roots
// (needed for example.com/appname instead of appname.example.com)
var caddy; var caddy;
var fs = require('fs'); var fs = require('fs');
// TODO this should be expanded to include proxies a la proxydyn // TODO this should be expanded to include proxies a la proxydyn
function writeCaddyfile(conf, cb) { function writeCaddyfile(conf, cb) {
fs.readdir(sitespath, function (err, nodes) { fs.readdir(config.caddy.sitespath, function (err, nodes) {
if (err) { if (err) {
if (cb) { if (cb) {
cb(err); cb(err);
@ -57,12 +61,12 @@ module.exports.create = function (config) {
throw err; throw err;
} }
conf.domains = nodes.filter(function (node) { conf.caddy.domains = nodes.filter(function (node) {
return /\./.test(node) && !/(^\.)|([\/\:\\])/.test(node); return /\./.test(node) && !/(^\.)|([\/\:\\])/.test(node);
}); });
var contents = tplCaddyfile(conf); var contents = tplCaddyfile(conf);
fs.writeFile(caddyfilepath, contents, 'utf8', function (err) { fs.writeFile(caddyConf, contents, 'utf8', function (err) {
if (err) { if (err) {
if (cb) { if (cb) {
cb(err); cb(err);
@ -104,7 +108,7 @@ module.exports.create = function (config) {
// Command failed: killall caddy // Command failed: killall caddy
// caddy: no process found // caddy: no process found
} }
caddy = spawn(caddypath, ['-conf', caddyfilepath], { stdio: ['ignore', 'pipe', 'pipe'] }); caddy = spawn(caddyBin, ['-conf', caddyConf], { stdio: ['ignore', 'pipe', 'pipe'] });
caddy.stdout.on('data', function (str) { caddy.stdout.on('data', function (str) {
console.error('[Caddy]', str.toString('utf8')); console.error('[Caddy]', str.toString('utf8'));
}); });