greenlock.js/examples/simple.js

68 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2016-08-04 22:49:35 +00:00
'use strict';
2018-05-19 23:42:49 +00:00
//var Greenlock = require('greenlock');
var Greenlock = require('../');
2016-08-04 22:49:35 +00:00
var db = {};
var config = {
2018-05-19 23:42:49 +00:00
server: 'https://acme-v02.api.letsencrypt.org/directory'
, version: 'draft-11'
2016-08-04 22:49:35 +00:00
2018-05-15 21:42:04 +00:00
, configDir: require('os').homedir() + '/acme/etc' // or /etc/acme or wherever
2016-08-04 22:49:35 +00:00
, privkeyPath: ':config/live/:hostname/privkey.pem' //
, fullchainPath: ':config/live/:hostname/fullchain.pem' // Note: both that :config and :hostname
, certPath: ':config/live/:hostname/cert.pem' // will be templated as expected
, chainPath: ':config/live/:hostname/chain.pem' //
, rsaKeySize: 2048
, debug: true
};
var handlers = {
setChallenge: function (opts, hostname, key, val, cb) { // called during the ACME server handshake, before validation
db[key] = {
hostname: hostname
, key: key
, val: val
};
cb(null);
}
, removeChallenge: function (opts, hostname, key, cb) { // called after validation on both success and failure
db[key] = null;
cb(null);
}
, getChallenge: function (opts, hostname, key, cb) { // this is special because it is called by the webserver
2018-05-15 21:42:04 +00:00
cb(null, db[key].val); // (see greenlock-cli/bin & greenlock-express/standalone),
2016-08-04 22:49:35 +00:00
// not by the library itself
}
, agreeToTerms: function (tosUrl, cb) { // gives you an async way to expose the legal agreement
cb(null, tosUrl); // (terms of use) to your users before accepting
}
};
2018-05-19 23:42:49 +00:00
var greenlock = Greenlock.create(config, handlers);
2018-05-15 21:42:04 +00:00
console.error("CHANGE THE EMAIL, DOMAINS, AND AGREE TOS IN THE EXAMPLE BEFORE RUNNING IT");
process.exit(1);
2016-08-04 22:49:35 +00:00
// checks :conf/renewal/:hostname.conf
2018-05-19 23:42:49 +00:00
greenlock.register({ // and either renews or registers
2016-08-04 22:49:35 +00:00
domains: ['example.com'] // CHANGE TO YOUR DOMAIN
, email: 'user@email.com' // CHANGE TO YOUR EMAIL
, agreeTos: false // set to true to automatically accept an agreement
// which you have pre-approved (not recommended)
, rsaKeySize: 2048
}, function (err) {
if (err) {
// Note: you must have a webserver running
// and expose handlers.getChallenge to it
// in order to pass validation
2018-05-15 21:42:04 +00:00
// See greenlock-cli and or greenlock-express
console.error('[Error]: greenlock/examples/standalone');
2016-08-04 22:49:35 +00:00
console.error(err.stack);
} else {
console.log('success');
}
});