-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
523-Added support for HTTPS to Server. (#524)
* Added support for HTTPS to server. * Move boot scripts to helper file catch bad ssl boot config fallback SSL boot to HTTP --------- Co-authored-by: timothycarambat <[email protected]>
- Loading branch information
1 parent
b7d2756
commit 74d2711
Showing
4 changed files
with
88 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { Telemetry } = require("../../models/telemetry"); | ||
const setupTelemetry = require("../telemetry"); | ||
|
||
function bootSSL(app, port = 3001) { | ||
try { | ||
console.log( | ||
`\x1b[33m[SSL BOOT ENABLED]\x1b[0m Loading the certificate and key for HTTPS mode...` | ||
); | ||
const fs = require("fs"); | ||
const https = require("https"); | ||
const privateKey = fs.readFileSync(process.env.HTTPS_KEY_PATH); | ||
const certificate = fs.readFileSync(process.env.HTTPS_CERT_PATH); | ||
const credentials = { key: privateKey, cert: certificate }; | ||
|
||
https | ||
.createServer(credentials, app) | ||
.listen(port, async () => { | ||
await setupTelemetry(); | ||
console.log(`Primary server in HTTPS mode listening on port ${port}`); | ||
}) | ||
.on("error", catchSigTerms); | ||
return app; | ||
} catch (e) { | ||
console.error( | ||
`\x1b[31m[SSL BOOT FAILED]\x1b[0m ${e.message} - falling back to HTTP boot.`, | ||
{ | ||
ENABLE_HTTPS: process.env.ENABLE_HTTPS, | ||
HTTPS_KEY_PATH: process.env.HTTPS_KEY_PATH, | ||
HTTPS_CERT_PATH: process.env.HTTPS_CERT_PATH, | ||
stacktrace: e.stack, | ||
} | ||
); | ||
return bootHTTP(app, port); | ||
} | ||
} | ||
|
||
function bootHTTP(app, port = 3001) { | ||
if (!app) throw new Error('No "app" defined - crashing!'); | ||
|
||
app | ||
.listen(port, async () => { | ||
await setupTelemetry(); | ||
console.log(`Primary server in HTTP mode listening on port ${port}`); | ||
}) | ||
.on("error", catchSigTerms); | ||
return app; | ||
} | ||
|
||
function catchSigTerms() { | ||
process.once("SIGUSR2", function () { | ||
Telemetry.flush(); | ||
process.kill(process.pid, "SIGUSR2"); | ||
}); | ||
process.on("SIGINT", function () { | ||
Telemetry.flush(); | ||
process.kill(process.pid, "SIGINT"); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
bootHTTP, | ||
bootSSL, | ||
}; |