Skip to content

Commit 762b209

Browse files
committed
fix: update server object properties
1 parent 6e7c410 commit 762b209

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/index.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1-
const Https = require('https');
21
const Http = require('http');
3-
const fs = require('fs');
2+
const Https = require('https');
43
const tls = require('tls');
54
const acme = require('@cocreate/acme')
5+
const fs = require('fs');
66

77
let server = {
8-
acme: new acme(), // Initially an empty object or some placeholder functionality
9-
10-
loadCertificates(domain) {
11-
try {
12-
return {
13-
key: fs.readFileSync(`/etc/certificates/${domain}/private-key.pem`),
14-
cert: fs.readFileSync(`/etc/certificates/${domain}/fullchain.pem`),
15-
};
16-
} catch (error) {
17-
console.error("Error loading certificates for domain:", domain);
18-
throw error; // Or handle it by returning default certificates
19-
}
20-
},
8+
acme: new acme(),
9+
http: Http.createServer(),
10+
https: Https.createServer({ SNICallback: sniCallback })
11+
};
2112

22-
async sniCallback(domain, cb) {
23-
try {
24-
console.log('sni')
25-
await server.acme.checkCertificate(domain); // Referencing `this.acme`
13+
function loadCertificates(domain) {
14+
try {
15+
return {
16+
key: fs.readFileSync(`/etc/certificates/${domain}/private-key.pem`),
17+
cert: fs.readFileSync(`/etc/certificates/${domain}/fullchain.pem`),
18+
};
19+
} catch (error) {
20+
console.error("Error loading certificates for domain:", domain);
21+
throw error; // Or handle it by returning default certificates
22+
}
23+
}
2624

27-
const sslContext = tls.createSecureContext(server.loadCertificates(domain));
28-
cb(null, sslContext);
29-
} catch (error) {
30-
console.error("Error in SNI callback for domain:", domain, error);
31-
cb(error); // handle error or use default context
32-
}
33-
},
25+
async function sniCallback(domain, cb) {
26+
try {
27+
console.log('sni')
28+
await server.acme.checkCertificate(domain); // Referencing `this.acme`
3429

35-
https: null, // Will be set after defining `sniCallback`
36-
http: Http.createServer()
37-
};
30+
const sslContext = tls.createSecureContext(loadCertificates(domain));
31+
cb(null, sslContext);
3832

39-
// Creating the HTTPS server with the SNI callback
40-
server.https = Https.createServer({ SNICallback: server.sniCallback });
33+
} catch (error) {
34+
console.error("Error in SNI callback for domain:", domain, error);
35+
cb(error); // handle error or use default context
36+
}
37+
}
4138

4239
module.exports = server;

0 commit comments

Comments
 (0)