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

View File

@ -3,8 +3,9 @@
function tplCaddyfile(conf) {
var contents = [];
conf.domains.forEach(function (hostname) {
conf.caddy.domains.forEach(function (hostname) {
var content = "";
var pagesname = hostname;
// TODO prefix
content+= "https://" + hostname + " {\n"
@ -17,10 +18,10 @@ function tplCaddyfile(conf) {
if (conf.locked) {
content += " root /srv/walnut/init.public/\n";
} 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_header Host {host}\n"
+ " proxy_header X-Forwarded-Host {host}\n"
@ -38,15 +39,18 @@ function tplCaddyfile(conf) {
module.exports.tplCaddyfile = tplCaddyfile;
module.exports.create = function (config) {
var spawn = require('child_process').spawn;
var caddypath = config.caddypath;
var caddyfilepath = config.caddyfilepath;
var sitespath = config.sitespath;
var caddyBin = config.caddy.bin;
var caddyConf = config.caddy.conf;
// 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 fs = require('fs');
// TODO this should be expanded to include proxies a la proxydyn
function writeCaddyfile(conf, cb) {
fs.readdir(sitespath, function (err, nodes) {
fs.readdir(config.caddy.sitespath, function (err, nodes) {
if (err) {
if (cb) {
cb(err);
@ -57,12 +61,12 @@ module.exports.create = function (config) {
throw err;
}
conf.domains = nodes.filter(function (node) {
conf.caddy.domains = nodes.filter(function (node) {
return /\./.test(node) && !/(^\.)|([\/\:\\])/.test(node);
});
var contents = tplCaddyfile(conf);
fs.writeFile(caddyfilepath, contents, 'utf8', function (err) {
fs.writeFile(caddyConf, contents, 'utf8', function (err) {
if (err) {
if (cb) {
cb(err);
@ -104,7 +108,7 @@ module.exports.create = function (config) {
// Command failed: killall caddy
// 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) {
console.error('[Caddy]', str.toString('utf8'));
});