Routers in separate files.

This commit is contained in:
John Livingston 2021-04-09 19:29:44 +02:00
parent 1728e52e9f
commit dcef708370
3 changed files with 38 additions and 11 deletions

View File

@ -0,0 +1,24 @@
import type { NextFunction, Request, Response } from 'express'
import { initWebchatRouter } from './webchat'
type InitRoutersOptions = Pick<RegisterServerOptions, 'settingsManager' | 'getRouter' | 'peertubeHelpers'>
async function initRouters ({
settingsManager,
getRouter,
peertubeHelpers
}: InitRoutersOptions): Promise<void> {
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
}

View File

@ -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<RegisterServerOptions, 'settingsManager' | 'getRouter' | 'peertubeHelpers'>
export async function initRouters ({
settingsManager,
async function initWebchatRouter ({
getRouter,
peertubeHelpers
}: InitRoutersOptions): Promise<void> {
const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../conversejs/index.html'))
peertubeHelpers,
settingsManager
}: InitRoutersOptions): Promise<Router> {
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
}

View File

@ -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'