greenlock-hapi.js/example.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-04-19 04:29:03 +00:00
'use strict';
//
// Configure LetsEncrypt
//
var leConfDir = require('os').homedir() + '/letsencrypt/etc';
throw new Error(
"You must edit the example to change the email address (and remove this error)."
+ " Also, you'll need to remove .testing() and rm -rf '" + leConfDir + "'"
+ " to get actual, trusted production certificates.");
2016-08-16 20:49:12 +00:00
var le = require('letsencrypt-express').create({
server: 'staging' // in production use https://acme-v01.api.letsencrypt.org/directory
, configDir: require('os').homedir() + '/letsencrypt/etc'
, approveDomains: function (opts, certs, cb) {
opts.domains = certs && certs.altnames || opts.domains;
opts.email = 'john.doe@example.com' // CHANGE ME
opts.agreeTos = true;
cb(null, { options: opts, certs: certs });
2016-04-19 04:29:03 +00:00
}
2016-08-16 20:49:12 +00:00
, debug: true
2016-04-19 04:29:03 +00:00
});
//
// Be Hapi
//
var hapi = require('hapi');
var https = require('spdy');
var server = new hapi.Server();
2016-08-16 20:49:12 +00:00
var acmeResponder = le.middleware();
var httpsServer = https.createServer(le.httpsOptions).listen(443);
2016-04-19 04:29:03 +00:00
server.connection({ listener: httpsServer, autoListen: false, tls: true });
2016-08-16 20:49:12 +00:00
2016-04-19 04:29:03 +00:00
server.route({
method: 'GET'
, path: '/.well-known/acme-challenge'
, handler: function (request, reply) {
var req = request.raw.req;
var res = request.raw.res;
reply.close(false);
acmeResponder(req, res);
}
});
server.route({
method: 'GET'
, path: '/'
, handler: function (request, reply) {
reply("Hello, I'm so Hapi!");
}
});
//
// Redirect HTTP to HTTPS
//
var http = require('http');
2016-08-16 20:49:12 +00:00
var redirectHttps = require('redirect-https')();
http.createServer(le.middleware(redirectHttps)).listen(80, function () {
console.log('handle ACME http-01 challenge and redirect to https');
});