Set up backend
This commit is contained in:
parent
83fb885e8b
commit
af18beb134
3 changed files with 1911 additions and 34 deletions
|
|
@ -8,9 +8,12 @@
|
||||||
"format": "prettier --write ."
|
"format": "prettier --write ."
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@fastify/compress": "^6.5.0",
|
||||||
|
"@fastify/static": "^6.12.0",
|
||||||
"@titaniumnetwork-dev/ultraviolet": "^2.0.0",
|
"@titaniumnetwork-dev/ultraviolet": "^2.0.0",
|
||||||
"@tomphttp/bare-server-node": "^2.0.1",
|
"@tomphttp/bare-server-node": "^2.0.1",
|
||||||
"classnames": "^2.3.2",
|
"classnames": "^2.3.2",
|
||||||
|
"fastify": "^4.25.1",
|
||||||
"framer-motion": "^10.16.16",
|
"framer-motion": "^10.16.16",
|
||||||
"i18next": "^23.7.9",
|
"i18next": "^23.7.9",
|
||||||
"i18next-browser-languagedetector": "^7.2.0",
|
"i18next-browser-languagedetector": "^7.2.0",
|
||||||
|
|
@ -19,9 +22,11 @@
|
||||||
"preact-iso": "^2.3.2",
|
"preact-iso": "^2.3.2",
|
||||||
"preact-render-to-string": "^6.3.1",
|
"preact-render-to-string": "^6.3.1",
|
||||||
"preact-router": "^4.1.2",
|
"preact-router": "^4.1.2",
|
||||||
|
"rammerhead": "https://github.com/holy-unblocker/rammerhead/releases/download/v1.2.41-holy.5/rammerhead-1.2.41-holy.5.tgz",
|
||||||
"react-helmet": "^6.1.0",
|
"react-helmet": "^6.1.0",
|
||||||
"react-i18next": "^13.5.0",
|
"react-i18next": "^13.5.0",
|
||||||
"react-icons": "^4.12.0"
|
"react-icons": "^4.12.0",
|
||||||
|
"tsx": "^4.7.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@preact/preset-vite": "^2.5.0",
|
"@preact/preset-vite": "^2.5.0",
|
||||||
|
|
|
||||||
1852
pnpm-lock.yaml
generated
1852
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
86
server.ts
Normal file
86
server.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import fastify from 'fastify';
|
||||||
|
import fastifyStatic from '@fastify/static';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
import path from 'path';
|
||||||
|
import createRammerhead from "rammerhead/src/server/index.js";
|
||||||
|
import { createBareServer } from "@tomphttp/bare-server-node";
|
||||||
|
import { createServer } from "http";
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
const bare = createBareServer("/bare/");
|
||||||
|
const rh = createRammerhead();
|
||||||
|
|
||||||
|
const rammerheadScopes = [
|
||||||
|
"/rammerhead.js",
|
||||||
|
"/hammerhead.js",
|
||||||
|
"/transport-worker.js",
|
||||||
|
"/task.js",
|
||||||
|
"/iframe-task.js",
|
||||||
|
"/worker-hammerhead.js",
|
||||||
|
"/messaging",
|
||||||
|
"/sessionexists",
|
||||||
|
"/deletesession",
|
||||||
|
"/newsession",
|
||||||
|
"/editsession",
|
||||||
|
"/needpassword",
|
||||||
|
"/syncLocalStorage",
|
||||||
|
"/api/shuffleDict",
|
||||||
|
"/mainport"
|
||||||
|
];
|
||||||
|
|
||||||
|
const rammerheadSession = /^\/[a-z0-9]{32}/;
|
||||||
|
|
||||||
|
function shouldRouteRh(req) {
|
||||||
|
const url = new URL(req.url, "http://0.0.0.0");
|
||||||
|
return (
|
||||||
|
rammerheadScopes.includes(url.pathname) ||
|
||||||
|
rammerheadSession.test(url.pathname)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function routeRhRequest(req, res) {
|
||||||
|
rh.emit("request", req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
function routeRhUpgrade(req, socket, head) {
|
||||||
|
rh.emit("upgrade", req, socket, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
const serverFactory = (handler, opts) => {
|
||||||
|
return createServer()
|
||||||
|
.on("request", (req, res) => {
|
||||||
|
if (bare.shouldRoute(req)) {
|
||||||
|
bare.routeRequest(req, res);
|
||||||
|
} else if (shouldRouteRh(req)) {
|
||||||
|
routeRhRequest(req, res);
|
||||||
|
} else {
|
||||||
|
handler(req, res);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.on("upgrade", (req, socket, head) => {
|
||||||
|
if (bare.shouldRoute(req)) {
|
||||||
|
bare.routeUpgrade(req, socket, head);
|
||||||
|
} else if (shouldRouteRh(req)) {
|
||||||
|
routeRhUpgrade(req, socket, head);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const app = fastify({ logger: true, serverFactory });
|
||||||
|
|
||||||
|
app.register(fastifyStatic, {
|
||||||
|
root: path.join(__dirname, 'dist'),
|
||||||
|
prefix: '/',
|
||||||
|
serve: true,
|
||||||
|
wildcard: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
app.setNotFoundHandler((req, res) => {
|
||||||
|
res.sendFile('index.html') // SPA catch-all
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen({
|
||||||
|
port: 8080
|
||||||
|
});
|
||||||
Loading…
Add table
Reference in a new issue