*Add newRouter function that defines a root endpoint and returns the pubkey

*Call newRouter in resetRouter
*Call resetRouter in start
This commit is contained in:
Derrick Hammer 2022-09-22 10:18:50 -04:00
parent 36da04bdfe
commit b74bcd5dd2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 15 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import express, { Express } from "express";
import http from "http";
import { AddressInfo } from "net";
import log from "loglevel";
import { getKeyPair } from "./dht";
let app: Express;
let router = express.Router();
@ -18,6 +19,7 @@ export function setRouter(newRouter: express.Router): void {
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;
@ -42,5 +44,17 @@ export function getServer(): http.Server {
}
export function resetRouter(): void {
setRouter(express.Router());
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"));
});
return router;
}