*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 { AddressInfo } from "net";
import log from "loglevel"; import log from "loglevel";
import fastify from "fastify";
import type { FastifyInstance } from "fastify";
import { getKeyPair } from "../lib/seed.js"; import { getKeyPair } from "../lib/seed.js";
let app: Express; let app: FastifyInstance;
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() { export async function start() {
app = express(); const keyPair = getKeyPair();
server = http.createServer(app); app = fastify({
resetRouter(); logger: true,
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.use(function (req, res, next) { await app.listen({ port: 80, host: "0.0.0.0" });
router(req, res, next);
});
}
export function getApp(): Express { app.get("/", (req, res) => {
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) => {
res.send(Buffer.from(keyPair.publicKey).toString("hex")); 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}`);
} }