From d14163d153f5508615c38459d9eda4ca3dbede7a Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 27 Oct 2019 03:59:49 -0600 Subject: [PATCH] minor refac --- greenlock.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ master.js | 12 +----------- single.js | 32 +------------------------------- 3 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 greenlock.js diff --git a/greenlock.js b/greenlock.js new file mode 100644 index 0000000..713ab23 --- /dev/null +++ b/greenlock.js @@ -0,0 +1,47 @@ +"use strict"; + +var pkg = require("./package.json"); + +module.exports.create = function(opts) { + var Greenlock = require("@root/greenlock"); + var packageAgent = pkg.name + "/" + pkg.version; + if ("string" === typeof opts.packageAgent) { + opts.packageAgent += " "; + } else { + opts.packageAgent = ""; + } + opts.packageAgent += packageAgent; + + var greenlock = Greenlock.create(opts); + greenlock.getAcmeHttp01ChallengeResponse = function(opts) { + return greenlock.find({ servername: opts.servername }).then(function(sites) { + if (!sites.length) { + return null; + } + var site = sites[0]; + if (!site.challenges || !site.challenges["http-01"]) { + return null; + } + + var plugin; + try { + plugin = require(site.challenges["http-01"].module); + plugin = plugin.create(site.challenges["http-01"]); + } catch (e) { + console.error("error getting acme http-01 plugin"); + console.error(e); + return null; + } + + return plugin.get(opts).then(function(result) { + // TODO is this the right way? + var ch = (result && result.challenge) || result || {}; + return { + keyAuthorization: ch.keyAuthorization + }; + }); + }); + }; + + return greenlock; +}; diff --git a/master.js b/master.js index 5089263..819b39f 100644 --- a/master.js +++ b/master.js @@ -6,8 +6,6 @@ var Master = module.exports; var cluster = require("cluster"); var os = require("os"); -var Greenlock = require("@root/greenlock"); -var pkg = require("./package.json"); Master.create = function(opts) { var workers = []; @@ -15,14 +13,7 @@ Master.create = function(opts) { var readyCb; var _kicked = false; - var packageAgent = pkg.name + "/" + pkg.version; - if ("string" === typeof opts.packageAgent) { - opts.packageAgent += " "; - } else { - opts.packageAgent = ""; - } - opts.packageAgent += packageAgent; - var greenlock = Greenlock.create(opts); + var greenlock = require("./greenlock.js").create(opts); var ready = new Promise(function(resolve) { resolveCb = resolve; @@ -69,7 +60,6 @@ GLE.create = function(opts) { gl.tlsOptions = {}; - return master; }; diff --git a/single.js b/single.js index bb1b751..61c8c35 100644 --- a/single.js +++ b/single.js @@ -4,39 +4,9 @@ require("./main.js"); var Single = module.exports; var Servers = require("./servers.js"); -var Greenlock = require("@root/greenlock"); Single.create = function(opts) { - var greenlock = Greenlock.create(opts); - greenlock.getAcmeHttp01ChallengeResponse = function(opts) { - return greenlock.find({ servername: opts.servername }).then(function(sites) { - if (!sites.length) { - return null; - } - var site = sites[0]; - if (!site.challenges || !site.challenges["http-01"]) { - return null; - } - - var plugin; - try { - plugin = require(site.challenges["http-01"].module); - plugin = plugin.create(site.challenges["http-01"]); - } catch (e) { - console.error("error getting acme http-01 plugin"); - console.error(e); - return null; - } - - return plugin.get(opts).then(function(result) { - // TODO is this the right way? - var ch = (result && result.challenge) || result || {}; - return { - keyAuthorization: ch.keyAuthorization - }; - }); - }); - }; + var greenlock = require("./greenlock.js").create(opts); var servers = Servers.create(greenlock, opts);