config: migrate from nestjs to tasks and websocket server in docker (#316)

* feat: make tasks script run in docker

* feat: make websocket server work in docker

* fix: format issue

* fix: broken lockfile

* fix: non matching typescript versions
This commit is contained in:
Meier Lukas
2024-04-07 11:32:29 +02:00
committed by GitHub
parent 35ca732744
commit 669c6c8955
14 changed files with 173 additions and 68 deletions

View File

@@ -0,0 +1,46 @@
{
"name": "@homarr/websocket",
"version": "0.1.0",
"private": true,
"main": "./src/main.ts",
"types": "./src/main.ts",
"license": "MIT",
"type": "module",
"scripts": {
"dev": "pnpm with-env tsx ./src/main.ts",
"build": "esbuild src/main.ts --bundle --platform=node --outfile=wssServer.cjs --external:bcrypt --loader:.html=text",
"clean": "rm -rf .turbo node_modules",
"lint": "eslint .",
"format": "prettier --check . --ignore-path ../../.gitignore",
"typecheck": "tsc --noEmit",
"with-env": "dotenv -e ../../.env --"
},
"dependencies": {
"@homarr/api": "workspace:^0.1.0",
"@homarr/auth": "workspace:^0.1.0",
"@homarr/common": "workspace:^0.1.0",
"@homarr/db": "workspace:^0.1.0",
"@homarr/definitions": "workspace:^0.1.0",
"@homarr/log": "workspace:^",
"@homarr/redis": "workspace:^0.1.0",
"@homarr/validation": "workspace:^0.1.0",
"ws": "^8.16.0",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@homarr/eslint-config": "workspace:^0.2.0",
"@homarr/prettier-config": "workspace:^0.1.0",
"@homarr/tsconfig": "workspace:^0.1.0",
"@types/ws": "^8.5.10",
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"typescript": "^5.4.4"
},
"eslintConfig": {
"root": true,
"extends": [
"@homarr/eslint-config/base"
]
},
"prettier": "@homarr/prettier-config"
}

View File

@@ -0,0 +1,62 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import { WebSocketServer } from "ws";
import { appRouter, createTRPCContext } from "@homarr/api/websocket";
import { getSessionFromToken, sessionTokenCookieName } from "@homarr/auth";
import { parseCookies } from "@homarr/common";
import { db } from "@homarr/db";
import { logger } from "@homarr/log";
const wss = new WebSocketServer({
port: 3001,
});
const handler = applyWSSHandler({
wss,
router: appRouter,
createContext: async ({ req }) => {
try {
const headers = Object.entries(req.headers).map(
([key, value]) =>
[key, typeof value === "string" ? value : value?.[0]] as [
string,
string,
],
);
const nextHeaders = new Headers(headers);
const store = parseCookies(nextHeaders.get("cookie") ?? "");
const sessionToken = store[sessionTokenCookieName];
const session = await getSessionFromToken(db, sessionToken);
return createTRPCContext({
headers: nextHeaders,
session,
});
} catch (error) {
logger.error(error);
return createTRPCContext({
headers: new Headers(),
session: null,
});
}
},
});
wss.on("connection", (websocket, incomingMessage) => {
logger.info(
` Connection (${wss.clients.size}) ${incomingMessage.method} ${incomingMessage.url}`,
);
websocket.once("close", (code, reason) => {
logger.info(
` Connection (${wss.clients.size}) ${code} ${reason.toString()}`,
);
});
});
logger.info("✅ WebSocket Server listening on ws://localhost:3001");
process.on("SIGTERM", () => {
logger.info("SIGTERM");
handler.broadcastReconnectNotification();
wss.close();
});

View File

@@ -0,0 +1,12 @@
{
"extends": "@homarr/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
},
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["."],
"exclude": ["node_modules"]
}