*Move app server logic on port 80 to a dedicated component
This commit is contained in:
parent
b35989c8bb
commit
e2ccfa5220
|
@ -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());
|
||||||
|
}
|
28
src/relay.ts
28
src/relay.ts
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue