From 00ec1cba843ff34f251eb0c9fa3aa01ddb4fe540 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 11 Aug 2016 22:55:26 -0400 Subject: [PATCH] move examples to examples/ --- examples/master.js | 35 +++++++++++++++++++++++ examples/serve.js | 33 ++++++++++++++++++++++ serve.js => examples/worker.js | 51 +++------------------------------- 3 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 examples/master.js create mode 100644 examples/serve.js rename serve.js => examples/worker.js (68%) diff --git a/examples/master.js b/examples/master.js new file mode 100644 index 0000000..de3dcd8 --- /dev/null +++ b/examples/master.js @@ -0,0 +1,35 @@ +'use strict'; + +var cluster = require('cluster'); + +module.exports.init = function (sharedOpts) { + var numCores = 2; // // Math.max(2, require('os').cpus().length) + var i; + var master = require('../master').create({ + debug: true + + + + , server: 'staging' + , webrootPath: sharedOpts.webrootPath + + + + , approveDomains: function (masterOptions, certs, cb) { + // Depending on your setup it may be more efficient + // for you to implement the approveDomains function + // in your master or in your workers. + // + // Since we implement it in the worker (below) in this example + // we'll give it an immediate approval here in the master + var results = { domain: masterOptions.domain, options: masterOptions, certs: certs }; + cb(null, results); + } + }); + + + + for (i = 0; i < numCores; i += 1) { + master.addWorker(cluster.fork()); + } +}; diff --git a/examples/serve.js b/examples/serve.js new file mode 100644 index 0000000..b2ea641 --- /dev/null +++ b/examples/serve.js @@ -0,0 +1,33 @@ +'use strict'; + +var cluster = require('cluster'); +var main; + + + +// You'll often see examples where people use cluster +// master and worker all in the same file, which is fine, +// but in order to conserve memory and especially to be +// less confusing, I'm splitting the code into two files +if (cluster.isMaster) { + main = require('./master'); +} +else { + main = require('./worker'); +} + + + +// this is nothing letsencrypt-cluster specific +// I'm just arbitrarily choosing to share some configuration +// that I know I'm going to use in both places +main.init({ + + // Depending on the strategy, the whole le-challenge-<> + // could be shared between worker and server, but since I'm just + // using using le-challenge-fs (as you'll see), I'm only sharing the webrootPath + webrootPath: require('os').tmpdir() + require('path').sep + 'acme-challenge' + + // this is used both by node-letsencrypt (master) and le-sni-auto (worker) +, renewWithin: 15 * 24 * 60 * 60 * 1000 +}); diff --git a/serve.js b/examples/worker.js similarity index 68% rename from serve.js rename to examples/worker.js index db0a2f4..b296a37 100644 --- a/serve.js +++ b/examples/worker.js @@ -1,43 +1,7 @@ 'use strict'; -var cluster = require('cluster'); -// TODO the le-challenge-<> should be shared between worker and server -var webrootPath = require('os').tmpdir() + require('path').sep + 'acme-challenge'; - -function runMaster() { - var numCores = 2; // // Math.max(2, require('os').cpus().length) - var i; - var master = require('./lib/master').create({ - debug: true - - - - , server: 'staging' - , webrootPath: webrootPath - - - - , approveDomains: function (masterOptions, certs, cb) { - // Depending on your setup it may be more efficient - // for you to implement the approveDomains function - // in your master or in your workers. - // - // Since we implement it in the worker (below) in this example - // we'll give it an immediate approval here in the master - var results = { domain: masterOptions.domain, options: masterOptions, certs: certs }; - cb(null, results); - } - }); - - - - for (i = 0; i < numCores; i += 1) { - master.addWorker(cluster.fork()); - } -} - -function runWorker() { - var worker = require('./lib/worker').create({ +module.exports.init = function (sharedOpts) { + var worker = require('../worker').create({ debug: true @@ -50,7 +14,7 @@ function runWorker() { - , webrootPath: webrootPath + , webrootPath: sharedOpts.webrootPath @@ -120,11 +84,4 @@ function runWorker() { var server = require('https').createServer(worker.httpsOptions, worker.middleware(app)); plainServer.listen(80); server.listen(443); -} - -if (cluster.isMaster) { - runMaster(); -} -else { - runWorker(); -} +};