Add option to use TLS
This commit is contained in:
parent
1568c03255
commit
f1b0c27e5d
|
@ -1,17 +1,21 @@
|
|||
"use strict";
|
||||
|
||||
import * as fs from "fs";
|
||||
import * as https from "https";
|
||||
|
||||
import * as appRoot from "app-root-path";
|
||||
import * as express from "express";
|
||||
import { Request } from "express";
|
||||
import * as ws from "express-ws";
|
||||
import * as sslRedirect from "heroku-ssl-redirect";
|
||||
|
||||
import * as api from "./api";
|
||||
import { langs } from "./langs";
|
||||
|
||||
const app = ws(express()).app;
|
||||
const host = process.env.HOST || "localhost";
|
||||
const port = parseInt(process.env.PORT) || 6119;
|
||||
const useTLS = process.env.TLS ? true : false;
|
||||
|
||||
const app = ws(express()).app;
|
||||
|
||||
app.set("query parser", (qs: string) => new URLSearchParams(qs));
|
||||
app.set("view engine", "ejs");
|
||||
|
@ -22,7 +26,13 @@ function getQueryParams(req: Request): URLSearchParams {
|
|||
return (req.query as unknown) as URLSearchParams;
|
||||
}
|
||||
|
||||
app.use(sslRedirect());
|
||||
app.use((req, res, next) => {
|
||||
if (useTLS && req.headers["x-forwarded-proto"] !== "https") {
|
||||
res.redirect(301, "https://" + req.hostname + req.originalUrl);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
app.get("/", (_, res) => {
|
||||
res.render(appRoot.path + "/frontend/pages/index", { langs });
|
||||
});
|
||||
|
@ -57,6 +67,16 @@ app.ws("/api/v1/ws", (ws, req) => {
|
|||
}
|
||||
});
|
||||
|
||||
app.listen(port, host, () =>
|
||||
const secureApp = useTLS
|
||||
? https.createServer(
|
||||
{
|
||||
key: fs.readFileSync("/etc/letsencrypt/live/riju.codes/privkey.pem"),
|
||||
cert: fs.readFileSync("/etc/letsencrypt/live/riju.codes/fullchain.pem"),
|
||||
},
|
||||
app
|
||||
)
|
||||
: app;
|
||||
|
||||
secureApp.listen(port, host, () =>
|
||||
console.log(`Listening on http://${host}:${port}`)
|
||||
);
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
"express": "^4.17.1",
|
||||
"express-ws": "^4.0.0",
|
||||
"file-loader": "^6.0.0",
|
||||
"heroku-ssl-redirect": "^0.0.4",
|
||||
"mkdirp": "^1.0.4",
|
||||
"monaco-editor": "^0.20.0",
|
||||
"node-pty": "^0.9.0",
|
||||
|
|
|
@ -1516,11 +1516,6 @@ hash.js@^1.0.0, hash.js@^1.0.3:
|
|||
inherits "^2.0.3"
|
||||
minimalistic-assert "^1.0.1"
|
||||
|
||||
heroku-ssl-redirect@^0.0.4:
|
||||
version "0.0.4"
|
||||
resolved "https://registry.yarnpkg.com/heroku-ssl-redirect/-/heroku-ssl-redirect-0.0.4.tgz#21ba0707aa503b50a412a0946abfaa88ef7d082c"
|
||||
integrity sha1-IboHB6pQO1CkEqCUar+qiO99CCw=
|
||||
|
||||
hmac-drbg@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
|
||||
|
|
Loading…
Reference in New Issue