diff --git a/server/lib/routers/index.ts b/server/lib/routers/index.ts new file mode 100644 index 00000000..6dea168a --- /dev/null +++ b/server/lib/routers/index.ts @@ -0,0 +1,24 @@ +import type { NextFunction, Request, Response } from 'express' +import { initWebchatRouter } from './webchat' + +type InitRoutersOptions = Pick + +async function initRouters ({ + settingsManager, + getRouter, + peertubeHelpers +}: InitRoutersOptions): Promise { + const router = getRouter() + router.get('/ping', (req: Request, res: Response, _next: NextFunction) => res.json({ message: 'pong' })) + + router.use('/webchat', await initWebchatRouter({ + getRouter, + peertubeHelpers, + settingsManager + })) +} + +export { + InitRoutersOptions, + initRouters +} diff --git a/server/lib/routers.ts b/server/lib/routers/webchat.ts similarity index 82% rename from server/lib/routers.ts rename to server/lib/routers/webchat.ts index 39807931..cb0c2430 100644 --- a/server/lib/routers.ts +++ b/server/lib/routers/webchat.ts @@ -1,19 +1,17 @@ +import type { InitRoutersOptions } from './index' +import type { Router, Request, Response, NextFunction } from 'express' import * as path from 'path' -import type { NextFunction, Request, Response } from 'express' const fs = require('fs').promises -type InitRoutersOptions = Pick - -export async function initRouters ({ - settingsManager, +async function initWebchatRouter ({ getRouter, - peertubeHelpers -}: InitRoutersOptions): Promise { - const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../conversejs/index.html')) + peertubeHelpers, + settingsManager +}: InitRoutersOptions): Promise { + const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html')) const router = getRouter() - router.get('/ping', (req: Request, res: Response) => res.json({ message: 'pong' })) - router.get('/webchat', async (req: Request, res: Response, next: NextFunction) => { + router.get('/', async (req: Request, res: Response, next: NextFunction) => { try { const settings = await settingsManager.getSettings([ 'chat-use-builtin', 'chat-room', 'chat-server', @@ -70,4 +68,9 @@ export async function initRouters ({ return next(error) } }) + return router +} + +export { + initWebchatRouter } diff --git a/server/main.ts b/server/main.ts index 7540ff7d..a5e086d9 100644 --- a/server/main.ts +++ b/server/main.ts @@ -1,5 +1,5 @@ import { initSettings } from './lib/settings' -import { initRouters } from './lib/routers' +import { initRouters } from './lib/routers/index' import type { Logger } from 'winston' import decache from 'decache'