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 { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl'
import { newResult, TestResult } from './utils' import { newResult, TestResult } from './utils'
import * as fs from 'fs' 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? // FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here?
// Testing the prosody config file. // Testing the prosody config file.
try { 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. 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`) result.messages.push(`The prosody configuration file (${filePath}) exists`)
const actualContent = await fs.promises.readFile(filePath, { const actualContent = await fs.promises.readFile(filePath, {
@ -30,7 +32,7 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
message: actualContent message: actualContent
}) })
const wantedContent = await getProsodyConfigContent(options) const wantedContent = wantedConfig.content
if (actualContent === wantedContent) { if (actualContent === wantedContent) {
result.messages.push('Prosody configuration file content is correct.') result.messages.push('Prosody configuration file content is correct.')
} else { } 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 const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigContent') logger.debug('Calling getProsodyConfig')
const port = '5280'
const peertubeDomain = 'localhost' const peertubeDomain = 'localhost'
const paths = await getProsodyFilePaths(options) const paths = await getProsodyFilePaths(options)
const logMode: LogMode = 'debug' const logMode: LogMode = 'debug'
return ` const content = `
admins = { } admins = { }
plugin_paths = { } plugin_paths = { }
@ -153,36 +159,35 @@ Component "room.localhost" "muc"
muc_room_default_change_subject = false muc_room_default_change_subject = false
muc_room_default_history_length = 20 muc_room_default_history_length = 20
` `
return {
content,
paths,
port
}
} }
async function getProsodyConfigPath (options: RegisterServerOptions): Promise<string> { async function writeProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigPath')
const paths = await getProsodyFilePaths(options)
return paths.config
}
async function writeProsodyConfig (options: RegisterServerOptions): Promise<void> {
const logger = options.peertubeHelpers.logger const logger = options.peertubeHelpers.logger
logger.debug('Calling writeProsodyConfig') logger.debug('Calling writeProsodyConfig')
logger.debug('Ensuring that the working dir exists') logger.debug('Ensuring that the working dir exists')
await ensureWorkingDir(options) await ensureWorkingDir(options)
logger.debug('Computing the Prosody config content') 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}`) logger.info(`Writing prosody configuration file to ${fileName}`)
await fs.promises.writeFile(fileName, content) await fs.promises.writeFile(fileName, content)
logger.debug('Prosody configuration file writen') logger.debug('Prosody configuration file writen')
return config
} }
export { export {
getProsodyConfigContent, getProsodyConfig,
getWorkingDir, getWorkingDir,
ensureWorkingDir, ensureWorkingDir,
getProsodyFilePaths, getProsodyFilePaths,
getProsodyConfigPath,
writeProsodyConfig 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 fs from 'fs'
import * as child_process from 'child_process' import * as child_process from 'child_process'
@ -99,14 +100,16 @@ async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Pro
result.ok = false // more tests to come result.ok = false // more tests to come
try { 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. 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`) result.messages.push(`The prosody configuration file (${filePath}) exists`)
const actualContent = await fs.promises.readFile(filePath, { const actualContent = await fs.promises.readFile(filePath, {
encoding: 'utf-8' encoding: 'utf-8'
}) })
const wantedContent = await getProsodyConfigContent(options) const wantedContent = wantedConfig.content
if (actualContent === wantedContent) { if (actualContent === wantedContent) {
result.messages.push('Prosody configuration file content is correct.') result.messages.push('Prosody configuration file content is correct.')
} else { } else {
@ -150,9 +153,9 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
// writing the configuration file // writing the configuration file
logger.debug('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 // launch prosody
logger.info('Going to 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'}`) 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> { async function sleep (ms: number): Promise<any> {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(resolve, ms) 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 { getBaseRouter } from '../helpers'
import * as path from 'path' import * as path from 'path'
const fs = require('fs').promises const fs = require('fs').promises
// const httpProxy = require('http-proxy')
async function initWebchatRouter ({ let httpBindRoute: RequestHandler
async function initWebchatRouter (options: RegisterServerOptions): Promise<Router> {
const {
getRouter, getRouter,
peertubeHelpers, peertubeHelpers,
settingsManager settingsManager
}: RegisterServerOptions): Promise<Router> { } = options
const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html')) const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html'))
const router = getRouter() const router: Router = getRouter()
router.get('/', async (req: Request, res: Response, next: NextFunction) => { // eslint-disable-next-line @typescript-eslint/no-misused-promises
router.get('/', async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try { try {
const settings = await settingsManager.getSettings([ const settings = await settingsManager.getSettings([
'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server', 'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server',
@ -25,7 +31,7 @@ async function initWebchatRouter ({
if (settings['chat-use-prosody']) { if (settings['chat-use-prosody']) {
server = 'localhost' server = 'localhost'
room = '{{VIDEO_UUID}}@room.localhost' room = '{{VIDEO_UUID}}@room.localhost'
boshUri = getBaseRouter() + 'http-bind' boshUri = getBaseRouter() + 'webchat/http-bind'
wsUri = '' wsUri = ''
} else if (settings['chat-use-builtin']) { } else if (settings['chat-use-builtin']) {
if (!settings['chat-server']) { if (!settings['chat-server']) {
@ -79,12 +85,42 @@ async function initWebchatRouter ({
res.type('html') res.type('html')
res.send(page) res.send(page)
} catch (error) { } 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 return router
} }
export { function changeHttpBindRoute ({ peertubeHelpers }: RegisterServerOptions, port: string | null): void {
initWebchatRouter 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
} }