From 1f5fcc5059234ab784b4f01e9ba861f37bad206a Mon Sep 17 00:00:00 2001 From: alexa Date: Mon, 27 May 2024 20:21:27 +0200 Subject: [PATCH] add endpoint to get live url --- src/features/live/endpoints.ts | 10 ++++++++++ src/features/liveFeature.ts | 5 ++++- src/swagger.ts | 29 ++++++++++++++++++++++++++++- tools/apiBuilder.mjs | 2 +- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/features/live/endpoints.ts diff --git a/src/features/live/endpoints.ts b/src/features/live/endpoints.ts new file mode 100644 index 0000000..8dd5de7 --- /dev/null +++ b/src/features/live/endpoints.ts @@ -0,0 +1,10 @@ +import { Request, Response } from "express"; + +export class LiveEndpoints { + static getWebsocketEndpoint() { + return (req: Request, res: Response, next: Function) => { + const url = process.env.WEBSOCKET_URL ?? "ws://127.0.0.1:8912"; + res.send(url); + } + } +} \ No newline at end of file diff --git a/src/features/liveFeature.ts b/src/features/liveFeature.ts index 6c6b355..4cb9945 100644 --- a/src/features/liveFeature.ts +++ b/src/features/liveFeature.ts @@ -2,15 +2,18 @@ import {MariaDbDatabase} from "./database/mariaDbDatabase"; import {ServerOptions, WebSocketServer} from "ws"; import {MessagingEndpoints} from "./messaging/endpoints"; import {CLI} from "../tooling/CLI"; -import {Message, User} from "./database/models"; +import {User} from "./database/models"; import {Application} from "express"; import {createServer} from "http"; import {UserWebSocket} from "./live/UserWebSocket"; import {safeUser} from "./authentication/actions"; import {PermissionsList} from "../enums/permissionsList"; +import {LiveEndpoints} from "./live/endpoints"; export class LiveFeature { static enable(app: Application, userMap: Map, db: MariaDbDatabase) { + app.get("/api/live/url", LiveEndpoints.getWebsocketEndpoint()); + const server = createServer(app); const wss = new WebSocketServer({ noServer: true diff --git a/src/swagger.ts b/src/swagger.ts index ef5e2ac..12a3a64 100644 --- a/src/swagger.ts +++ b/src/swagger.ts @@ -1010,7 +1010,34 @@ export const swaggerOptions = { } ] } - } + }, + "/api/live/url": { + get: { + summary: "Get the websocket URL", + tags: [ + "Live" + ], + description: "Get the websocket URL", + responses: { + 200: { + description: "Websocket URL retrieved successfully", + content: { + 'application/json': { + schema: { + type: "object", + properties: { + url: { + type: "string", + description: "The websocket URL" + } + } + } + } + } + } + } + } + }, }, components: { schemas: { diff --git a/tools/apiBuilder.mjs b/tools/apiBuilder.mjs index a85bf35..a3ce441 100644 --- a/tools/apiBuilder.mjs +++ b/tools/apiBuilder.mjs @@ -1,5 +1,5 @@ import fs from "fs"; -import {swaggerOptions} from "../src/swagger.js"; +import {swaggerOptions} from "../src/swagger.ts"; const start = `import {ApiBase} from "./ApiBase.mjs";