Upgrade bare-server-node

This will install the latest version of the bare server from NPM
This removes the non-git version of the bare server.
This will also allow for WebSocket support by handing the upgrade event.
This commit is contained in:
David Reed 2022-12-07 20:58:57 -05:00
parent 4ef0d2b0ba
commit cafdeb6d00
No known key found for this signature in database
GPG key ID: 2211691D8A1EE72F
13 changed files with 21 additions and 1543 deletions

View file

@ -1,4 +1,4 @@
import { Server } from '../bare/Server.mjs';
import createBareServer from '@tomphttp/bare-server-node'
import http from 'http';
import path from 'path';
import express from 'express';
@ -6,7 +6,7 @@ import { readFile } from 'fs/promises';
import pkg from './routes.mjs';
import { paintSource, tryReadFile } from './randomization.mjs';
const bare = new Server('/bare/', '');
const bare = createBareServer('/bare/');
const config = JSON.parse(await readFile(new URL('./config.json',
import.meta.url)));
const { pages, text404 } = pkg;
@ -14,13 +14,26 @@ const __dirname = path.resolve();
const port = process.env.PORT || config.port;
const app = express();
const router = express.Router();
const server = http.createServer(app);
const server = http.createServer();
server.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
app(req, res);
}
});
router.get('/', async(req, res) => res.send(paintSource(tryReadFile(path.normalize(__dirname + '/views/' + (['/', '/?'].includes(req.url) ? pages.index : pages[Object.keys(req.query)[0]]))))));
app.use(router);
app.use('/', (req, res, next) => {
if (!bare.route_request(req, res)) return next()
});
app.use(express.static(path.normalize(__dirname + '/views')));
app.disable('x-powered-by');
app.use((req, res) => {