Server: switch to Fastify

Switch config to TOML over JSON (it's just nicer TBH)
This commit is contained in:
MotorTruck1221 2024-10-13 04:33:25 -06:00
parent deda273eda
commit 620ad25c61
No known key found for this signature in database
GPG key ID: 08F417E2B8B61EA4
20 changed files with 1373 additions and 683 deletions

37
server/serverFactory.ts Normal file
View file

@ -0,0 +1,37 @@
import { createServer } from 'node:http';
import wisp from 'wisp-server-node';
import rammerhead from '@rubynetwork/rammerhead';
import { FastifyServerFactory, FastifyServerFactoryHandler, RawServerDefault } from 'fastify';
import { parsedDoc } from './config.js';
const rh = rammerhead.createRammerhead({
logLevel: parsedDoc.server.server.logging ? 'debug' : 'disabled',
reverseProxy: parsedDoc.server.rammerhead.reverseproxy,
disableLocalStorageSync: parsedDoc.server.rammerhead.localstorage_sync ? false : true,
disableHttp2: parsedDoc.server.rammerhead.http2 ? false : true
});
const serverFactory: FastifyServerFactory = (handler: FastifyServerFactoryHandler): RawServerDefault => {
const httpServer = createServer();
httpServer.on('request', (req, res) => {
if (rammerhead.shouldRouteRh(req)) {
rammerhead.routeRhRequest(rh, req, res);
}
else {
handler(req, res);
}
});
httpServer.on('upgrade', (req, socket, head) => {
if (rammerhead.shouldRouteRh(req)) {
rammerhead.routeRhUpgrade(rh, req, socket, head);
}
else if (parsedDoc.server.server.wisp) {
if (req.url?.endsWith('/wisp/')) {
wisp.routeRequest(req, socket as any, head);
}
}
});
return httpServer;
}
export { serverFactory };