*Move app server logic on port 80 to a dedicated component

This commit is contained in:
Derrick Hammer 2022-09-21 08:54:27 -04:00
parent b35989c8bb
commit e2ccfa5220
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 100 additions and 4993 deletions

45
src/app.ts Normal file
View File

@ -0,0 +1,45 @@
import express, { Express } from "express";
import http from "http";
import { AddressInfo } from "net";
import log from "loglevel";
let app: Express;
let router = express.Router();
let server: http.Server;
export function getRouter(): express.Router {
return router;
}
export function setRouter(newRouter: express.Router): void {
router = newRouter;
}
export async function start() {
server = http.createServer(app);
await new Promise((resolve) => {
server.listen(80, "0.0.0.0", function () {
const address = server.address() as AddressInfo;
log.info(
"HTTP/App Server started on ",
`${address.address}:${address.port}`
);
resolve(null);
});
});
app = express();
app.use(function (req, res, next) {
router(req, res, next);
});
}
export function getApp(): Express {
return app;
}
export function getServer(): http.Server {
return server;
}
export function resetRouter(): void {
setRouter(express.Router());
}

View File

@ -17,37 +17,9 @@ import { AddressInfo } from "net";
import promiseRetry from "promise-retry"; import promiseRetry from "promise-retry";
import { getSslContext } from "./ssl.js"; import { getSslContext } from "./ssl.js";
let app: Express;
let router = express.Router();
export function getRouter(): express.Router {
return router;
}
export function setRouter(newRouter: express.Router): void {
router = newRouter;
}
export function resetRouter(): void {
setRouter(express.Router());
}
export async function start() { export async function start() {
const relayPort = config.uint("port"); const relayPort = config.uint("port");
app = express();
app.use(function (req, res, next) {
router(req, res, next);
});
let httpServer = http.createServer(app);
await new Promise((resolve) => {
httpServer.listen(80, "0.0.0.0", function () {
const address = httpServer.address() as AddressInfo;
log.info("HTTP Server started on ", `${address.address}:${address.port}`);
resolve(null);
});
});
const dht = await getDHT(); const dht = await getDHT();
const statusCodeServer = http.createServer(function (req, res) { const statusCodeServer = http.createServer(function (req, res) {

5020
yarn.lock

File diff suppressed because it is too large Load Diff