Refactoring getProsodyConfig stuffs. Preparing the http bind router.

This commit is contained in:
John Livingston 2021-04-15 12:17:08 +02:00
parent 7dbb964ad7
commit af46ecc3a2
4 changed files with 85 additions and 36 deletions

View File

@ -1,4 +1,4 @@
import { getProsodyConfigContent, getProsodyConfigPath, getWorkingDir } from '../prosody/config'
import { getProsodyConfig, getWorkingDir } from '../prosody/config'
import { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl'
import { newResult, TestResult } from './utils'
import * as fs from 'fs'
@ -18,7 +18,9 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
// FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here?
// Testing the prosody config file.
try {
const filePath = await getProsodyConfigPath(options)
const wantedConfig = await getProsodyConfig(options)
const filePath = wantedConfig.paths.config
await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist.
result.messages.push(`The prosody configuration file (${filePath}) exists`)
const actualContent = await fs.promises.readFile(filePath, {
@ -30,7 +32,7 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
message: actualContent
})
const wantedContent = await getProsodyConfigContent(options)
const wantedContent = wantedConfig.content
if (actualContent === wantedContent) {
result.messages.push('Prosody configuration file content is correct.')
} else {

View File

@ -75,14 +75,20 @@ async function getProsodyFilePaths (options: RegisterServerOptions): Promise<Pro
}
}
async function getProsodyConfigContent (options: RegisterServerOptions): Promise<string> {
interface ProsodyConfig {
content: string
paths: ProsodyFilePaths
port: string
}
async function getProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigContent')
logger.debug('Calling getProsodyConfig')
const port = '5280'
const peertubeDomain = 'localhost'
const paths = await getProsodyFilePaths(options)
const logMode: LogMode = 'debug'
return `
const content = `
admins = { }
plugin_paths = { }
@ -153,36 +159,35 @@ Component "room.localhost" "muc"
muc_room_default_change_subject = false
muc_room_default_history_length = 20
`
return {
content,
paths,
port
}
}
async function getProsodyConfigPath (options: RegisterServerOptions): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigPath')
const paths = await getProsodyFilePaths(options)
return paths.config
}
async function writeProsodyConfig (options: RegisterServerOptions): Promise<void> {
async function writeProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling writeProsodyConfig')
logger.debug('Ensuring that the working dir exists')
await ensureWorkingDir(options)
logger.debug('Computing the Prosody config content')
const content = await getProsodyConfigContent(options)
const config = await getProsodyConfig(options)
const content = config.content
const fileName = config.paths.config
const fileName = await getProsodyConfigPath(options)
logger.info(`Writing prosody configuration file to ${fileName}`)
await fs.promises.writeFile(fileName, content)
logger.debug('Prosody configuration file writen')
return config
}
export {
getProsodyConfigContent,
getProsodyConfig,
getWorkingDir,
ensureWorkingDir,
getProsodyFilePaths,
getProsodyConfigPath,
writeProsodyConfig
}

View File

@ -1,4 +1,5 @@
import { getProsodyConfigContent, getProsodyConfigPath, getProsodyFilePaths, writeProsodyConfig } from './config'
import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config'
import { changeHttpBindRoute } from '../routers/webchat'
import * as fs from 'fs'
import * as child_process from 'child_process'
@ -99,14 +100,16 @@ async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Pro
result.ok = false // more tests to come
try {
const filePath = await getProsodyConfigPath(options)
const wantedConfig = await getProsodyConfig(options)
const filePath = wantedConfig.paths.config
await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist.
result.messages.push(`The prosody configuration file (${filePath}) exists`)
const actualContent = await fs.promises.readFile(filePath, {
encoding: 'utf-8'
})
const wantedContent = await getProsodyConfigContent(options)
const wantedContent = wantedConfig.content
if (actualContent === wantedContent) {
result.messages.push('Prosody configuration file content is correct.')
} else {
@ -150,9 +153,9 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
// writing the configuration file
logger.debug('Writing the configuration file')
await writeProsodyConfig(options)
const config = await writeProsodyConfig(options)
const filePaths = await getProsodyFilePaths(options)
const filePaths = config.paths
// launch prosody
logger.info('Going to launch prosody')
@ -176,6 +179,9 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
logger.info(`Prosody process exited with code ${code ?? 'null'}`)
})
// Set the http-bind route.
changeHttpBindRoute(options, config.port)
async function sleep (ms: number): Promise<any> {
return new Promise((resolve) => {
setTimeout(resolve, ms)

View File

@ -1,17 +1,23 @@
import type { Router, Request, Response, NextFunction } from 'express'
import type { Router, RequestHandler, Request, Response, NextFunction } from 'express'
import { getBaseRouter } from '../helpers'
import * as path from 'path'
const fs = require('fs').promises
// const httpProxy = require('http-proxy')
let httpBindRoute: RequestHandler
async function initWebchatRouter (options: RegisterServerOptions): Promise<Router> {
const {
getRouter,
peertubeHelpers,
settingsManager
} = options
async function initWebchatRouter ({
getRouter,
peertubeHelpers,
settingsManager
}: RegisterServerOptions): Promise<Router> {
const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html'))
const router = getRouter()
router.get('/', async (req: Request, res: Response, next: NextFunction) => {
const router: Router = getRouter()
// eslint-disable-next-line @typescript-eslint/no-misused-promises
router.get('/', async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const settings = await settingsManager.getSettings([
'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server',
@ -25,7 +31,7 @@ async function initWebchatRouter ({
if (settings['chat-use-prosody']) {
server = 'localhost'
room = '{{VIDEO_UUID}}@room.localhost'
boshUri = getBaseRouter() + 'http-bind'
boshUri = getBaseRouter() + 'webchat/http-bind'
wsUri = ''
} else if (settings['chat-use-builtin']) {
if (!settings['chat-server']) {
@ -79,12 +85,42 @@ async function initWebchatRouter ({
res.type('html')
res.send(page)
} catch (error) {
return next(error)
next(error)
}
})
changeHttpBindRoute(options, null)
router.all('/http-bind', (req: Request, res: Response, next: NextFunction) => {
httpBindRoute(req, res, next)
})
return router
}
export {
initWebchatRouter
function changeHttpBindRoute ({ peertubeHelpers }: RegisterServerOptions, port: string | null): void {
const logger = peertubeHelpers.logger
logger.info('Changing http-bind port for ' + (port ?? 'null'))
if (port !== null && !/^\d+$/.test(port)) {
logger.error('Port is not valid. Replacing by null')
port = null
}
if (port === null) {
httpBindRoute = (_req: Request, res: Response, _next: NextFunction) => {
res.status(404)
res.send('Not found')
}
} else {
logger.error('Not implemented yet')
// const proxy = new httpProxy.HttpProxy()
// httpBindRoute = (req: Request, res: Response, _next: NextFunction) => {
// proxy.proxyRequest(req, res, {
// host: 'localhost',
// port: port
// })
// }
}
}
export {
initWebchatRouter,
changeHttpBindRoute
}