Skip to content

Commit

Permalink
523-Added support for HTTPS to Server. (#524)
Browse files Browse the repository at this point in the history
* 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
pritchey and timothycarambat authored Jan 5, 2024
1 parent b7d2756 commit 74d2711
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
11 changes: 10 additions & 1 deletion docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ GID='1000'
# PASSWORDUPPERCASE=1
# PASSWORDNUMERIC=1
# PASSWORDSYMBOL=1
# PASSWORDREQUIREMENTS=4
# PASSWORDREQUIREMENTS=4

###########################################
######## ENABLE HTTPS SERVER ##############
###########################################
# By enabling this and providing the path/filename for the key and cert,
# the server will use HTTPS instead of HTTP.
#ENABLE_HTTPS="true"
#HTTPS_CERT_PATH="sslcert/cert.pem"
#HTTPS_KEY_PATH="sslcert/key.pem"
9 changes: 9 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ VECTOR_DB="lancedb"
#PASSWORDNUMERIC=1
#PASSWORDSYMBOL=1
#PASSWORDREQUIREMENTS=4

###########################################
######## ENABLE HTTPS SERVER ##############
###########################################
# By enabling this and providing the path/filename for the key and cert,
# the server will use HTTPS instead of HTTP.
#ENABLE_HTTPS="true"
#HTTPS_CERT_PATH="sslcert/cert.pem"
#HTTPS_KEY_PATH="sslcert/key.pem"
25 changes: 6 additions & 19 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ const { getVectorDbClass } = require("./utils/helpers");
const { adminEndpoints } = require("./endpoints/admin");
const { inviteEndpoints } = require("./endpoints/invite");
const { utilEndpoints } = require("./endpoints/utils");
const { Telemetry } = require("./models/telemetry");
const { developerEndpoints } = require("./endpoints/api");
const setupTelemetry = require("./utils/telemetry");
const { extensionEndpoints } = require("./endpoints/extensions");
const { bootHTTP, bootSSL } = require("./utils/boot");
const app = express();
const apiRouter = express.Router();
const FILE_LIMIT = "3GB";
Expand Down Expand Up @@ -95,20 +94,8 @@ app.all("*", function (_, response) {
response.sendStatus(404);
});

app
.listen(process.env.SERVER_PORT || 3001, async () => {
await setupTelemetry();
console.log(
`Primary server listening on port ${process.env.SERVER_PORT || 3001}`
);
})
.on("error", function (err) {
process.once("SIGUSR2", function () {
Telemetry.flush();
process.kill(process.pid, "SIGUSR2");
});
process.on("SIGINT", function () {
Telemetry.flush();
process.kill(process.pid, "SIGINT");
});
});
if (!!process.env.ENABLE_HTTPS) {
bootSSL(app, process.env.SERVER_PORT || 3001);
} else {
bootHTTP(app, process.env.SERVER_PORT || 3001);
}
63 changes: 63 additions & 0 deletions server/utils/boot/index.js
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,
};

0 comments on commit 74d2711

Please sign in to comment.