Skip to content

Commit fe91e85

Browse files
committed
Create interface that’s isomorphic with Node’s https.createServer()
1 parent 0189845 commit fe91e85

2 files changed

Lines changed: 39 additions & 29 deletions

File tree

‎index.js‎

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
1-
const https = require('https')
21
const Express = require('express')
32
const AcmeTLS = require('./lib/acme-tls')
4-
53
const hostname = require('@small-tech/cross-platform-hostname')
64

75
//
8-
// The API I’m eventually aiming for is:
9-
//
10-
// const https = require('@small-tech/https')
11-
// const Express = require('express')
6+
// Create and configure a basic “Hello, world!” Express app.
127
//
13-
// const app = new Express()
14-
// const server = https.createServer(app)
8+
const app = Express()
9+
10+
app.get('/', (request, response) => {
11+
response.writeHeader(200, {'Content-Type': 'text/html'})
12+
response.end('<!doctype html><html lang=\'en\'><head><meta charset=\'utf-8\'/><title>Hello, world!</title><style>body{background-color: white; font-family: sans-serif;}</style></head><body><h1>Hello, world!</h1></body></html>')
13+
})
14+
1515
//
16-
// So, basically, to be isomorphic with the built-in https module.
16+
// Create an HTTPS server with
1717
//
18-
// (And you can optionally supply an options object as the first argument, which would include the types of options
19-
// you can see below.)
2018

21-
const acmeTLS = AcmeTLS.create({
19+
const options = {
2220
server: 'staging',
2321
configDir: `./config/${hostname}`,
2422
approvedDomains: [hostname],
2523
debug: true
26-
})
27-
28-
// Additional options for the server (none yet).
29-
const options = {}
30-
31-
// Add TLS options from the ACME TLS Certificate to any existing options that
32-
// might have been passed in above. (Currently, this is the SNICallback function.)
33-
Object.assign(options, acmeTLS.tlsOptions)
34-
35-
// Create an Express app.
36-
const app = Express()
37-
38-
app.get('/', (request, response) => {
39-
response.writeHeader(200, {'Content-Type': 'text/html'})
40-
response.end('<!doctype html><html lang=\'en\'><head><meta charset=\'utf-8\'/><title>Hello, world!</title><style>body{background-color: white; font-family: sans-serif;}</style></head><body><h1>Hello, world!</h1></body></html>')
41-
})
24+
}
4225

43-
const server = https.createServer(options, app)
26+
const server = AcmeTLS.createServer(options, app)
4427

4528
server.listen(443, () => {
4629
console.log(' 🎉 Server running on port 443.')

‎lib/acme-tls.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const http = require('http')
2+
const https = require('https')
23
const redirectHTTPS = require('./redirect-https')
34
const hostname = require('@small-tech/cross-platform-hostname')
45

@@ -91,6 +92,32 @@ Greenlock._undefine = function (gl) {
9192

9293
return gl;
9394
};
95+
96+
97+
// Creates an https server with auto Let’s Encrypt certificate management.
98+
// (isomorphic with Node’s built in https.createServer() method).
99+
Greenlock.createServer = function(options, requestListener) {
100+
// The first parameter is optional. If omitted, requestListener should
101+
// be passed as the first argument.
102+
if (typeof options === 'function') {
103+
requestListener = options
104+
options = {}
105+
}
106+
107+
const greenlock = Greenlock.create(options)
108+
109+
// Add TLS options from the ACME TLS Certificate to any existing options that
110+
// might have been passed in above. (Currently, this is the SNICallback function.)
111+
Object.assign(options, greenlock.tlsOptions)
112+
113+
const server = https.createServer(options, requestListener)
114+
115+
console.log('[acme-tls] Created server.')
116+
117+
return server
118+
}
119+
120+
94121
Greenlock.create = function (gl) {
95122
gl.store = require('./le-store-certbot').create({
96123
debug: gl.debug

0 commit comments

Comments
 (0)