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 <rambat1010@gmail.com>
This commit is contained in:
pritchey 2024-01-04 20:22:15 -05:00 committed by GitHub
parent b7d2756754
commit 74d2711d80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 20 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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";
@ -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);
}

View File

@ -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,
};