move examples to examples/

This commit is contained in:
AJ ONeal 2016-08-11 22:55:26 -04:00
parent 1b12c3973b
commit 913cf7a3fd
3 changed files with 72 additions and 47 deletions

35
examples/master.js Normal file
View File

@ -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());
}
};

33
examples/serve.js Normal file
View File

@ -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-<<strategy>>
// 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
});

View File

@ -1,43 +1,7 @@
'use strict'; 'use strict';
var cluster = require('cluster'); module.exports.init = function (sharedOpts) {
// TODO the le-challenge-<<strategy>> should be shared between worker and server var worker = require('../worker').create({
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({
debug: true 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)); var server = require('https').createServer(worker.httpsOptions, worker.middleware(app));
plainServer.listen(80); plainServer.listen(80);
server.listen(443); server.listen(443);
} };
if (cluster.isMaster) {
runMaster();
}
else {
runWorker();
}