*switch app to using fastify

This commit is contained in:
Derrick Hammer 2022-12-19 07:13:11 -05:00
parent 84b69e09af
commit ef03883605
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 11 additions and 47 deletions

View File

@ -1,60 +1,24 @@
import express, { Express } from "express";
import http from "http";
import { AddressInfo } from "net";
import log from "loglevel";
import fastify from "fastify";
import type { FastifyInstance } from "fastify";
import { getKeyPair } from "../lib/seed.js";
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;
}
let app: FastifyInstance;
export async function start() {
app = express();
server = http.createServer(app);
resetRouter();
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);
});
const keyPair = getKeyPair();
app = fastify({
logger: true,
});
app.use(function (req, res, next) {
router(req, res, next);
});
}
await app.listen({ port: 80, host: "0.0.0.0" });
export function getApp(): Express {
return app;
}
export function getServer(): http.Server {
return server;
}
export function resetRouter(): void {
setRouter(newRouter());
}
function newRouter(): express.Router {
const router = express.Router();
let keyPair = getKeyPair();
router.get("/", (req, res) => {
app.get("/", (req, res) => {
res.send(Buffer.from(keyPair.publicKey).toString("hex"));
});
return router;
const address = app.server.address() as AddressInfo;
log.info("HTTP/App Server started on ", `${address.address}:${address.port}`);
}