greenlock-express.js/greenlock-shim.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-10-27 03:59:49 -06:00
"use strict";
module.exports.create = function(opts) {
2019-11-01 15:14:07 -06:00
var Greenlock = require("@root/greenlock");
2019-11-18 22:56:28 -07:00
var Init = require("@root/greenlock/lib/init.js");
2019-11-05 03:09:42 -07:00
var greenlock = opts.greenlock;
2019-11-12 01:46:47 -07:00
2019-11-16 16:50:12 -07:00
/*
2019-11-12 01:46:47 -07:00
if (!greenlock && opts.packageRoot) {
try {
greenlock = require(path.resolve(opts.packageRoot, "greenlock.js"));
} catch (e) {
if ("MODULE_NOT_FOUND" !== e.code) {
throw e;
}
}
}
2019-11-16 16:50:12 -07:00
*/
2019-11-05 03:09:42 -07:00
if (!greenlock) {
2019-11-16 16:50:12 -07:00
opts = Init._init(opts);
greenlock = Greenlock.create(opts);
}
2019-11-18 00:53:26 -07:00
opts.packageAgent = addGreenlockAgent(opts);
2019-11-12 01:46:47 -07:00
2019-11-16 16:50:12 -07:00
try {
if (opts.notify) {
greenlock._defaults.notify = opts.notify;
2019-11-05 03:09:42 -07:00
}
2019-11-16 16:50:12 -07:00
} catch (e) {
console.error("Developer Error: notify not attached correctly");
2019-11-05 03:09:42 -07:00
}
2019-10-28 01:06:43 -06:00
2019-11-01 15:14:07 -06:00
// re-export as top-level function to simplify rpc with workers
greenlock.getAcmeHttp01ChallengeResponse = function(opts) {
return greenlock.challenges.get(opts);
};
2019-10-27 03:59:49 -06:00
2019-11-05 04:01:58 -07:00
greenlock._find({}).then(function(sites) {
if (sites.length <= 0) {
console.warn("warning: No sites available. Did you add them?");
console.warn(" npx greenlock add --subject example.com --altnames example.com");
return;
}
console.info("Ready to Serve:");
2019-11-12 01:46:47 -07:00
2019-11-05 04:01:58 -07:00
var max = 3;
if (sites.length >= 1) {
sites.slice(0, max).forEach(function(site) {
console.info("\t", site.altnames.join(" "));
});
}
if (sites.length > max) {
console.info("and %d others", sites.length - max);
}
});
2019-11-01 15:14:07 -06:00
return greenlock;
2019-10-27 03:59:49 -06:00
};
2019-11-18 00:53:26 -07:00
function addGreenlockAgent(opts) {
// Add greenlock as part of Agent, unless this is greenlock
var packageAgent = opts.packageAgent || "";
if (!/greenlock(-express|-pro)?/i.test(packageAgent)) {
var pkg = require("./package.json");
packageAgent += " Greenlock_Express/" + pkg.version;
}
return packageAgent.trim();
}