greenlock-hapi.js/example.js

70 lines
1.7 KiB
JavaScript

'use strict';
//
// Configure LetsEncrypt
//
var LEX = require('letsencrypt-express').testing();
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.");
var lex = LEX.create({
configDir: leConfDir
// leave `null` to disable automatic registration
, approveRegistration: function (hostname, cb) {
// Note: this is the place to check your database
// to get the user associated with this domain
cb(null, {
domains: [hostname]
, email: 'CHANGE_ME' // user@example.com
, agreeTos: true
});
}
});
//
// Be Hapi
//
var hapi = require('hapi');
var https = require('spdy');
var server = new hapi.Server();
var acmeResponder = LEX.createAcmeResponder(lex);
var httpsServer = https.createServer(lex.httpsOptions).listen(443);
server.connection({ listener: httpsServer, autoListen: false, tls: true });
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');
http.createServer(LEX.createAcmeResponder(lex, function redirectHttps(req, res) {
res.setHeader('Location', 'https://' + req.headers.host + req.url);
res.statusCode = 302;
res.end('<!-- Hello Developer Person! Please use HTTPS instead -->');
})).listen(80);